-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmux.go
147 lines (107 loc) · 3.13 KB
/
mux.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
package track
import (
"fmt"
"net/http"
"strings"
)
//Get adds HTTP GET method and the router path for the handler
func (r *Router) Get(path string, h http.HandlerFunc) {
r.handle(path, http.MethodGet, h)
}
//Post adds HTTP POST method and the router path for the handler
func (r *Router) Post(path string, h http.HandlerFunc) {
r.handle(path, http.MethodPost, h)
}
//Delete adds HTTP DELETE method and the router path for the handler
func (r *Router) Delete(path string, h http.HandlerFunc) {
r.handle(path, http.MethodDelete, h)
}
//Patch adds HTTP Patch method and the router path for the handler
func (r *Router) Patch(path string, h http.HandlerFunc) {
r.handle(path, http.MethodPatch, h)
}
//Put adds HTTP PUT method and the router path for the handler
func (r *Router) Put(path string, h http.HandlerFunc) {
r.handle(path, http.MethodPut, h)
}
//Head adds HTTP HEAD method and the router path for the handler
func (r *Router) Head(path string, h http.HandlerFunc) {
r.handle(path, http.MethodHead, h)
}
//Options adds HTTP OPTIONS method and the router path for the handler
func (r *Router) Options(path string, h http.HandlerFunc) {
r.handle(path, http.MethodOptions, h)
}
//Trace adds HTTP TRACE method and the router path for the handler
func (r *Router) Trace(path string, h http.HandlerFunc) {
r.handle(path, http.MethodTrace, h)
}
//Connect adds HTTP CONNECT method and the router path for the handler
func (r *Router) Connect(path string, h http.HandlerFunc) {
r.handle(path, http.MethodConnect, h)
}
func (r *Router) handle(path, method string, h http.Handler) {
if len(path) < 1 {
panic("router path is empty")
}
if path[0] != '/' {
panic("route path must start with /")
}
var pathSplitted []string
if strings.HasPrefix(path, "/") {
pathSplitted = strings.Split(path[1:], "/")
} else {
pathSplitted = strings.Split(path, "/")
}
if len(pathSplitted) > 0 {
for _, v := range r.middlewares {
h = v(h.ServeHTTP)
}
r.add(pathSplitted, method, h)
}
}
func exampleMiddleware(next http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("befor")
next.ServeHTTP(w, r)
fmt.Println("after")
})
}
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
reqPath := req.URL.Path
if strings.HasSuffix(reqPath, "/") {
reqPath = strings.TrimSuffix(reqPath, "/")
}
reqParts := strings.Split(reqPath, "/")[1:]
router := r.search(reqParts)
if router == nil {
http.NotFound(w, req)
return
}
// if router.Method != req.Method {
// methodNotAllowedHandler(w, req)
// return
// }
for k, v := range router.Method {
if v == req.Method {
break
}
if k == len(router.Method)-1 && v != req.Method {
methodNotAllowedHandler(w, req)
return
}
}
// var ctx context.Context
// if len(router.Pram) > 0 {
// for k, v := range router.Pram {
// ctx = context.WithValue(req.Context(), k[1:], v)
// req = req.WithContext(ctx)
// }
// }
router.Value.ServeHTTP(w, req)
return
}
func methodNotAllowedHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write(nil)
}