This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_lambda.go
92 lines (73 loc) · 2.64 KB
/
router_lambda.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
package golam
import (
"net/http"
)
func newLambdaRouter() Router {
return &lambdaRouter{
routeTable: make(map[string]*route),
}
}
var _ Router = (*lambdaRouter)(nil)
type lambdaRouter struct {
routeTable map[string]*route
}
func (lr *lambdaRouter) FindRoute(path string) *route {
return lr.routeTable[path]
}
func (lr *lambdaRouter) AddRoute(method string, path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
r := lr.routeTable[path]
if r == nil {
r = &route{
path: path,
}
lr.routeTable[path] = r
}
method = replaceMethodWildcardToBlank(method)
r.params.setParamsInfo(method, findParamKeys(path))
r.handlers.setHandler(method, func(c Context) error {
wrappedHandler := wrapMiddleware(handler, middleware...)
return wrappedHandler(c)
})
}
func (lr *lambdaRouter) DelRoute(method string, path string) {
r := lr.routeTable[path]
if r == nil {
return
}
method = replaceMethodWildcardToBlank(method)
r.handlers.delHandler(method)
r.params.delParamsInfo(method)
if r.handlers.countHandler() == 0 && r.params.countParamsInfo() == 0 {
delete(lr.routeTable, path)
}
}
func (lr *lambdaRouter) Any(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
lr.AddRoute("", path, handler, middleware...)
}
func (lr *lambdaRouter) GET(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
lr.AddRoute(http.MethodGet, path, handler, middleware...)
}
func (lr *lambdaRouter) HEAD(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
lr.AddRoute(http.MethodHead, path, handler, middleware...)
}
func (lr *lambdaRouter) POST(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
lr.AddRoute(http.MethodPost, path, handler, middleware...)
}
func (lr *lambdaRouter) PUT(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
lr.AddRoute(http.MethodPut, path, handler, middleware...)
}
func (lr *lambdaRouter) PATCH(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
lr.AddRoute(http.MethodPatch, path, handler, middleware...)
}
func (lr *lambdaRouter) DELETE(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
lr.AddRoute(http.MethodDelete, path, handler, middleware...)
}
func (lr *lambdaRouter) CONNECT(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
lr.AddRoute(http.MethodConnect, path, handler, middleware...)
}
func (lr *lambdaRouter) OPTIONS(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
lr.AddRoute(http.MethodOptions, path, handler, middleware...)
}
func (lr *lambdaRouter) TRACE(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
lr.AddRoute(http.MethodTrace, path, handler, middleware...)
}