-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
83 lines (73 loc) · 1.97 KB
/
engine.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
package gweb
import (
"log"
"net/http"
"strings"
)
type HandleFunc func(*Context)
func defaultHandler(g *Context) {
g.String(http.StatusOK, "web service is running.")
}
// Engine 框架核心
type Engine struct {
*RouterGroup
router *router
groups []*RouterGroup
}
func NewDefault() *Engine {
engine := &Engine{
router: newRouter(),
}
engine.RouterGroup = &RouterGroup{engine: engine}
engine.groups = []*RouterGroup{engine.RouterGroup}
engine.Use(recovery())
return engine
}
// Run 启动运行服务
func (e *Engine) Run(addr string) error {
return http.ListenAndServe(addr, e)
}
// ServeHTTP 实现Handler接口
func (e *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var middleware []HandleFunc
// 找到所有需要应用的中间件
for _, group := range e.groups {
if strings.HasPrefix(req.URL.Path, group.prefix) {
middleware = append(middleware, group.middlewares...)
}
}
c := newContext(w, req)
// 需要应用的中间件放到请求的handlers里面待执行
c.handlers = middleware
e.handle(c)
//fmt.Fprintf(w, "Hello, %q", html.EscapeString(req.URL.Path))
}
// handle 匹配路由,执行handler
func (e *Engine) handle(g *Context) {
log.Println(g.Path)
n, params := e.router.Match(g.Method, g.Path)
if n == nil && g.Path == "/" {
// 默认路由
g.handlers = append(g.handlers, defaultHandler)
} else if n == nil {
// 404
g.handlers = append(g.handlers, func(c *Context) {
c.String(http.StatusNotFound, "404 NOT FOUND")
})
} else {
g.Params = params
key := g.Method + "-" + n.pattern
// 路由handler,放到待执行列表最后
g.handlers = append(g.handlers, e.router.handlers[key])
}
// 开始执行中间件
g.Next()
}
// AddRoute 添加路由记录
func (e *Engine) AddRoute(method, pattern string, handler HandleFunc) {
e.router.AddRoute(method, pattern, handler)
}
// GET 添加GET请求路由
func (e *Engine) GET(pattern string, handler HandleFunc) {
e.router.AddRoute(http.MethodGet, pattern, handler)
}