-
Notifications
You must be signed in to change notification settings - Fork 0
/
axisapi.go
112 lines (95 loc) · 3.2 KB
/
axisapi.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
package axisapi
/*
*Author:AxisZql
*Date: 2022-3-10
*/
import (
"log"
"net/http"
"strings"
)
// HandleFunc defines the request handler used by axisapi
type HandleFunc func(*Context)
// RouterGroup defines the router group
type RouterGroup struct {
prefix string // router prefix
middlewares []HandleFunc //support middleware
parent *RouterGroup // support nesting (支持嵌套)
engine *Engine
}
// Engine implement the interface of ServeHTTP
type Engine struct {
*RouterGroup
router *router
groups []*RouterGroup //store all groups
}
// Engine is the uni handler for all requests
// 定义ServeHTTP方法捕获所有request,本框架的核心(在go中实现接口方法的struct都可以强制转化为接口类型)
func (eg *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var middlewares []HandleFunc
for _, group := range eg.groups {
if strings.HasPrefix(req.URL.Path, group.prefix) {
middlewares = append(middlewares, group.middlewares...)
}
}
ctx := newContext(w, req)
ctx.handlers = middlewares
eg.router.handle(ctx)
}
// New is the constructor of axisapi.Engine
func New() *Engine {
engine := &Engine{router: newRouter()}
engine.RouterGroup = &RouterGroup{engine: engine}
engine.groups = []*RouterGroup{engine.RouterGroup}
return engine
}
// Default use the default logging and error handling middleware
func Default() *Engine {
engine := New()
engine.Use(Logger(), Recovery())
return engine
}
// 包内函数,添加路由
func (eg *Engine) addRoute(method string, pattern string, handler HandleFunc) {
eg.router.addRoute(method, pattern, handler)
}
// GET defines the method to add GET request
func (eg *Engine) GET(pattern string, handler HandleFunc) {
eg.addRoute("GET", pattern, handler)
}
// POST defines the method to add POST request
func (eg *Engine) POST(pattern string, handler HandleFunc) {
eg.addRoute("POST", pattern, handler)
}
// Group is defined to create a new RouterGroup
// all groups share the same Engine instance,engine can use the method
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
}
// Use the middleware(可以一次性添加多个中间件)(由于Engine嵌套RouterGroup所以非路由组也可调用Use方法来设置中间件)
func (group *RouterGroup) Use(middlewares ...HandleFunc) {
group.middlewares = append(group.middlewares, middlewares...)
}
func (group *RouterGroup) addRoute(method string, comp string, handler HandleFunc) {
pattern := group.prefix + comp
log.Printf("Router %4s - %s", method, pattern)
// 调用engine实现的addRouter方法,保证之前的路由逻辑不会受影响
group.engine.addRoute(method, pattern, handler)
}
func (group *RouterGroup) GET(pattern string, handler HandleFunc) {
group.addRoute("GET", pattern, handler)
}
func (group *RouterGroup) POST(pattern string, handler HandleFunc) {
group.addRoute("POST", pattern, handler)
}
// Run defines the method to start a http Server
func (eg *Engine) Run(addr string) error {
return http.ListenAndServe(addr, eg)
}