/
router.go
188 lines (168 loc) · 4.07 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
179
180
181
182
183
184
185
186
187
188
package axe
import (
"html/template"
"net/http"
"sort"
"time"
"github.com/go-playground/form"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"github.com/unrolled/render"
validator "gopkg.in/go-playground/validator.v9"
)
// NewRouter create router
func NewRouter() *Router {
return &Router{
routes: make([]route, 0),
handlers: make([]HandlerFunc, 0),
funcs: make(template.FuncMap),
statics: make(map[string]string),
layouts: make(map[string]LayoutFunc),
}
}
// Router http router
type Router struct {
path string
routes []route
handlers []HandlerFunc
funcs template.FuncMap
statics map[string]string
layouts map[string]LayoutFunc
}
// Use use handlers
func (p *Router) Use(handlers ...HandlerFunc) {
p.handlers = append(p.handlers, handlers...)
}
// FuncMap set html template funcmap
func (p *Router) FuncMap(n string, f interface{}) {
p.funcs[n] = f
}
// Static static files
func (p *Router) Static(path, dir string) {
p.statics[path] = dir
}
// GET http get
func (p *Router) GET(path string, handlers ...HandlerFunc) {
p.add(http.MethodGet, path, handlers...)
}
// POST http post
func (p *Router) POST(path string, handlers ...HandlerFunc) {
p.add(http.MethodPost, path, handlers...)
}
// PUT http put
func (p *Router) PUT(path string, handlers ...HandlerFunc) {
p.add(http.MethodPut, path, handlers...)
}
// PATCH http patch
func (p *Router) PATCH(path string, handlers ...HandlerFunc) {
p.add(http.MethodPatch, path, handlers...)
}
// DELETE http delete
func (p *Router) DELETE(path string, handlers ...HandlerFunc) {
p.add(http.MethodDelete, path, handlers...)
}
func (p *Router) add(method, path string, handlers ...HandlerFunc) {
p.routes = append(
p.routes,
route{
method: method,
path: path,
handlers: append(p.handlers, handlers...),
},
)
}
// Resources resources
func (p *Router) Resources(path string, index, create, show, update, destroy []HandlerFunc) {
if index != nil {
p.GET(path, index...)
}
if create != nil {
p.POST(path, create...)
}
path += "/{id}"
if show != nil {
p.GET(path, show...)
}
if update != nil {
p.POST(path, update...)
}
if destroy != nil {
p.DELETE(path, destroy...)
}
}
// Group sub-router
func (p *Router) Group(path string, router *Router) {
for _, rt := range router.routes {
p.add(
rt.method,
path+rt.path,
rt.handlers...,
)
}
for k, v := range router.statics {
p.statics[k] = v
}
for k, v := range router.layouts {
p.layouts[k] = v
}
for k, v := range router.funcs {
p.funcs[k] = v
}
}
// Walk walk routes
func (p *Router) Walk(f func(method, path string, handlers ...HandlerFunc) error) error {
sort.Sort(routes(p.routes))
for _, r := range p.routes {
if e := f(r.method, r.path, r.handlers...); e != nil {
return e
}
}
return nil
}
// Handler http handle
func (p *Router) Handler(views string, debug bool) http.Handler {
rdr := render.New(render.Options{
Directory: views,
Extensions: []string{".html"},
Funcs: []template.FuncMap{},
IndentJSON: debug,
IndentXML: debug,
IsDevelopment: debug,
})
rut := mux.NewRouter()
dec := form.NewDecoder()
vat := validator.New()
for k, v := range p.statics {
log.Debugf("GET %s => %s", k, v)
rut.PathPrefix(k).Handler(http.StripPrefix(k, http.FileServer(http.Dir(v)))).Methods(http.MethodGet)
}
for _, rt := range p.routes {
r := route{
method: rt.method,
path: rt.path,
handlers: append([]HandlerFunc{}, rt.handlers...),
}
log.Debugf("%s %s [%d]", r.method, r.path, len(r.handlers))
rut.HandleFunc(r.path, func(wrt http.ResponseWriter, req *http.Request) {
now := time.Now()
log.Infof("%s %s %s", req.Proto, req.Method, req.URL)
log.Debugf("%s %s", r.method, r.path)
log.Debug(req.Header)
ctx := Context{
Request: req,
Writer: wrt,
Payload: make(H),
Params: mux.Vars(req),
index: 0,
handlers: r.handlers,
decoder: dec,
validate: vat,
render: rdr,
}
log.Debug(ctx.Params)
ctx.Next()
log.Infof("%d %s", ctx.code, time.Now().Sub(now))
}).Methods(r.method)
}
return rut
}