-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter.go
69 lines (59 loc) · 1.44 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
// Copyright 2022 YBCZ, Inc. All rights reserved.
//
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file in the root of the source
// tree.
package mqms
import (
"fmt"
)
// HandlerFunc 函数体
type HandlerFunc func(*Context) (err error)
// IRouter 路由
type IRouter interface {
// Use 使用预处理函数
Use(...HandlerFunc) IRouter
// Group 路径分组
Group(string) *Route
// Item 业务函数
Item(string, HandlerFunc) *Route
}
type Route struct {
functions []HandlerFunc
basePath string
engine *Engine
}
// Use 使用预处理函数
func (r *Route) Use(handlers ...HandlerFunc) IRouter {
r.functions = handlers
return r
}
// Group 路径分组
func (r *Route) Group(name string) *Route {
return &Route{
functions: r.functions,
basePath: r.combineRoute(name),
engine: r.engine,
}
}
// Item 业务函数
func (r *Route) Item(name string, handlerFunc HandlerFunc) *Route {
uri := r.combineRoute(name)
r.engine.routes[uri] = append(r.functions, handlerFunc)
r.engine.handler.Log(fmt.Sprintf("[MQMS] %-25s --> %s (%d functions)\n", uri, nameOfFunction(handlerFunc), len(r.engine.routes[uri])))
return &Route{
functions: r.functions,
basePath: uri,
engine: r.engine,
}
}
func (r *Route) combineRoute(name string) string {
if r.basePath == "" {
return name
} else if name == "" {
return r.basePath
} else {
return r.basePath + "." + name
}
}
var _ IRouter = &Route{}