-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
152 lines (122 loc) · 4.16 KB
/
request.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package web
import (
"github.com/procyon-projects/goo"
"net/http"
)
type RequestObjectCache struct {
hasOnlyBody bool
bodyFieldIndex int
paramFieldIndex int
pathFieldIndex int
headerFieldIndex int
fields []goo.Field
}
type RequestHandlerObject interface{}
type RequestHandlerFunction = func(context *WebRequestContext)
type RequestHandlerOption func(handler *RequestHandler)
type RequestMethod string
const (
unknownMethod RequestMethod = "[unknown-method]"
RequestMethodGet RequestMethod = http.MethodGet
RequestMethodPost RequestMethod = http.MethodPost
RequestMethodPut RequestMethod = http.MethodPut
RequestMethodDelete RequestMethod = http.MethodDelete
RequestMethodPatch RequestMethod = http.MethodPatch
RequestMethodOptions RequestMethod = http.MethodOptions
RequestMethodHead RequestMethod = http.MethodHead
)
type RequestHandler struct {
Path string
Method RequestMethod
HandlerFunc RequestHandlerFunction
RequestObject RequestHandlerObject
requestObjectMetadata *RequestObjectMetadata
}
func newHandler(handler RequestHandlerFunction, method RequestMethod, options ...RequestHandlerOption) RequestHandler {
if handler == nil {
panic("Handler must not be null")
}
handlerType := goo.GetType(handler)
if !handlerType.IsFunction() {
panic("Handler must be function")
}
requestHandler := &RequestHandler{
HandlerFunc: handler,
Method: method,
}
for _, option := range options {
option(requestHandler)
}
if requestHandler.RequestObject != nil {
requestHandler.requestObjectMetadata = ScanRequestObjectMetadata(requestHandler.RequestObject)
}
return *requestHandler
}
func Get(handler RequestHandlerFunction, options ...RequestHandlerOption) RequestHandler {
return newHandler(handler, RequestMethodGet, options...)
}
func Post(handler RequestHandlerFunction, options ...RequestHandlerOption) RequestHandler {
return newHandler(handler, RequestMethodPost, options...)
}
func Put(handler RequestHandlerFunction, options ...RequestHandlerOption) RequestHandler {
return newHandler(handler, RequestMethodPut, options...)
}
func Delete(handler RequestHandlerFunction, options ...RequestHandlerOption) RequestHandler {
return newHandler(handler, RequestMethodDelete, options...)
}
func Patch(handler RequestHandlerFunction, options ...RequestHandlerOption) RequestHandler {
return newHandler(handler, RequestMethodPatch, options...)
}
func Options(handler RequestHandlerFunction, options ...RequestHandlerOption) RequestHandler {
return newHandler(handler, RequestMethodOptions, options...)
}
func Head(handler RequestHandlerFunction, options ...RequestHandlerOption) RequestHandler {
return newHandler(handler, RequestMethodHead, options...)
}
func RequestObject(requestObject RequestHandlerObject) RequestHandlerOption {
return func(handler *RequestHandler) {
handler.RequestObject = requestObject
}
}
func Path(path string) RequestHandlerOption {
return func(handler *RequestHandler) {
handler.Path = path
}
}
type HandlerRegistry interface {
Register(info ...RequestHandler)
RegisterGroup(prefix string, info ...RequestHandler)
}
type SimpleHandlerRegistry struct {
registryMap map[string][]RequestHandler
}
func NewSimpleHandlerRegistry() SimpleHandlerRegistry {
return SimpleHandlerRegistry{
registryMap: make(map[string][]RequestHandler),
}
}
func (registry SimpleHandlerRegistry) Register(info ...RequestHandler) {
registry.RegisterGroup("", info...)
}
func (registry SimpleHandlerRegistry) RegisterGroup(prefix string, info ...RequestHandler) {
if len(info) == 0 {
return
}
for _, handler := range info {
if prefix+handler.Path == "" {
panic("Specify a path or a prefix while registering a request handler")
}
}
if registry.registryMap[prefix] == nil {
registry.registryMap[prefix] = make([]RequestHandler, 0)
}
registry.registryMap[prefix] = append(registry.registryMap[prefix], info...)
}
func (registry SimpleHandlerRegistry) clear() {
for key := range registry.registryMap {
delete(registry.registryMap, key)
}
}
func (registry SimpleHandlerRegistry) getRegistryMap() map[string][]RequestHandler {
return registry.registryMap
}