-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
136 lines (119 loc) · 3.08 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
package goblog
import (
"net/http"
"strings"
"github.com/teambition/trie-mux"
)
type Router struct {
root string
rt string
trie *trie.Trie
otherwise Middleware
middleware Middleware
mds []Middleware
}
type RouterOptions struct {
Root string
IgnoreCase bool
FixedPathRedirect bool
TrailingSlashRedirect bool
}
var defaultRouterOptions = RouterOptions{
Root: "/",
IgnoreCase: true,
FixedPathRedirect: true,
TrailingSlashRedirect: true,
}
func NewRouter(routerOptions ...RouterOptions) *Router {
opts := defaultRouterOptions
if len(routerOptions) > 0 {
opts = routerOptions[0]
}
if opts.Root == "" || opts.Root[len(opts.Root) - 1] != '/' {
opts.Root += "/"
}
return &Router{
root: opts.Root,
rt: opts.Root[0 : len(opts.Root)-1],
mds: make([]Middleware, 0),
trie: trie.New(trie.Options{
IgnoreCase: opts.IgnoreCase,
FixedPathRedirect: opts.FixedPathRedirect,
TrailingSlashRedirect: opts.TrailingSlashRedirect,
}),
}
}
func (r *Router) Handle(method, pattern string, handlers ...Middleware) {
if method == "" {
panic(Err.WithMsg("invalid method"))
}
if len(handlers) == 0 {
panic(Err.WithMsg("invalid middleware"))
}
r.trie.Define(pattern).Handle(strings.ToUpper(method), Compose(handlers...))
}
func (r *Router) Get(pattern string, handlers ...Middleware) {
r.Handle(http.MethodGet, pattern, handlers...)
}
func (r *Router) Otherwise(handlers ...Middleware) {
if len(handlers) == 0 {
panic(Err.WithMsg("invalid middleware"))
}
r.otherwise = Compose(handlers...)
}
func (r *Router) Serve(ctx *Context) error {
path := ctx.Path
method := ctx.Method
var handler Middleware
if !strings.HasPrefix(path, r.root) && path != r.rt {
return nil
}
if path == r.rt {
path = "/"
} else if l := len(r.rt); l > 0 {
path = path[l:]
}
matched := r.trie.Match(path)
if matched.Node == nil {
//FixedPathRedirect or TrailingSlashRedirect
if matched.TSR != "" || matched.FPR != "" {
ctx.Req.URL.Path = matched.TSR
if matched.FPR != "" {
ctx.Req.URL.Path = matched.FPR
}
if len(r.root) > 1 {
ctx.Req.URL.Path = r.root + ctx.Req.URL.Path[1:]
}
code := http.StatusMovedPermanently
if method != "GET" {
code = http.StatusTemporaryRedirect
}
ctx.Status(code)
return ctx.Redirect(ctx.Req.URL.String())
}
if r.otherwise == nil {
return ErrNotImplemented.WithMsgf(`"%s" is not implemented`, ctx.Path)
}
handler = r.otherwise
} else {
ok := false
if handler, ok = matched.Node.GetHandler(method).(Middleware); !ok {
// OPTIONS support
if method == http.MethodOptions {
ctx.Set(HeaderAllow, matched.Node.GetAllow())
return ctx.End(http.StatusNoContent)
}
if r.otherwise == nil {
// If no route handler is returned, it's a 405 error
ctx.Set(HeaderAllow, matched.Node.GetAllow())
return ErrMethodNotAllowed.WithMsgf(`"%s" is not allowed in "%s"`, method, ctx.Path)
}
handler = r.otherwise
}
}
ctx.SetAny(paramsKey, matched.Params)
if len(r.mds) > 0 {
handler = Compose(r.middleware, handler)
}
return handler(ctx)
}