-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
router.go
163 lines (121 loc) · 5.42 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
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
package gorouter
import (
"net/http"
"github.com/valyala/fasthttp"
)
// MiddlewareFunc is a http middleware function type
type MiddlewareFunc func(http.Handler) http.Handler
// FastHTTPMiddlewareFunc is a fasthttp middleware function type
type FastHTTPMiddlewareFunc func(fasthttp.RequestHandler) fasthttp.RequestHandler
// Router is a micro framework, HTTP request router, multiplexer, mux
type Router interface {
// PrettyPrint prints the tree text representation to console
PrettyPrint() string
// POST adds http.Handler as router handler
// under POST method and given patter
POST(pattern string, handler http.Handler)
// GET adds http.Handler as router handler
// under GET method and given patter
GET(pattern string, handler http.Handler)
// PUT adds http.Handler as router handler
// under PUT method and given patter
PUT(pattern string, handler http.Handler)
// DELETE adds http.Handler as router handler
// under DELETE method and given patter
DELETE(pattern string, handler http.Handler)
// PATCH adds http.Handler as router handler
// under PATCH method and given patter
PATCH(pattern string, handler http.Handler)
// OPTIONS adds http.Handler as router handler
// under OPTIONS method and given patter
OPTIONS(pattern string, handler http.Handler)
// HEAD adds http.Handler as router handler
// under HEAD method and given patter
HEAD(pattern string, handler http.Handler)
// CONNECT adds http.Handler as router handler
// under CONNECT method and given patter
CONNECT(pattern string, handler http.Handler)
// TRACE adds http.Handler as router handler
// under TRACE method and given patter
TRACE(pattern string, handler http.Handler)
// USE adds middleware functions ([]MiddlewareFunc)
// to whole router branch under given method and patter
USE(method, pattern string, fs ...MiddlewareFunc)
// USEANY adds middleware functions ([]MiddlewareFunc)
// to whole router branch for all methods and patter
USEANY(pattern string, fs ...MiddlewareFunc)
// Handle adds http.Handler as router handler
// under given method and patter
Handle(method, pattern string, handler http.Handler)
// Mount another handler as a subrouter
Mount(pattern string, handler http.Handler)
// Compile optimizes Tree nodes reducing static nodes depth when possible
Compile()
// ServeHTTP dispatches the request to the route handler
// whose pattern matches the request URL
ServeHTTP(http.ResponseWriter, *http.Request)
// ServeFiles replies to the request with the
// contents of the named file or directory.
ServeFiles(fs http.FileSystem, root string, strip bool)
// NotFound replies to the request with the 404 Error code
NotFound(http.Handler)
// NotAllowed replies to the request with the 405 Error code
NotAllowed(http.Handler)
}
// FastHTTPRouter is a fasthttp micro framework, HTTP request router, multiplexer, mux
type FastHTTPRouter interface {
// PrettyPrint prints the tree text representation to console
PrettyPrint() string
// POST adds fasthttp.RequestHandler as router handler
// under POST method and given patter
POST(pattern string, handler fasthttp.RequestHandler)
// GET adds fasthttp.RequestHandler as router handler
// under GET method and given patter
GET(pattern string, handler fasthttp.RequestHandler)
// PUT adds fasthttp.RequestHandler as router handler
// under PUT method and given patter
PUT(pattern string, handler fasthttp.RequestHandler)
// DELETE adds fasthttp.RequestHandler as router handler
// under DELETE method and given patter
DELETE(pattern string, handler fasthttp.RequestHandler)
// PATCH adds fasthttp.RequestHandler as router handler
// under PATCH method and given patter
PATCH(pattern string, handler fasthttp.RequestHandler)
// OPTIONS adds fasthttp.RequestHandler as router handler
// under OPTIONS method and given patter
OPTIONS(pattern string, handler fasthttp.RequestHandler)
// HEAD adds fasthttp.RequestHandler as router handler
// under HEAD method and given patter
HEAD(pattern string, handler fasthttp.RequestHandler)
// CONNECT adds fasthttp.RequestHandler as router handler
// under CONNECT method and given patter
CONNECT(pattern string, handler fasthttp.RequestHandler)
// TRACE adds fasthttp.RequestHandler as router handler
// under TRACE method and given patter
TRACE(pattern string, handler fasthttp.RequestHandler)
// USE adds middleware functions ([]MiddlewareFunc)
// to whole router branch under given method and patter
USE(method, pattern string, fs ...FastHTTPMiddlewareFunc)
// USEANY adds middleware functions ([]MiddlewareFunc)
// to whole router branch for all methods and patter
USEANY(pattern string, fs ...FastHTTPMiddlewareFunc)
// Handle adds fasthttp.RequestHandler as router handler
// under given method and patter
Handle(method, pattern string, handler fasthttp.RequestHandler)
// Mount another handler as a subrouter
Mount(pattern string, handler fasthttp.RequestHandler)
// Compile optimizes Tree nodes reducing static nodes depth when possible
Compile()
// HandleFastHTTP dispatches the request to the route handler
// whose pattern matches the request URL
HandleFastHTTP(ctx *fasthttp.RequestCtx)
// ServeFile replies to the request with the
// contents of the named file or directory.
ServeFiles(root string, stripSlashes int)
// NotFound replies to the request with the
// 404 Error code
NotFound(fasthttp.RequestHandler)
// NotFound replies to the request with the
// 405 Error code
NotAllowed(fasthttp.RequestHandler)
}