-
Notifications
You must be signed in to change notification settings - Fork 6
/
router.go
214 lines (187 loc) · 5.47 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package foolgo
import (
"errors"
"reflect"
"strings"
)
type Router struct {
}
type router_rewrite struct {
urlParts map[int]map[string]string
staticNum int
method string
controller string
action string
}
var (
router_instance *Router
router_rewrite_map = make(map[string][]*router_rewrite)
router_rewrite_key = make(map[string]string)
)
func NewRouter() *Router { /*{{{*/
if router_instance != nil {
return router_instance
}
router_instance = &Router{}
return router_instance
} /*}}}*/
func GetRouter() *Router {
return router_instance
}
// Reg router in controller by func RegRouter() map[string]interface{}
// support rewrite
func RegRouter(controller FGController, controller_name string) error { /*{{{*/
url_map := controller.RegRouter()
if url_map == nil {
return nil
}
for pattern, tb := range url_map {
switch tb.(type) {
case string:
if old, ok := router_rewrite_key["GET "+pattern]; ok == true {
return errors.New("Can't register router:\"GET " + pattern + "\",it had been registed in controller:" + old)
}
r := createRewriteRouter(pattern, "GET", controller_name, tb.(string))
if r == nil {
return errors.New("Can't register router:" + pattern)
}
router_rewrite_map["GET"] = append(router_rewrite_map["GET"], r)
router_rewrite_key["GET "+pattern] = controller_name
case map[string]string:
for method, action := range tb.(map[string]string) {
method = strings.ToUpper(method)
if old, ok := router_rewrite_key[method+" "+pattern]; ok == true {
return errors.New("Can't register router:\"" + method + " " + pattern + "\",it had been registed in controller:" + old)
}
r := createRewriteRouter(pattern, method, controller_name, action)
if r == nil {
return errors.New("Can't register router:\"" + method + " " + pattern + "\"")
}
router_rewrite_map[method] = append(router_rewrite_map[method], r)
router_rewrite_key[method+" "+pattern] = controller_name
}
}
}
return nil
} /*}}}*/
// create rewrite router
func createRewriteRouter(pattern, method, controller, action string) *router_rewrite { /*{{{*/
if pattern == "" || action == "" {
return nil
}
url_parts := strings.Split(strings.Trim(pattern, "/"), "/")
router := &router_rewrite{
urlParts: make(map[int]map[string]string),
staticNum: 0,
method: method,
controller: controller,
action: action,
}
for pos, part := range url_parts {
if part[0:1] == ":" {
router.urlParts[pos] = map[string]string{
"name": part[1:],
"type": "var",
}
} else if part[0:1] == "*" {
router.urlParts[pos] = map[string]string{
"name": part,
"type": "",
}
break
} else {
router.urlParts[pos] = map[string]string{
"name": part,
"type": "",
}
router.staticNum++
}
}
return router
} /*}}}*/
func (this *Router) MatchRewrite(url, method string) (string, string, map[string]string, error) { /*{{{*/
if _, ok := router_rewrite_map[method]; ok == false {
return "", "", nil, errors.New("No match")
}
paths := strings.Split(strings.Trim(url, "/"), "/")
for _, router := range router_rewrite_map[method] {
if match_param, matched := this.matchRouter(router, paths); matched == false {
continue
} else {
return router.controller, router.action, match_param, nil
}
}
return "", "", nil, errors.New("No match")
} /*}}}*/
func (this *Router) matchRouter(router *router_rewrite, paths []string) (map[string]string, bool) { /*{{{*/
var match_param map[string]string
var cnt int
for pos, part := range paths {
if _, ok := router.urlParts[pos]; ok == false {
return nil, false
}
if router.urlParts[pos]["type"] == "" {
if router.urlParts[pos]["name"] == "*" {
if pos < len(paths) {
if match_param == nil {
match_param = make(map[string]string)
}
param := paths[pos:]
param_num := len(param) / 2
for i := 0; i < param_num; i++ {
match_param[param[i*2]] = param[i*2+1]
}
}
break
}
if router.urlParts[pos]["name"] != part {
return nil, false
}
cnt++
} else if router.urlParts[pos]["type"] == "var" {
if match_param == nil {
match_param = make(map[string]string)
}
match_param[router.urlParts[pos]["name"]] = part
}
}
if cnt != router.staticNum {
return nil, false
}
return match_param, true
} /*}}}*/
// Get controller and action name from
// request parame "m"
// eg: demo.index,return demo index
func (this *Router) ParseMethod(method string) (controller_name string, action_name string) { /*{{{*/
method_map := strings.SplitN(method, ".", 2)
switch len(method_map) {
case 1:
controller_name = method_map[0]
case 2:
controller_name = method_map[0]
action_name = method_map[1]
}
return controller_name, action_name
} /*}}}*/
// create new controller by controller name
func (this *Router) NewController(controller_name string) (reflect.Value, error) { /*{{{*/
register := GetRegister()
//m_arr := make([]reflect.Value, 0)
var controller_instance reflect.Value
if register == nil {
//http 500
return controller_instance, errors.New("Server Error : Can't find \"Register\"")
}
controller_type := register.GetController(controller_name)
if controller_type == nil {
//http 404
return controller_instance, errors.New("Warn : Can't find " + controller_name)
}
controller_instance = reflect.New(controller_type)
if false == controller_instance.IsValid() {
//http 404
return controller_instance, errors.New("Warn : Can't find " + controller_name)
}
return controller_instance, nil
} /*}}}*/