forked from MordFustang21/nova
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nova.go
269 lines (220 loc) · 5.82 KB
/
nova.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Package nova is an HTTP request multiplexer. It matches the URL of each incoming request against a list of registered patterns
// and calls the handler for the pattern that most closely matches the URL. As well as providing some nice logging and response features.
package nova
import (
"net/http"
"path"
"strings"
)
// Server represents the router and all associated data
type Server struct {
// radix tree for looking up routes
paths map[string]*Node
middleWare []Middleware
// error callback func
errorFunc ErrorFunc
// debug defines logging for requests
debug bool
}
// RequestFunc is the callback used in all handler func
type RequestFunc func(req *Request) error
// ErrorFunc is the callback used for errors
type ErrorFunc func(req *Request, err error)
// Node holds a single route with accompanying children routes
type Node struct {
route *Route
children map[string]*Node
}
// Middleware holds all middleware functions
type Middleware struct {
middleFunc func(*Request, func())
}
// New returns new supernova router
func New() *Server {
s := new(Server)
s.paths = make(map[string]*Node)
return s
}
// EnableDebug toggles output for incoming requests
func (sn *Server) EnableDebug(debug bool) {
if debug {
sn.debug = true
}
}
// Error sets the callback for errors
func (sn *Server) Error(f ErrorFunc) {
sn.errorFunc = f
}
// handler is the main entry point into the router
func (sn *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
request := NewRequest(w, r)
var logMethod func()
if sn.debug {
logMethod = getDebugMethod(request)
}
if logMethod != nil {
defer logMethod()
}
// Run Middleware
finished := sn.runMiddleware(request)
if !finished {
return
}
route := sn.climbTree(request.GetMethod(), cleanPath(request.URL.Path))
if route != nil {
err := route.call(request)
// if we got error and erroFunc is set pass along
if err != nil {
if sn.errorFunc != nil {
sn.errorFunc(request, err)
}
}
if request.ResponseCode == 0 {
request.ResponseCode = http.StatusOK
}
return
}
http.NotFound(request.ResponseWriter, request.Request)
}
// All adds route for all http methods
func (sn *Server) All(route string, routeFunc RequestFunc) {
sn.addRoute("", buildRoute(route, routeFunc))
}
// Get adds only GET method to route
func (sn *Server) Get(route string, routeFunc RequestFunc) {
sn.addRoute("GET", buildRoute(route, routeFunc))
}
// Post adds only POST method to route
func (sn *Server) Post(route string, routeFunc RequestFunc) {
sn.addRoute("POST", buildRoute(route, routeFunc))
}
// Put adds only PUT method to route
func (sn *Server) Put(route string, routeFunc RequestFunc) {
sn.addRoute("PUT", buildRoute(route, routeFunc))
}
// Delete adds only DELETE method to route
func (sn *Server) Delete(route string, routeFunc RequestFunc) {
sn.addRoute("DELETE", buildRoute(route, routeFunc))
}
// Restricted adds route that is restricted by method
func (sn *Server) Restricted(method, route string, routeFunc RequestFunc) {
sn.addRoute(method, buildRoute(route, routeFunc))
}
// addRoute takes route and method and adds it to route tree
func (sn *Server) addRoute(method string, route *Route) {
if sn.paths[method] == nil {
sn.paths[method] = newNode()
}
parts := strings.Split(route.route, "/")
currentNode := sn.paths[method]
for index, val := range parts {
childKey := val
if len(val) > 1 {
if val[0] == ':' {
childKey = ""
}
}
// see if node already exists
if node, ok := currentNode.children[childKey]; ok {
currentNode = node
} else {
node := newNode()
currentNode.children[childKey] = node
currentNode = node
}
if index == len(parts)-1 {
node := newNode()
node.route = route
currentNode.children[childKey] = node
}
}
}
func newNode() *Node {
n := new(Node)
n.children = make(map[string]*Node)
return n
}
// climbTree takes in path and traverses tree to find route
func (sn *Server) climbTree(method, path string) *Route {
parts := strings.Split(path, "/")
currentNode, ok := sn.paths[method]
if !ok {
currentNode, ok = sn.paths[""]
if !ok {
return nil
}
}
for _, val := range parts {
var node *Node
node = currentNode.children[val]
if node == nil {
node = currentNode.children[""]
}
if node == nil {
return nil
}
currentNode = node
}
if node, ok := currentNode.children[parts[len(parts)-1]]; ok {
return node.route
}
if node, ok := currentNode.children[""]; ok {
return node.route
}
return nil
}
// buildRoute creates new Route
func buildRoute(route string, routeFunc RequestFunc) *Route {
routeObj := new(Route)
routeObj.routeFunc = routeFunc
routeObj.routeParamsIndex = make(map[int]string)
if route[len(route)-1] == '/' && len(route) > 1 {
route = route[:len(route)-1]
}
routeObj.route = route
return routeObj
}
// Use adds a new function to the middleware stack
func (sn *Server) Use(f func(req *Request, next func())) {
if sn.middleWare == nil {
sn.middleWare = make([]Middleware, 0)
}
middle := new(Middleware)
middle.middleFunc = f
sn.middleWare = append(sn.middleWare, *middle)
}
// Internal method that runs the middleware
func (sn *Server) runMiddleware(req *Request) bool {
stackFinished := true
for m := range sn.middleWare {
nextCalled := false
sn.middleWare[m].middleFunc(req, func() {
nextCalled = true
})
if !nextCalled {
stackFinished = false
break
}
}
return stackFinished
}
// cleanPath returns the canonical path for p, eliminating . and .. elements.
// Borrowed from the net/http package.
func cleanPath(p string) string {
if p == "" || p == "/" {
return "/"
}
if p[0] != '/' {
p = "/" + p
}
if p[len(p)-1] == '/' {
p = p[:len(p)-1]
}
np := path.Clean(p)
// path.Clean removes trailing slash except for root;
// put the trailing slash back if necessary.
if p[len(p)-1] == '/' && np != "/" {
np += "/"
}
return np
}