-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrouter.go
178 lines (148 loc) · 3.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package pulse
import (
"github.com/gopulse/pulse/constants"
"net/http"
"strings"
"time"
)
type Handler func(ctx *Context) error
type HandlerFunc func(w http.ResponseWriter, r *http.Request) error
type Router struct {
routes map[string][]*Route
notFoundHandler Handler
middlewares map[string][]Middleware
}
type Static struct {
Root string
Compress bool
ByteRange bool
IndexName string
CacheDuration time.Duration
}
func NewRouter() *Router {
router := &Router{
routes: make(map[string][]*Route),
middlewares: make(map[string][]Middleware),
}
router.notFoundHandler = func(ctx *Context) error {
http.NotFound(ctx.ResponseWriter, ctx.Request)
return nil
}
return router
}
func (r *Router) Add(method, path string, handlers ...Handler) {
route := &Route{
Path: path,
Handlers: handlers,
}
parts := strings.Split(path, "/")
for _, part := range parts {
if strings.HasPrefix(part, constants.ParamSign) {
route.ParamNames = append(route.ParamNames, strings.TrimPrefix(part, constants.ParamSign))
}
}
route.Path = strings.Join(parts, "/")
r.routes[method] = append(r.routes[method], route)
}
func (r *Router) Find(method, path string) []Handler {
routes, ok := r.routes[method]
if !ok {
return nil
}
for _, route := range routes {
if matches, params := route.match(path); matches {
c := NewContext(nil, nil)
c.Params = params
return r.applyMiddleware(route.Handlers, method)
}
}
return nil
}
func (r *Router) applyMiddleware(handlers []Handler, method string) []Handler {
for i := len(r.middlewares[method]) - 1; i >= 0; i-- {
middleware := r.middlewares[method][i]
for j := len(handlers) - 1; j >= 0; j-- {
handler := handlers[j]
handlers[j] = func(ctx *Context) error {
return middleware.Handle(ctx, handler)
}
}
}
return handlers
}
func RouterHandler(router *Router) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
path := req.URL.Path
method := req.Method
handlers := router.Find(method, path)
c := NewContext(w, req)
for _, h := range handlers {
err := h(c)
if err != nil {
break
}
}
}
}
func (r *Route) match(path string) (bool, map[string]string) {
parts := strings.Split(path, "/")
routeParts := strings.Split(r.Path, "/")
if strings.HasSuffix(path, "/") {
parts = parts[:len(parts)-1]
}
if len(parts) != len(routeParts) {
return false, nil
}
params := make(map[string]string)
for i, part := range routeParts {
if strings.HasPrefix(part, constants.ParamSign) {
paramName := strings.TrimPrefix(part, constants.ParamSign)
params[paramName] = parts[i]
} else if part == constants.WildcardSign {
return true, params
} else if part != parts[i] {
return false, nil
}
}
return true, params
}
func (options *Static) notFoundHandler(w http.ResponseWriter) {
w.WriteHeader(http.StatusNotFound)
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
_, err := w.Write([]byte("404 Not Found"))
if err != nil {
return
}
}
func (options *Static) PathRewrite(r *http.Request) []byte {
path := r.URL.Path
if len(path) > 1 && path[len(path)-1] == '/' {
path = path[:len(path)-1]
}
// Remove the last part of the path
parts := strings.Split(path, "/")
if len(parts) > 1 {
parts = parts[:len(parts)-1]
}
path = strings.Join(parts, "/")
if options.IndexName != "" {
// Append the index file name to the path
path += "/"
path += options.IndexName
}
return []byte(path)
}
func (r *Router) Static(prefix, root string, options *Static) {
if options == nil {
options = &Static{}
}
if options.Root == "" {
options.Root = root
}
fs := http.FileServer(http.Dir(options.Root))
handler := http.StripPrefix(prefix, fs)
r.Get(prefix, func(ctx *Context) error {
handler.ServeHTTP(ctx.ResponseWriter, ctx.Request)
return nil
})
}