-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
166 lines (156 loc) · 4.26 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
package mano
import (
"fmt"
"reflect"
"regexp"
)
type Handler interface{}
type HandlerFunc func(Context) interface{}
type ServiceFunc func(Context) reflect.Value
type Router struct {
method HttpMethod
pattern string
middlewares []string
handler Handler
handle HandlerFunc
controller interface{}
// group *RouterGroup
// isGroup bool
// parent *Router
// items []*Router
}
func routerGetParams(handlerType reflect.Type, app *Application, instance interface{}) []ServiceFunc {
params := make([]ServiceFunc, handlerType.NumIn())
for i := 0; i < handlerType.NumIn(); i++ {
//ptyp := handlerType.In(i)
typeName := handlerType.In(i).String()
fn, ok := app.paramterMap[typeName]
if !ok {
switch typeName {
case "*mano.Application":
param := reflect.ValueOf(app)
fn = func(ctx Context) reflect.Value {
return param
}
case "mano.Context":
fn = func(ctx Context) reflect.Value {
return reflect.ValueOf(ctx)
}
case "*mano.Request":
fn = func(ctx Context) reflect.Value {
return reflect.ValueOf(ctx.Request())
}
case "*mano.Response":
fn = func(ctx Context) reflect.Value {
return reflect.ValueOf(ctx.Response())
}
case "*http.Request":
fn = func(ctx Context) reflect.Value {
return reflect.ValueOf(ctx.Request().RawRequest())
}
case "http.ResponseWriter":
fn = func(ctx Context) reflect.Value {
return reflect.ValueOf(ctx.Response().Writer())
}
default:
for _, val := range app.attrs {
vt := reflect.TypeOf(val)
if vt.String() == typeName {
param := reflect.ValueOf(val)
fn = func(ctx Context) reflect.Value {
return param
}
}
}
break
}
// 如果给定对象为实例方法,则传入实例
// TODO: 观察是否会有参数冲突
if fn == nil && i == 0 && instance != nil {
typ := reflect.TypeOf(instance)
if handlerType.In(i) == typ {
param := reflect.ValueOf(instance)
fn = func(ctx Context) reflect.Value {
return param
}
}
}
if fn == nil {
panic(fmt.Sprintf("参数不支持或未定义:%v", typeName))
}
app.paramterMap[typeName] = fn
}
params[i] = fn
}
return params
}
func routerBuildHandle(method reflect.Value, params []ServiceFunc) HandlerFunc {
return func(ctx Context) interface{} {
arr := make([]reflect.Value, len(params))
for i := 0; i < len(params); i++ {
arr[i] = params[i](ctx)
}
returns := method.Call(arr)
if len(returns) > 0 {
return returns[0].Interface()
}
return nil
}
}
func buildHandler(app *Application, handler Handler, instance interface{}) (handle HandlerFunc) {
if method, ok := handler.(reflect.Method); ok {
handle = routerBuildHandle(method.Func, routerGetParams(method.Func.Type(), app, instance))
} else {
handlerType := reflect.TypeOf(handler)
switch handlerType.Kind() {
case reflect.Func:
handle = routerBuildHandle(reflect.ValueOf(handler), routerGetParams(handlerType, app, instance))
default:
fmt.Println(handlerType.Kind())
}
}
return
}
var patternRegexp = regexp.MustCompile(`\{\s*(\w+)\s*(\??)\s*\}`)
func compilePattern(pattern string) (string, []string) {
var keys []string
var index []int
var matched []string
for {
index = patternRegexp.FindStringIndex(pattern)
if len(index) != 2 {
break
}
matched = patternRegexp.FindStringSubmatch(pattern)
if len(matched) != 3 {
break
}
keys = append(keys, matched[1])
partten := "(?P<" + matched[1] + ">\\w+)"
if matched[2] == "?" {
partten += "?"
//TODO:如果后面有空格的话将会判断不到
//t.Fatalf("****:%v----%v", index[1]+1, len(pattern))
if len(pattern) == index[1]+1 && pattern[index[1]] == '/' {
index[1] = index[1] + 1
partten += "/?"
}
}
pattern = pattern[:index[0]] + partten + pattern[index[1]:]
// t.Fatalf("****:%v", len(matched))
//break
}
return pattern, keys
}
// func (router *Router) buildTo(table *RouteTable, app *Application) {
// if !router.isGroup {
// pattern := regexp.MustCompile(compilePattern(mergePattern(router)))
// handler := buildHandler(app, router.handler)
// table.Register(pattern, handler, 0, false)
// } else {
// for _, r := range router.items {
// r.parent = router
// r.buildTo(table, app)
// }
// }
// }