-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
116 lines (96 loc) · 2.8 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
package gee
import (
"fmt"
"log/slog"
"net/http"
"path"
)
// Router defines all router handle interface.
type Router interface {
Use(middlewares ...Middleware)
Any(pattern string, h http.HandlerFunc)
Get(pattern string, h http.HandlerFunc)
Post(pattern string, h http.HandlerFunc)
Delete(pattern string, h http.HandlerFunc)
Patch(pattern string, h http.HandlerFunc)
Put(pattern string, h http.HandlerFunc)
Options(pattern string, h http.HandlerFunc)
Head(pattern string, h http.HandlerFunc)
Group(pattern string) Router
Handle(pattern string, h http.Handler)
ServeHTTP(w http.ResponseWriter, req *http.Request)
}
var _ Router = (*Route)(nil)
type Route struct {
mux *http.ServeMux
middlewares []Middleware
groupPath string
handler http.Handler
notFoundHandler http.Handler
}
func NewRouter() *Route {
return &Route{
mux: http.NewServeMux(),
}
}
func (r *Route) Handle(pattern string, h http.Handler) {
r.handler = chain(r.mux, r.middlewares)
r.mux.Handle(pattern, h)
slog.Debug(fmt.Sprintf("[Gee-debug] %-25s --> %s", pattern, nameOfFunction(h)))
}
func (r *Route) Use(h ...Middleware) {
r.middlewares = append(r.middlewares, h...)
}
func (r *Route) Any(pattern string, h http.HandlerFunc) {
r.Get(pattern, h)
r.Post(pattern, h)
r.Delete(pattern, h)
r.Patch(pattern, h)
r.Put(pattern, h)
r.Options(pattern, h)
r.Head(pattern, h)
}
func (r *Route) Get(pattern string, h http.HandlerFunc) {
r.Handle(r.getPattern("GET", pattern), h)
}
func (r *Route) Post(pattern string, h http.HandlerFunc) {
r.Handle(r.getPattern("POST", pattern), h)
}
func (r *Route) Delete(pattern string, h http.HandlerFunc) {
r.Handle(r.getPattern("DELETE", pattern), h)
}
func (r *Route) Patch(pattern string, h http.HandlerFunc) {
r.Handle(r.getPattern("PATCH", pattern), h)
}
func (r *Route) Put(pattern string, h http.HandlerFunc) {
r.mux.Handle(r.getPattern("PUT", pattern), h)
}
func (r *Route) Options(pattern string, h http.HandlerFunc) {
r.mux.Handle(r.getPattern("OPTIONS", pattern), h)
}
func (r *Route) Head(pattern string, h http.HandlerFunc) {
r.Handle(r.getPattern("HEAD", pattern), h)
}
func (r *Route) Group(pattern string) Router {
return &Route{mux: r.mux, middlewares: r.middlewares, groupPath: pattern, handler: r.handler, notFoundHandler: r.notFoundHandler}
}
func (r *Route) NotFound(h http.Handler) {
r.notFoundHandler = h
}
func (r *Route) getPattern(method string, pattern string) string {
if r.groupPath == "" {
return method + " " + pattern
}
return method + " " + path.Join(r.groupPath, pattern)
}
func (r *Route) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if r.handler == nil {
if r.notFoundHandler != nil {
r.notFoundHandler.ServeHTTP(w, req)
return
}
http.NotFound(w, req)
return
}
r.handler.ServeHTTP(NewWrapResponseWriter(w), req)
}