-
Notifications
You must be signed in to change notification settings - Fork 0
/
gee.go
115 lines (94 loc) · 2.88 KB
/
gee.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
package gee
import (
"log"
"net/http"
"strings"
)
type HandlerFunc func(ctx *Context)
type RouterGroup struct {
prefix string
middlewares []HandlerFunc
parent *RouterGroup
engine *Engine
}
type Engine struct {
*RouterGroup
router *router
groups []*RouterGroup
}
func New() *Engine {
engine := &Engine{router: newRouter()}
engine.RouterGroup = &RouterGroup{engine: engine}
engine.groups = []*RouterGroup{engine.RouterGroup}
return engine
}
func Default() *Engine {
engine := New()
engine.Use(Logger(), Recovery(), Server())
return engine
}
func (group *RouterGroup) Group(prefix string) *RouterGroup {
engine := group.engine
newGroup := &RouterGroup{
prefix: group.prefix + prefix,
parent: group,
engine: engine,
}
engine.groups = append(engine.groups, newGroup)
return newGroup
}
func (group *RouterGroup) Use(middlewares ...HandlerFunc) {
group.middlewares = append(group.middlewares, middlewares...)
}
func (group *RouterGroup) addRoute(method string, comp string, handler HandlerFunc) {
pattern := group.prefix + comp
if !IsDebug() == true {
log.Printf("[Gee] %4s - %s", method, pattern)
group.engine.router.addRoute(method, pattern, handler)
}
group.engine.router.addRoute(method, pattern, handler)
}
func (group *RouterGroup) GET(pattern string, handler HandlerFunc) {
group.addRoute(http.MethodGet, pattern, handler)
}
func (group *RouterGroup) POST(pattern string, handler HandlerFunc) {
group.addRoute(http.MethodPost, pattern, handler)
}
func (group *RouterGroup) PUT(pattern string, handler HandlerFunc) {
group.addRoute(http.MethodPut, pattern, handler)
}
func (group *RouterGroup) DELETE(pattern string, handler HandlerFunc) {
group.addRoute(http.MethodDelete, pattern, handler)
}
func (group *RouterGroup) PATCH(pattern string, handler HandlerFunc) {
group.addRoute(http.MethodPatch, pattern, handler)
}
func (group *RouterGroup) HEAD(pattern string, handler HandlerFunc) {
group.addRoute(http.MethodHead, pattern, handler)
}
func (group *RouterGroup) OPTIONS(pattern string, handler HandlerFunc) {
group.addRoute(http.MethodOptions, pattern, handler)
}
func (group *RouterGroup) Any(pattern string, handler HandlerFunc) {
group.addRoute("GET", pattern, handler)
group.addRoute("POST", pattern, handler)
group.addRoute("PUT", pattern, handler)
group.addRoute("DELETE", pattern, handler)
group.addRoute("PATCH", pattern, handler)
group.addRoute("HEAD", pattern, handler)
group.addRoute("OPTIONS", pattern, handler)
}
func (engine *Engine) Run(addr string) (err error) {
return http.ListenAndServe(addr, engine)
}
func (engine *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var middlewares []HandlerFunc
for _, group := range engine.groups {
if strings.HasPrefix(r.URL.Path, group.prefix) {
middlewares = append(middlewares, group.middlewares...)
}
}
c := NewContext(w, r)
c.handlers = middlewares
engine.router.handle(c)
}