-
Notifications
You must be signed in to change notification settings - Fork 10
/
route.go
170 lines (147 loc) · 4.63 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package route
import (
"errors"
"fmt"
"reflect"
"runtime"
"strings"
"sync"
"github.com/totoval/framework/app"
"github.com/totoval/framework/helpers/log"
"github.com/totoval/framework/helpers/toto"
"github.com/totoval/framework/http/middleware"
"github.com/totoval/framework/policy"
"github.com/totoval/framework/request"
"github.com/totoval/framework/request/websocket"
)
var RouteNameMap *routeNameMap
type routeNameMap struct {
lock sync.RWMutex
data map[string]string
}
func newRouteNameMap() *routeNameMap {
return &routeNameMap{
data: make(map[string]string),
}
}
func (rnm *routeNameMap) set(routeName, routeUrl string) {
rnm.lock.Lock()
defer rnm.lock.Unlock()
rnm.data[routeName] = routeUrl
}
func (rnm *routeNameMap) Get(routeName string, param toto.S) (url string, err error) {
rnm.lock.RLock()
defer rnm.lock.RUnlock()
routeUrl, ok := rnm.data[routeName]
if !strings.Contains(routeUrl, "/:") {
// simple url
return routeUrl, nil
}
// param url
if !ok {
return "", errors.New("Cannot find route by Name " + routeName)
}
for k, v := range param {
routeUrl = strings.Replace(routeUrl, "/:"+k, v, 1)
}
return routeUrl, nil
}
type route struct {
name string
bindFunc func(handlers ...request.HandlerFunc)
handlers []request.HandlerFunc
relativePath string
httpMethod string
basicPath string
prefixHandlersNum int
wsHandler websocket.Handler
}
const maxRouteMapLength = 1000
type engineRoute struct {
lock sync.RWMutex
data map[request.EngineHash]chan *route
}
func newEngineRoute() *engineRoute {
return &engineRoute{
data: make(map[request.EngineHash]chan *route),
}
}
func (erm *engineRoute) initEngine(eh request.EngineHash) {
if erm.data[eh] == nil {
erm.data[eh] = make(chan *route, maxRouteMapLength)
}
}
func (erm *engineRoute) Get(eh request.EngineHash) chan *route {
erm.lock.RLock()
defer erm.lock.RUnlock()
return erm.data[eh]
}
func (erm *engineRoute) Set(eh request.EngineHash, r *route) {
//@todo doesn't process the situation that for multi serve
erm.lock.Lock()
defer erm.lock.Unlock()
erm.initEngine(eh)
erm.data[eh] <- r
}
func (erm *engineRoute) Close(eh request.EngineHash) {
close(erm.data[eh])
}
var engineRouteMap *engineRoute
func init() {
engineRouteMap = newEngineRoute()
RouteNameMap = newRouteNameMap()
}
func newRoute(httpMethod string, g *group, relativePath string, bindFunc func(ginHandlers ...request.HandlerFunc), handlers ...request.HandlerFunc) *route {
r := route{httpMethod: httpMethod, prefixHandlersNum: len(g.RouterGroup.Handlers), basicPath: g.RouterGroup.BasePath(), relativePath: relativePath, bindFunc: bindFunc, handlers: handlers}
engineRouteMap.Set(g.engineHash, &r)
return &r
}
func newWsRoute(httpMethod string, g *group, relativePath string, bindWsFunc func(wsHandler websocket.Handler, ginHandlers ...request.HandlerFunc), wsHandler websocket.Handler, handlers ...request.HandlerFunc) *route {
r := route{wsHandler: wsHandler, httpMethod: httpMethod, prefixHandlersNum: len(g.RouterGroup.Handlers), basicPath: g.RouterGroup.BasePath(), relativePath: relativePath, bindFunc: func(handlers ...request.HandlerFunc) {
// normalHandler -> wshandler
httpHs := append(handlers, func(context request.Context) {
//@todo websocket handler placeholder
})
bindWsFunc(wsHandler, httpHs...)
}, handlers: handlers}
engineRouteMap.Set(g.engineHash, &r)
return &r
}
func (r *route) Name(routeName string) policy.RoutePolicier {
r.name = routeName
return r
}
func (r *route) Can(policies policy.Policier, action policy.Action) {
r.handlers = append([]request.HandlerFunc{middleware.Policy(policies, action)}, r.handlers...)
}
func (r *route) handlerNum() int {
return r.prefixHandlersNum + len(r.handlers)
}
func (r *route) absolutePath() string {
return fmt.Sprintf("%s%s", r.basicPath, r.relativePath)
}
func (r *route) lastHandlerName() string {
if r.httpMethod == httpMethodWebsocket {
h := reflect.ValueOf(r.wsHandler).Elem().Type()
return h.PkgPath() + "." + h.Name()
}
if len(r.handlers) > 0 {
return runtime.FuncForPC(reflect.ValueOf(r.handlers[len(r.handlers)-1]).Pointer()).Name()
}
return "nil"
}
func Bind(engine *request.Engine) {
hash := engine.Hash()
defer engineRouteMap.Close(hash)
for r := range engineRouteMap.Get(hash) {
r.bindFunc(r.handlers...)
// for Route() function to easy retrieve url by name
RouteNameMap.set(r.name, r.absolutePath())
if app.GetMode() != app.ModeProduction {
log.Info(fmt.Sprintf("%-6s %-30s --> %s (%d handlers)\n", r.httpMethod, r.absolutePath(), r.lastHandlerName(), r.handlerNum()))
}
if len(engineRouteMap.Get(hash)) <= 0 {
break
}
}
}