-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrouter.go
More file actions
162 lines (133 loc) · 3.63 KB
/
router.go
File metadata and controls
162 lines (133 loc) · 3.63 KB
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
package cycapi
import (
"net/http"
"path"
"strings"
)
type HandlerFunc func(*Context)
type version struct {
Name string
Resources map[string]resource
}
type resource struct {
Name string
Handler HandlerFunc
Subresources map[string]subresource
}
type subresource struct {
Name string
Handler HandlerFunc
}
type Routes struct {
Map map[string]version
}
var Router *Routes
func init() {
Router = &Routes{
make(map[string]version),
}
}
// Main entry point for each call
func (r *Routes) ServeHTTP(w http.ResponseWriter, req *http.Request) {
ctx := NewContext(w, req)
defer ctx.Request.Body.Close()
ctx.RouteInfo.Method = req.Method
// Remove trailing slashes
urlPath := path.Clean("/" + req.URL.Path)
for place, value := range strings.Split(urlPath, "/") {
switch place {
case 1:
ctx.RouteInfo.VersionName = value
break
case 2:
// Always check for a custom method i.e. user:login
resourceParts := strings.SplitN(value, ":", 2)
// The resource will always be in position 0
ctx.RouteInfo.ResourceName = resourceParts[0]
// Add the custom method if it exists
if len(resourceParts) == 2 {
ctx.RouteInfo.CustomMethod = resourceParts[1]
}
break
case 3:
ctx.RouteInfo.ResourceID = value
break
case 4:
// Always check for a custom method i.e. user:login
resourceParts := strings.SplitN(value, ":", 2)
// The resource will always be in position 0
ctx.RouteInfo.SubresourceName = resourceParts[0]
// Add the custom method if it exists
if len(resourceParts) == 2 {
ctx.RouteInfo.CustomMethod = resourceParts[1]
}
break
case 5:
ctx.RouteInfo.SubresourceID = value
break
default:
// Ignore
break
}
}
// Add tags to the sentry hub from the context
ctx.SetTag("Method", ctx.RouteInfo.Method)
ctx.SetTag("Resource", ctx.RouteInfo.ResourceName)
ctx.SetTag("SubResource", ctx.RouteInfo.SubresourceName)
// Load the subresource
if ctx.RouteInfo.SubresourceName != "" {
resource, exists := Router.Map[ctx.RouteInfo.VersionName].Resources[ctx.RouteInfo.ResourceName]
if !exists {
ctx.NotFound()
} else {
subresource, exists := resource.Subresources[ctx.RouteInfo.SubresourceName]
if !exists {
ctx.NotFound()
} else {
if ctx.RouteInfo.Method == "GET" && ctx.RouteInfo.SubresourceID == "" {
ctx.RouteInfo.Method = "LIST"
}
subresource.Handler.ServeHTTP(ctx)
}
}
} else {
resource, exists := Router.Map[ctx.RouteInfo.VersionName].Resources[ctx.RouteInfo.ResourceName]
if !exists {
ctx.NotFound()
} else {
if ctx.RouteInfo.Method == "GET" && ctx.RouteInfo.ResourceID == "" {
ctx.RouteInfo.Method = "LIST"
}
resource.Handler.ServeHTTP(ctx)
}
}
}
func (f HandlerFunc) ServeHTTP(context *Context) {
f(context)
}
func (r *Routes) AddVersion(name string) {
var version version
version.Name = name
version.Resources = make(map[string]resource)
Router.Map[name] = version
}
func (r *Routes) AddResource(name string, versionName string, handler HandlerFunc) {
var resourceObj resource
resourceObj.Name = name
resourceObj.Handler = handler
resourceObj.Subresources = make(map[string]subresource)
_, exists := Router.Map[versionName]
if !exists {
var version version
version.Name = name
version.Resources = make(map[string]resource)
Router.Map[versionName] = version
}
Router.Map[versionName].Resources[name] = resourceObj
}
func (r *Routes) AddSubresource(name string, version string, resourceName string, handler HandlerFunc) {
var subresource subresource
subresource.Name = name
subresource.Handler = handler
Router.Map[version].Resources[resourceName].Subresources[name] = subresource
}