-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
164 lines (138 loc) · 3.63 KB
/
route.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
package tgin
import (
"net/http"
)
var (
_ http.Handler = (*RouteGroup)(nil)
)
type RouteHandler func(c *Context)
type handlerFunctions map[string]RouteHandler
type RouteGroup struct {
prefix string
handlers map[string]handlerFunctions
mux *http.ServeMux
middlewares *MiddlewareTree
}
func NewRouteGroup() *RouteGroup {
return &RouteGroup{
prefix: "",
mux: http.NewServeMux(),
middlewares: newMiddlewareTree(),
handlers: map[string]handlerFunctions{},
}
}
func (rg *RouteGroup) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ww := &ResponseWriterWrapper{
ResponseWriter: w,
code: 200,
}
if hj, ok := w.(http.Hijacker); ok {
ww.Hijacker = hj
}
ctx := newContext(ww, r)
ww.ctx = ctx
ctx.mux = rg.mux
ctx.middlewares = rg.middlewares.BuildMiddlewares(r.URL.Path)
ctx.Next()
if !ctx.aborted && !ctx.served {
rg.mux.ServeHTTP(ww, r)
}
}
func (rg *RouteGroup) getPath(path string) string {
return rg.prefix + path
}
func (rg *RouteGroup) getContext(w http.ResponseWriter, r *http.Request) *Context {
if rww, ok := w.(*ResponseWriterWrapper); ok {
if rww.ctx != nil {
return rww.ctx
}
}
return newContext(w, r)
}
func (rg *RouteGroup) handle(method, path string, handler RouteHandler) {
fullPath := rg.getPath(path)
hfs, have := rg.handlers[fullPath]
if have {
hfs[method] = handler
} else {
nhfs := handlerFunctions{}
nhfs[method] = handler
rg.handlers[fullPath] = nhfs
rg.mux.HandleFunc(fullPath, func(w http.ResponseWriter, r *http.Request) {
ctx := rg.getContext(w, r)
lhfs, have := rg.handlers[fullPath]
if !have {
ctx.Text(404, "404 page not found\n")
return
}
hdl, have := lhfs[ctx.Method]
if !have {
ctx.Text(404, "404 page not found\n")
return
}
hdl(ctx)
})
}
}
func (rg *RouteGroup) Group(prefix string) *RouteGroup {
return &RouteGroup{
prefix: rg.getPath(prefix),
mux: rg.mux,
middlewares: rg.middlewares,
handlers: map[string]handlerFunctions{},
}
}
func (rg *RouteGroup) Any(path string, handler RouteHandler) {
rg.mux.HandleFunc(rg.getPath(path), func(w http.ResponseWriter, r *http.Request) {
ctx := rg.getContext(w, r)
handler(ctx)
})
}
func (rg *RouteGroup) Get(path string, handler RouteHandler) {
rg.handle("GET", path, handler)
}
func (rg *RouteGroup) GET(path string, handler RouteHandler) {
rg.Get(path, handler)
}
func (rg *RouteGroup) Post(path string, handler RouteHandler) {
rg.handle("POST", path, handler)
}
func (rg *RouteGroup) POST(path string, handler RouteHandler) {
rg.Post(path, handler)
}
func (rg *RouteGroup) Put(path string, handler RouteHandler) {
rg.handle("PUT", path, handler)
}
func (rg *RouteGroup) PUT(path string, handler RouteHandler) {
rg.Put(path, handler)
}
func (rg *RouteGroup) Delete(path string, handler RouteHandler) {
rg.handle("DELETE", path, handler)
}
func (rg *RouteGroup) DELETE(path string, handler RouteHandler) {
rg.Delete(path, handler)
}
func (rg *RouteGroup) Head(path string, handler RouteHandler) {
rg.handle("HEAD", path, handler)
}
func (rg *RouteGroup) HEAD(path string, handler RouteHandler) {
rg.Head(path, handler)
}
func (rg *RouteGroup) Options(path string, handler RouteHandler) {
rg.handle("OPTIONS", path, handler)
}
func (rg *RouteGroup) OPTIONS(path string, handler RouteHandler) {
rg.Options(path, handler)
}
func (rg *RouteGroup) StaticFile(path, filePath string) {
handler := func(c *Context) {
c.File(filePath)
}
rg.Get(path, handler)
rg.Head(path, handler)
}
func (rg *RouteGroup) Use(middlewares ...RouteHandler) {
for _, middleware := range middlewares {
rg.middlewares.Add(rg.prefix, middleware)
}
}