-
Notifications
You must be signed in to change notification settings - Fork 7
/
routes.go
107 lines (99 loc) · 2.52 KB
/
routes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package banjo
import (
"reflect"
)
// Routes struct
//
// Used for storing users closures
// The struct have one map for each method table
// table type: map[string]func(ctx *Context)
//
type Routes struct {
GET map[string]func(ctx *Context)
POST map[string]func(ctx *Context)
PUT map[string]func(ctx *Context)
PATCH map[string]func(ctx *Context)
OPTIONS map[string]func(ctx *Context)
HEAD map[string]func(ctx *Context)
DELETE map[string]func(ctx *Context)
}
// CreateRoutes function
//
// Create Routes with empty method tables
//
// Params:
// - None
//
// Response:
// - routes {Routes} Routes struct with empty map fields
//
func CreateRoutes() Routes {
return Routes{
GET: make(map[string]func(ctx *Context)),
POST: make(map[string]func(ctx *Context)),
PUT: make(map[string]func(ctx *Context)),
PATCH: make(map[string]func(ctx *Context)),
OPTIONS: make(map[string]func(ctx *Context)),
HEAD: make(map[string]func(ctx *Context)),
DELETE: make(map[string]func(ctx *Context)),
}
}
// Block function
//
// Returns func(request Request) Response closure
//
// Params:
// - method {string} HTTP Request Method
// - url {string} HTTP Request URL
//
// Response:
// - closure {func(request Request) Response} Returns closure with user
//
func (routes Routes) Block(method string, url string) func(ctx *Context) {
object := reflect.ValueOf(routes)
value := reflect.Indirect(object).FieldByName(method)
intf := value.Interface()
table := intf.(map[string]func(ctx *Context))
block, ok := table[url]
if ok {
return block
}
return notFound()
}
// Push function
//
// Adding new element to one of fields in Routes struct
// Element should be passed by url {string},
// value type of {func(request Request) Response}
//
// Params:
// - method {string} HTTP Request Method
// - url {string} HTTP Request URL
// - closure {func(request Request) Response}
//
// Response:
// - None
//
func (routes Routes) Push(method string, url string, closure func(ctx *Context)) {
object := reflect.ValueOf(routes)
object = reflect.Indirect(object)
value := object.FieldByName(method)
value.SetMapIndex(reflect.ValueOf(url), reflect.ValueOf(closure))
}
// notFound function
//
// Returns default error page Response if
// closure with given method & url are not available
//
// Params:
// - None
//
// Response:
// - closure {func(request Request) Response} Default error closure
//
func notFound() func(ctx *Context) {
return func(ctx *Context) {
ctx.Response.Body = "Page Not Found"
ctx.Response.Status = 404
}
}