-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroute.go
94 lines (78 loc) · 1.68 KB
/
route.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
package flute
import (
"github.com/gorilla/mux"
"net/http"
)
// 内部路由
type Router struct {
*mux.Router
}
func NewRouter() *Router {
r := Router{mux.NewRouter()}
return &r
}
func (r *Router) Start(addr string) error {
http.Handle("/", r)
return http.ListenAndServe(addr, nil)
}
func (r *Router) AddFunc(path string, method string, f func(c *Context)) {
r.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
context := Context{w, r}
f(&context)
}).Methods(method)
}
// 内部的restful路由实现, 还有中间件的调用, 总是感觉不优雅, 如果你有更好的思路,请告诉我,谢谢
func (r *Router) Resources(path string, controller ControllerInterface, middlewares []MiddlewareInterface) {
path1 := "/" + path
path2 := path1 + "/{id}"
beforeFunc := func(c *Context) bool {
for _, v := range middlewares {
v.Init(c)
if v.Before() == false {
return false
}
}
return true
}
afterFunc := func(c *Context) {
for _, v := range middlewares {
v.Init(c)
v.After()
}
}
r.AddFunc(path1, "GET", func(c *Context) {
if beforeFunc(c) {
controller.Init(c)
controller.Index()
}
afterFunc(c)
})
r.AddFunc(path2, "GET", func(c *Context) {
if beforeFunc(c) {
controller.Init(c)
controller.Show()
}
afterFunc(c)
})
r.AddFunc(path1, "POST", func(c *Context) {
if beforeFunc(c) {
controller.Init(c)
controller.Create()
}
afterFunc(c)
})
r.AddFunc(path2, "PUT", func(c *Context) {
if beforeFunc(c) {
controller.Init(c)
controller.Update()
}
afterFunc(c)
})
r.AddFunc(path2, "DELETE", func(c *Context) {
if beforeFunc(c) {
controller.Init(c)
controller.Delete()
}
afterFunc(c)
})
}