-
Notifications
You must be signed in to change notification settings - Fork 1
/
router.go
130 lines (106 loc) · 2.95 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
package router
import (
"context"
"net/http"
"strings"
)
type RouteRegistrar interface {
GET(uri string, handler http.HandlerFunc)
POST(uri string, handler http.HandlerFunc)
PUT(uri string, handler http.HandlerFunc)
PATCH(uri string, handler http.HandlerFunc)
DELETE(uri string, handler http.HandlerFunc)
Group(uri string) RouteRegistrar
}
type PanicHandler func(w http.ResponseWriter, r *http.Request, err any)
type Router interface {
RouteRegistrar
OnPanic(handler PanicHandler)
OnRouteNotFound(handler http.HandlerFunc)
ServeHTTP(writer http.ResponseWriter, request *http.Request)
}
type router struct {
registry map[method]Tree
panicHandler PanicHandler
routeNotFoundHandler http.HandlerFunc
}
func createRegistry() map[method]Tree {
return map[method]Tree{
GET: newTree(),
POST: newTree(),
PUT: newTree(),
PATCH: newTree(),
DELETE: newTree(),
}
}
func New() Router {
return &router{
registry: createRegistry(),
}
}
func (r *router) GET(uri string, handler http.HandlerFunc) {
r.register(GET, uri, handler)
}
func (r *router) POST(uri string, handler http.HandlerFunc) {
r.register(POST, uri, handler)
}
func (r *router) PUT(uri string, handler http.HandlerFunc) {
r.register(PUT, uri, handler)
}
func (r *router) PATCH(uri string, handler http.HandlerFunc) {
r.register(PATCH, uri, handler)
}
func (r *router) DELETE(uri string, handler http.HandlerFunc) {
r.register(DELETE, uri, handler)
}
func (r *router) Group(uri string) RouteRegistrar {
return &group{basePath: uri, routerEngine: r}
}
func (r *router) register(m method, uri string, handler http.HandlerFunc) {
uri = strings.Trim(uri, pathSeparator)
r.registry[m].add(uri, newRoute(uri, handler))
}
func (r *router) resolve(m method, uri string) (*route, error) {
return r.registry[m].find(uri)
}
func (r *router) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
defer func() {
if e := recover(); e != nil && r.panicHandler != nil {
r.panicHandler(writer, request, e)
}
}()
uri := strings.Trim(request.RequestURI, pathSeparator)
m := stringToMethod(request.Method)
route, err := r.resolve(m, uri)
if err != nil {
r.handleError(writer, request, err)
return
}
pathVariables := route.getPathVariablesMap()
ctx := request.Context()
ctx = context.WithValue(ctx, pathVariablesContextKey, pathVariables)
request = request.WithContext(ctx)
route.handler(writer, request)
}
func (r *router) handleError(writer http.ResponseWriter, request *http.Request, err error) {
switch err.(type) {
case *routeNotFoundError:
writer.WriteHeader(http.StatusNotFound)
if r.routeNotFoundHandler != nil {
r.routeNotFoundHandler.ServeHTTP(writer, request)
return
}
return
default:
if r.panicHandler != nil {
r.panicHandler(writer, request, err)
}
return
}
}
func (r *router) OnPanic(handler PanicHandler) {
r.panicHandler = handler
}
func (r *router) OnRouteNotFound(handler http.HandlerFunc) {
r.routeNotFoundHandler = handler
}