forked from zhyueh/figo
/
router.go
68 lines (55 loc) · 1.46 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
// router base on reflect
// we would create a new controller for
// every http request
package figo
import (
//"fmt"
"github.com/zhyueh/figo/toolkit"
"reflect"
)
type Router struct {
routerMap map[string]reflect.Type
autoRouterMap map[string]reflect.Type
routerFunc func(string) ControllerInterface
}
func NewRouter() *Router {
re := &Router{}
re.routerFunc = nil
re.routerMap = make(map[string]reflect.Type, 10)
re.autoRouterMap = make(map[string]reflect.Type, 10)
return re
}
func (this *Router) SetRouterFunc(routerFunc func(string) ControllerInterface) {
this.routerFunc = routerFunc
}
func (this *Router) AddController(path string, c ControllerInterface) {
this.routerMap[path] = reflect.Indirect(reflect.ValueOf(c)).Type()
}
func (this *Router) AddAutoRouterController(path string, c ControllerInterface) {
this.autoRouterMap[path] = reflect.Indirect(reflect.ValueOf(c)).Type()
}
func (this *Router) GetController(path string) ControllerInterface {
var ci ControllerInterface = nil
//router func should be first
if this.routerFunc != nil {
ci = this.routerFunc(path)
}
//normal router map is second
if ci == nil {
cit, _ := this.routerMap[path]
if cit == nil {
//auto router map is the last
comps := toolkit.SplitString(path, "/")
if len(comps) > 1 {
cit, _ = this.autoRouterMap[comps[0]]
}
}
if cit == nil {
return nil
} else {
return reflect.New(cit).Interface().(ControllerInterface)
}
} else {
return ci
}
}