forked from socialpoint-labs/bsk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
101 lines (78 loc) · 2.05 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
package httpx
import (
"net/http"
"strings"
"context"
)
// NewRouter returns a new Router struct
func NewRouter(opts ...Option) *Router {
options := &options{}
for _, o := range opts {
o(options)
}
if options.responder == nil {
options.responder = NewResponder()
}
return &Router{
options: options,
mux: http.NewServeMux(),
}
}
// Router allows to route requests to according handlers
type Router struct {
options *options
routes []*route
mux *http.ServeMux
initialized bool
}
// Route registers a route pattern against its handler
func (router *Router) Route(pattern string, handler http.Handler, decorators ...Decorator) {
router.routes = append(router.routes, &route{pattern, handler, decorators})
}
// ServeHTTP implements http.Handler
func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !router.initialized {
registerRoutes(router.mux, router, "", []Decorator{})
router.initialized = true
}
h, _ := router.mux.Handler(r)
ctx := context.WithValue(r.Context(), responderKey, router.options.responder)
r = r.WithContext(ctx)
h.ServeHTTP(w, r)
}
type route struct {
pattern string
handler http.Handler
decorators []Decorator
}
func registerRoutes(mux *http.ServeMux, router *Router, prefix string, decorators []Decorator) {
for _, r := range router.routes {
uri := prefix + r.pattern
if uri != "/" {
uri = strings.TrimRight(uri, "/")
}
if child, ok := r.handler.(*Router); ok {
registerRoutes(mux, child, uri, append(r.decorators, decorators...))
} else {
if uri == "/" {
r.decorators = append(r.decorators, RootDecorator())
}
if prefix != "" {
r.decorators = append(r.decorators, StripPrefixDecorator(prefix))
}
for _, decorator := range append(r.decorators, decorators...) {
r.handler = decorator(r.handler)
}
mux.Handle(uri, r.handler)
}
}
}
// Option is the common type of functions that set options
type Option func(*options)
type options struct {
responder *Responder
}
type contextKey int
const (
responderKey contextKey = iota
)