-
Notifications
You must be signed in to change notification settings - Fork 1
/
routing.go
151 lines (132 loc) · 3.84 KB
/
routing.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
package apikit
import (
"github.com/revel/revel"
"reflect"
"path"
"io/ioutil"
"strings"
"regexp"
"errors"
"github.com/robfig/pathtree"
)
// Register the RESTControllers
func RegisterRESTControllers(controllers []RESTController) {
revel.MainRouter = revel.NewRouter(path.Join(revel.BasePath, "conf", "routes"))
revel.MainRouter.Refresh()
for _, c := range controllers {
revel.RegisterController(c,
[]*revel.MethodType{
&revel.MethodType{
Name: "Get",
Args: []*revel.MethodArg{
{"id", reflect.TypeOf((*uint64)(nil))},
},
},
&revel.MethodType{
Name: "Post",
},
&revel.MethodType{
Name: "Put",
},
&revel.MethodType{
Name: "Delete",
Args: []*revel.MethodArg{
{"id", reflect.TypeOf((*uint64)(nil))},
},
},
},
)
}
restcontrollerPath := path.Join(revel.BasePath, "conf", "restcontroller-routes")
restcontrollerData, err := ioutil.ReadFile(restcontrollerPath)
if err != nil {
panic(err)
}
restcontrollerRoutes, err := parseRoutes(restcontrollerPath, "", string(restcontrollerData))
if err != nil {
panic(err)
}
revel.MainRouter.Routes = append(revel.MainRouter.Routes, restcontrollerRoutes...)
updateTree(revel.MainRouter)
}
func parseRoutes(routesPath, joinedPath, content string) ([]*revel.Route, error) {
var routes []*revel.Route
// For each line..
for n, line := range strings.Split(content, "\n") {
line = strings.TrimSpace(line)
if len(line) == 0 || line[0] == '#' {
continue
}
const modulePrefix = "module:"
// Handle included routes from modules.
// e.g. "module:testrunner" imports all routes from that module.
if strings.HasPrefix(line, modulePrefix) {
continue
}
// A single route
method, path, action, fixedArgs, found := parseRouteLine(line)
if !found {
continue
}
// this will avoid accidental double forward slashes in a route.
// this also avoids pathtree freaking out and causing a runtime panic
// because of the double slashes
if strings.HasSuffix(joinedPath, "/") && strings.HasPrefix(path, "/") {
joinedPath = joinedPath[0 : len(joinedPath)-1]
}
//path = strings.Join([]string{revel.BasePath, joinedPath, path}, "")
if strings.Contains(action, ":") {
return nil, errors.New("revel-apikit does not yet support catchall (:) actions")
}
// This will import the module routes under the path described in the
// routes file (joinedPath param). e.g. "* /jobs module:jobs" -> all
// routes' paths will have the path /jobs prepended to them.
// See #282 for more info
if strings.Contains(line, "*") {
return nil, errors.New("revel-apikit does not yet support wildcard (*) routes")
}
route := revel.NewRoute(method, path, action, fixedArgs, routesPath, n)
routes = append(routes, route)
}
return routes, nil
}
// Groups:
// 1: method
// 4: path
// 5: action
// 6: fixedargs
var routePattern *regexp.Regexp = regexp.MustCompile(
"(?i)^(GET|POST|PUT|DELETE|PATCH|OPTIONS|HEAD|WS|\\*)" +
"[(]?([^)]*)(\\))?[ \t]+" +
"(.*/[^ \t]*)[ \t]+([^ \t(]+)" +
`\(?([^)]*)\)?[ \t]*$`)
func parseRouteLine(line string) (method, path, action, fixedArgs string, found bool) {
var matches []string = routePattern.FindStringSubmatch(line)
if matches == nil {
return
}
method, path, action, fixedArgs = matches[1], matches[4], matches[5], matches[6]
found = true
return
}
func updateTree(router *revel.Router) error {
router.Tree = pathtree.New()
for _, route := range router.Routes {
err := router.Tree.Add(route.TreePath, route)
// Allow GETs to respond to HEAD requests.
if err == nil && route.Method == "GET" {
err = router.Tree.Add(treePath("HEAD", route.Path), route)
}
// Error adding a route to the pathtree.
if err != nil {
return errors.New(err.Error())
}
}
return nil
}
func treePath(method, path string) string {
if method == "*" {
method = ":METHOD"
}
return "/" + method + path
}