-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrout.go
159 lines (132 loc) · 3.21 KB
/
trout.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
package trout
import (
"context"
"net/http"
"strings"
"sync"
)
type Router struct {
node *node
finalized bool
paramsPool sync.Pool
NotFound http.Handler
MethodNotAllowed http.Handler
PanicHandler func(http.ResponseWriter, *http.Request, interface{})
}
type Param struct {
Key string
Value string
}
type Params []*Param
func (ps Params) ByName(name string) string {
for i := range ps {
if ps[i].Key == name {
return ps[i].Value
}
}
return ""
}
type paramsKey struct{}
var ParamsKey = paramsKey{}
func ParamsFromContext(ctx context.Context) Params {
p, _ := ctx.Value(ParamsKey).(Params)
return p
}
func New() *Router {
m := &Router{
node: newNode(""),
}
m.paramsPool.New = func() interface{} {
return &Params{}
}
return m
}
func (r *Router) GET(path string, h http.Handler) {
r.node.getPathNode(path).addMethod(http.MethodGet, h)
}
func (r *Router) HEAD(path string, h http.Handler) {
r.node.getPathNode(path).addMethod(http.MethodHead, h)
}
func (r *Router) OPTIONS(path string, h http.Handler) {
r.node.getPathNode(path).addMethod(http.MethodOptions, h)
}
func (r *Router) POST(path string, h http.Handler) {
r.node.getPathNode(path).addMethod(http.MethodPost, h)
}
func (r *Router) PUT(path string, h http.Handler) {
r.node.getPathNode(path).addMethod(http.MethodPut, h)
}
func (r *Router) PATCH(path string, h http.Handler) {
r.node.getPathNode(path).addMethod(http.MethodPatch, h)
}
func (r *Router) DELETE(path string, h http.Handler) {
r.node.getPathNode(path).addMethod(http.MethodDelete, h)
}
func (r *Router) Handle(method string, path string, h http.Handler) {
r.node.getPathNode(path).addMethod(method, h)
}
func (r *Router) Lookup(method string, path string) (h http.Handler, ps Params, found bool) {
node, pars, found := r.node.match(path, r.getParams)
if found && node != nil && node.methods != nil {
h = node.methods[strings.ToUpper(method)]
if h != nil {
ps = *pars
found = true
}
}
return
}
func (r *Router) recv(w http.ResponseWriter, req *http.Request) {
if rcv := recover(); rcv != nil {
r.PanicHandler(w, req, rcv)
}
}
func (r *Router) getParams() *Params {
ps := r.paramsPool.Get().(*Params)
*ps = (*ps)[0:0]
return ps
}
func (r *Router) putParams(ps *Params) {
if ps != nil {
r.paramsPool.Put(ps)
}
}
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if r.PanicHandler != nil {
defer r.recv(w, req)
}
if !r.finalized {
r.node.finalize()
r.finalized = true
}
node, ps, found := r.node.match(req.URL.Path, r.getParams)
defer r.putParams(ps)
if found && node != nil && node.methods != nil {
m := node.methods[strings.ToUpper(req.Method)]
if m != nil {
if node.paramsCount > 0 {
ctx := req.Context()
ctx = context.WithValue(ctx, ParamsKey, *ps)
req = req.WithContext(ctx)
}
m.ServeHTTP(w, req)
} else {
w.Header().Set("Allow", strings.Join(node.allowed, ", "))
if r.MethodNotAllowed != nil {
r.MethodNotAllowed.ServeHTTP(w, req)
} else {
http.Error(w,
http.StatusText(http.StatusMethodNotAllowed),
http.StatusMethodNotAllowed,
)
}
return
}
} else {
if r.NotFound != nil {
r.NotFound.ServeHTTP(w, req)
} else {
http.NotFound(w, req)
}
}
}