-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
150 lines (128 loc) · 3.6 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
package r2router
import (
//"log"
"net/http"
"strings"
//"time"
)
const (
HTTP_METHOD_GET = "GET"
HTTP_METHOD_POST = "POST"
HTTP_METHOD_DELETE = "DELETE"
HTTP_METHOD_OPTIONS = "OPTIONS"
HTTP_METHOD_HEAD = "HEAD"
HTTP_METHOD_PUT = "PUT"
HTTP_METHOD_PATCH = "PATCH"
)
type Handler interface {
ServeHTTP(w http.ResponseWriter, req *http.Request, params Params)
}
// HandlerFunc define interface handler
type HandlerFunc func(w http.ResponseWriter, req *http.Request, params Params)
func (h HandlerFunc) ServeHTTP(w http.ResponseWriter, req *http.Request, params Params) {
h(w, req, params)
}
type Router struct {
roots map[string]*rootNode
HandleMethodNotAllowed bool
MethodNotAllowed http.HandlerFunc
NotFound http.HandlerFunc
}
// NewRouter return a new Router
func NewRouter() *Router {
r := &Router{}
r.roots = make(map[string]*rootNode)
r.HandleMethodNotAllowed = true
return r
}
// http Handler Interface
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
//now := time.Now()
if root, exist := r.roots[req.Method]; exist {
handler, params, _ := root.match(req.URL.Path)
if handler != nil {
handler.ServeHTTP(w, req, params)
//log.Println(time.Now().Sub(now))
return
}
}
r.handleMissing(w, req)
}
func (r *Router) handleMissing(w http.ResponseWriter, req *http.Request) {
// if options find handler for different method
if req.Method == HTTP_METHOD_OPTIONS {
// build and serve options
availableMethods := make([]string, 0, len(r.roots))
for method := range r.roots {
if root, exist := r.roots[method]; exist {
handler, _, _ := root.match(req.URL.Path)
if handler != nil {
availableMethods = append(availableMethods, method)
}
}
}
if len(availableMethods) > 0 {
w.Header().Add("Allow", strings.Join(availableMethods, ", "))
w.WriteHeader(http.StatusOK)
return
}
}
if r.HandleMethodNotAllowed {
for method := range r.roots {
if root, exist := r.roots[method]; exist {
handler, _, _ := root.match(req.URL.Path)
if handler != nil {
if r.MethodNotAllowed != nil {
r.MethodNotAllowed(w, req)
} else {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
}
return
}
}
}
}
if r.NotFound != nil {
r.NotFound(w, req)
} else {
http.NotFound(w, req)
}
}
func (r *Router) Get(path string, handler HandlerFunc) {
r.AddHandler(HTTP_METHOD_GET, path, handler)
}
func (r *Router) Head(path string, handler HandlerFunc) {
r.AddHandler(HTTP_METHOD_HEAD, path, handler)
}
func (r *Router) Post(path string, handler HandlerFunc) {
r.AddHandler(HTTP_METHOD_POST, path, handler)
}
func (r *Router) Put(path string, handler HandlerFunc) {
r.AddHandler(HTTP_METHOD_PUT, path, handler)
}
func (r *Router) Delete(path string, handler HandlerFunc) {
r.AddHandler(HTTP_METHOD_DELETE, path, handler)
}
func (r *Router) Patch(path string, handler HandlerFunc) {
r.AddHandler(HTTP_METHOD_PATCH, path, handler)
}
func (r *Router) AddHandler(method, path string, handler HandlerFunc) {
if _, exists := r.roots[method]; !exists {
r.roots[method] = newRouteTree()
}
r.roots[method].addRoute(path, HandlerFunc(handler))
}
// Group takes a path which typically a prefix for an endpoint
// It will call callback function with a group router which
// you can add handler for different request methods
func (r *Router) Group(path string, fn func(r *GroupRouter)) {
gr := NewGroupRouter(r, path)
fn(gr)
}
func (r *Router) Dump() string {
s := ""
for method, root := range r.roots {
s += method + "\n" +root.dump()
}
return s
}