-
Notifications
You must be signed in to change notification settings - Fork 0
/
forest.go
348 lines (307 loc) · 7.4 KB
/
forest.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package forest
import (
stdcontext "context"
"fmt"
"net/http"
"os"
"reflect"
"runtime"
"sort"
"strings"
"sync"
)
type (
rootGroup = Group
Forest struct {
*rootGroup
mu sync.Mutex
contextPool sync.Pool
node *node
nodes map[string]*node
routes map[string]*Route
notFound []HandlerFunc
methodNotAllowed []HandlerFunc
notFoundRoute *Route
methodNotAllowedRoute *Route
maxParam int
debug bool
hostMatch func(string, string) bool
Server *http.Server
}
HandlerFunc func(Context) error
ErrorHandlerFunc func(error, Context)
)
var (
ErrNotFound = NewError(http.StatusNotFound)
ErrMethodNotAllowed = NewError(http.StatusMethodNotAllowed)
ErrInternalServerError = NewError(http.StatusInternalServerError)
NotFoundMessage = []byte(ErrNotFound.Error())
MethodNotAllowedMessage = []byte(ErrMethodNotAllowed.Error())
NotFoundHandler = func(c Context) error {
return c.Bytes(http.StatusNotFound, NotFoundMessage)
}
MethodNotAllowedHandler = func(c Context) error {
return c.Bytes(http.StatusMethodNotAllowed, MethodNotAllowedMessage)
}
ErrorHandler = func(err error, c Context) {
if err == nil {
return
}
e, ok := err.(*Error)
if !ok {
e = ErrInternalServerError
}
if resp := c.Response(); !resp.Written() {
c.String(e.Code, e.Error())
}
}
)
func handlerName(h HandlerFunc) string {
t := reflect.ValueOf(h).Type()
if t.Kind() == reflect.Func {
return runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
}
return t.String()
}
func combineHandlers(m1 []HandlerFunc, m2 []HandlerFunc) []HandlerFunc {
m := make([]HandlerFunc, 0, len(m1)+len(m2))
m = append(m, m1...)
m = append(m, m2...)
return m
}
func sprintf(format string, args ...interface{}) string {
if len(args) == 0 {
return format
}
return fmt.Sprintf(format, args...)
}
func debugPrint(msg string, args ...interface{}) {
fmt.Fprint(os.Stdout, sprintf(msg, args...))
}
type Option func(*Forest)
func (opt Option) Group() GroupOption {
return func(g *Group) {
opt(g.forest)
}
}
func Debug() Option {
return func(e *Forest) {
e.debug = true
}
}
func HostMatch(matcher func(string, string) bool) Option {
return func(e *Forest) {
e.hostMatch = matcher
}
}
func Middlewares(handlers ...HandlerFunc) Option {
return func(e *Forest) {
e.middlewares = handlers
}
}
func New(opts ...Option) *Forest {
e := &Forest{
node: &node{},
routes: make(map[string]*Route),
}
e.rootGroup = &Group{
forest: e,
middlewares: make([]HandlerFunc, 0),
}
e.contextPool = sync.Pool{
New: func() interface{} {
return e.NewContext(nil, nil)
},
}
e.Logger = newLogger()
e.ErrorHandler = ErrorHandler
e.NotFound(NotFoundHandler)
e.MethodNotAllowed(MethodNotAllowedHandler)
e.SetOptions(opts...)
return e
}
func HostMatcher(host, dst string) bool {
c := len(host) - len(dst)
if c < 0 {
return false
}
index := strings.IndexRune(dst, '*')
if index < 0 {
return false
}
return dst[:index] == host[:index] && dst[index+1:] == host[c+index+1:]
}
func WrapHandler(h http.Handler) HandlerFunc {
return func(c Context) error {
h.ServeHTTP(c.Response(), c.Request())
return nil
}
}
func WrapHandlerFunc(h http.HandlerFunc) HandlerFunc {
return func(c Context) error {
h(c.Response(), c.Request())
return nil
}
}
func (e *Forest) addRoute(host, method, path string) *Route {
key := host + method + path
if route, ok := e.routes[key]; ok {
return route
}
root := e.node
if host != "" {
if e.nodes == nil {
e.nodes = make(map[string]*node, 0)
}
h, ok := e.nodes[host]
if !ok {
h = &node{}
e.nodes[host] = h
}
root = h
}
route := &Route{
host: host,
method: method,
path: path,
}
root.insert(route.Path(), route)
// maybe should check route.pnames can't be repeated
if l := len(route.pnames); l > e.maxParam {
e.maxParam = l
}
e.routes[key] = route
return route
}
func (e *Forest) findHost(host string) *node {
if host != "" && len(e.nodes) > 0 {
if node, ok := e.nodes[host]; ok {
return node
}
if e.hostMatch == nil {
return e.node
}
for h, node := range e.nodes {
if e.hostMatch(host, h) {
return node
}
}
}
return e.node
}
func (e *Forest) findRoute(host, method, path string, params *contextParams) *Route {
n := e.findHost(host).find(path, params)
if n == nil || n.routes == nil || len(n.routes) == 0 {
return e.notFoundRoute
}
for _, route := range n.routes {
if route.Method() == method {
return route
}
}
return e.methodNotAllowedRoute
}
func (e *Forest) Route(name string) *Route {
for _, route := range e.routes {
if route.Name == name {
return route
}
}
return nil
}
func (e *Forest) Routes() []*Route {
routes := make([]*Route, 0, len(e.routes))
for _, r := range e.routes {
routes = append(routes, r)
}
sort.Slice(routes, func(i, j int) bool {
return routes[i].Path() < routes[j].Path()
})
return routes
}
func (e *Forest) URL(name string, args ...interface{}) string {
if r := e.Route(name); r != nil {
return r.URL(args...)
}
return ""
}
func (e *Forest) NotFound(handlers ...HandlerFunc) *Route {
if e.notFoundRoute == nil {
e.notFoundRoute = &Route{}
}
e.notFound = handlers
e.notFoundRoute.handlers = combineHandlers(e.middlewares, e.notFound)
return e.notFoundRoute
}
func (e *Forest) MethodNotAllowed(handlers ...HandlerFunc) *Route {
if e.methodNotAllowedRoute == nil {
e.methodNotAllowedRoute = &Route{}
}
e.methodNotAllowed = handlers
e.methodNotAllowedRoute.handlers = combineHandlers(e.middlewares, e.methodNotAllowed)
return e.methodNotAllowedRoute
}
func (e *Forest) Use(middlewares ...HandlerFunc) *Forest {
e.rootGroup.Use(middlewares...)
e.notFoundRoute.handlers = combineHandlers(e.middlewares, e.notFound)
e.methodNotAllowedRoute.handlers = combineHandlers(e.middlewares, e.methodNotAllowed)
return e
}
func (e *Forest) Mount(child *Forest, opts ...GroupOption) {
e.rootGroup.Mount(child.rootGroup, opts...)
}
func (e *Forest) MountGroup(child *Group, opts ...GroupOption) {
e.rootGroup.Mount(child, opts...)
}
func (e *Forest) NewContext(w http.ResponseWriter, r *http.Request) *context {
c := &context{
params: &contextParams{
pvalues: make([]string, e.maxParam),
},
response: NewResponse(w),
}
return c
}
func (e *Forest) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c := e.contextPool.Get().(*context)
c.reset(r, w)
defer e.contextPool.Put(c)
path := r.URL.RawPath
if path == "" {
path = r.URL.Path
}
// pass []string is faster than *context than *([]string)
c.route = e.findRoute(r.Host, r.Method, path, c.params)
c.Next()
}
func (e *Forest) configure(addr string) error {
e.mu.Lock()
defer e.mu.Unlock()
if e.debug {
for _, r := range e.Routes() {
debugPrint(r.String())
}
debugPrint("Listening and serving HTTP on %s\n", addr)
}
if e.Server == nil {
e.Server = &http.Server{Handler: e}
}
e.Server.Addr = addr
return nil
}
func (e *Forest) SetOptions(opts ...Option) {
for _, opt := range opts {
opt(e)
}
}
func (e *Forest) Start(addr string) error {
e.configure(addr)
return e.Server.ListenAndServe()
}
func (e *Forest) StartTLS(addr string, certFile, keyFile string) error {
e.configure(addr)
return e.Server.ListenAndServeTLS(certFile, keyFile)
}
func (e *Forest) Shutdown(ctx stdcontext.Context) error {
return e.Server.Shutdown(ctx)
}