-
Notifications
You must be signed in to change notification settings - Fork 4
/
router.go
189 lines (155 loc) · 4.7 KB
/
router.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package httpway
import (
"encoding/json"
"github.com/julienschmidt/httprouter"
"net/http"
"reflect"
)
type Handler func(http.ResponseWriter, *http.Request)
func New() *Router {
return &Router{
Router: httprouter.New(),
}
}
type Router struct {
*httprouter.Router
SessionManager SessionManager
Logger Logger
prev *Router
handle Handler
}
// register a GET handler with path
func (r *Router) GET(path string, handle Handler) {
r.Handle("GET", path, handle)
}
// register a HEAD handler with path
func (r *Router) HEAD(path string, handle Handler) {
r.Handle("HEAD", path, handle)
}
// register a OPTIONS handler with path
func (r *Router) OPTIONS(path string, handle Handler) {
r.Handle("OPTIONS", path, handle)
}
// register a POST handler with path
func (r *Router) POST(path string, handle Handler) {
r.Handle("POST", path, handle)
}
// register a POST handler with path and payload to be decoded from body
func (r *Router) POSTwPayload(path string, handle Handler, payload interface{}) {
r.HandleWPayload("POST", path, handle, payload)
}
// register a PUT handler with path
func (r *Router) PUT(path string, handle Handler) {
r.Handle("PUT", path, handle)
}
// register a PUT handler with path and payload to be decoded from body
func (r *Router) PUTwPayload(path string, handle Handler, payload interface{}) {
r.HandleWPayload("PUT", path, handle, payload)
}
// register a PATCH handler with path
func (r *Router) PATCH(path string, handle Handler) {
r.Handle("PATCH", path, handle)
}
// register a DELETE handler with path
func (r *Router) DELETE(path string, handle Handler) {
r.Handle("DELETE", path, handle)
}
// Handle registers a new request handle with the given path and method.
//
// For GET, POST, PUT, PATCH and DELETE requests the respective shortcut
// functions can be used.
//
// This function is intended for bulk loading and to allow the usage of less
// frequently used, non-standardized or custom methods (e.g. for internal
// communication with a proxy).
func (r *Router) Handle(method, path string, handle Handler) {
newHandle := r.GenerateChainHandler(handle)
r.Router.Handle(method, path, newHandle)
}
// HandleWPayload it's the same with Handle only will try to get from the
// body of the request the data and json unmarshal to the payload type
func (r *Router) HandleWPayload(method, path string, handle Handler, payload interface{}) {
payloadType := reflect.TypeOf(payload)
newhandler := func(hw http.ResponseWriter, hr *http.Request) {
ctx := GetContext(hr)
decoder := json.NewDecoder(hr.Body)
defer hr.Body.Close()
var decodedPayload = reflect.New(payloadType).Interface()
err := decoder.Decode(decodedPayload)
if err != nil {
hw.WriteHeader(400)
if r.Logger != nil {
r.Logger.Error("Unable to decode your payload for %s (%s)", payloadType.Name(), err)
}
return
}
ctx.payload = decodedPayload
handle(hw, hr)
}
r.Handle(method, path, newhandler)
}
//Add a middleware before (and after) the handler run
// router := httpway.New()
// public := router.Middleware(AccessLogger)
// private := public.Middleware(AuthCheck)
//
// public.GET("/public", somePublicHandler)
// private.GET("/private", somePrivateHandler)
//
// func AccessLogger(w http.ResponseWriter, r *http.Request) {
// startTime:=time.Now()
//
// httpway.GetContext(r).Next(w, r)
//
// fmt.Printf("Request: %s duration: %s\n", r.URL.EscapedPath(), time.Since(startTime))
// }
//
// func AuthCheck(w http.ResponseWriter, r *http.Request) {
// ctx := httpway.GetContext(r)
//
// if !ctx.Session().IsAuth() {
// http.Error(w, "Auth required", 401)
// return
// }
// ctx.Next(w, r)
// }
//
func (r *Router) Middleware(handle Handler) *Router {
rt := &Router{
prev: r,
handle: handle,
Router: r.Router,
Logger: r.Logger,
SessionManager: r.SessionManager,
}
return rt
}
//get httprouter handler with all the middlewares chained
func (router *Router) GenerateChainHandler(handle Handler) httprouter.Handle {
if router.prev == nil {
return func(w http.ResponseWriter, r *http.Request, pr httprouter.Params) {
w = CreateContext(router, w, r, nil, nil, &pr)
handle(w, r)
}
}
var (
lastMiddleware Handler
middlewareList = make([]Handler, 0)
)
mid := router
middlewareList = append(middlewareList, handle)
for mid.prev != nil {
if mid.prev.handle == nil {
lastMiddleware = mid.handle
break
}
middlewareList = append(middlewareList, mid.handle)
mid = mid.prev
}
middlewareListLen := len(middlewareList)
httprouterHandler := func(w http.ResponseWriter, r *http.Request, pr httprouter.Params) {
w = CreateContext(router, w, r, &middlewareList, &middlewareListLen, &pr)
lastMiddleware(w, r)
}
return httprouterHandler
}