-
Notifications
You must be signed in to change notification settings - Fork 7
/
routes.go
184 lines (155 loc) · 3.47 KB
/
routes.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
// Copyright 2014 Tim Shannon. All rights reserved.
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package main
import (
"fmt"
"net/http"
"runtime"
"bitbucket.org/tshannon/freehold/log"
"bitbucket.org/tshannon/treemux"
)
var rootHandler *treemux.Mux
var appHandler *treemux.Mux
func init() {
setupRoutes()
}
func setupRoutes() {
rootHandler = treemux.NewServeMux()
rootHandler.Handle("/", &methodHandler{
get: rootGet,
post: rootPost,
})
rootHandler.Handle("/v1/file/", &methodHandler{
get: fileGet,
post: filePost,
put: filePut,
delete: fileDelete,
})
rootHandler.Handle("/v1/datastore/", &methodHandler{
get: datastoreGet,
post: datastorePost,
put: datastorePut,
delete: datastoreDelete,
})
rootHandler.Handle("/v1/properties/", &methodHandler{
get: propertiesGet,
put: propertiesPut,
})
rootHandler.Handle("/docs/", &methodHandler{
get: docsGet,
})
rootHandler.Handle("/v1/auth/", &methodHandler{
get: authGet,
})
rootHandler.Handle("/v1/auth/session/", &methodHandler{
get: sessionGet,
post: sessionPost,
delete: sessionDelete,
})
rootHandler.Handle("/v1/application/", &methodHandler{
get: appGet,
post: appPost,
put: appPut,
delete: appDelete,
})
rootHandler.Handle("/v1/application/available/", &methodHandler{
get: appAvailableGet,
post: appAvailablePost,
})
rootHandler.Handle("/v1/settings/", &methodHandler{
get: settingsGet,
put: settingsPut,
delete: settingsDelete,
})
rootHandler.Handle("/v1/auth/user/", &methodHandler{
get: userGet,
post: userPost,
put: userPut,
delete: userDelete,
})
rootHandler.Handle("/v1/auth/token/", &methodHandler{
get: tokenGet,
post: tokenPost,
delete: tokenDelete,
})
rootHandler.Handle("/v1/log/", &methodHandler{
get: logGet,
})
rootHandler.Handle("/v1/backup/", &methodHandler{
get: backupGet,
post: backupPost,
})
//apps
appHandler = treemux.NewServeMux()
rootHandler.SetChild(appHandler)
appHandler.Handle("/", &methodHandler{
get: appRootGet,
})
appHandler.Handle("/v1/file/", &methodHandler{
get: fileGet,
post: filePost,
put: filePut,
delete: fileDelete,
})
appHandler.Handle("/v1/datastore/", &methodHandler{
get: datastoreGet,
post: datastorePost,
put: datastorePut,
delete: datastoreDelete,
})
appHandler.Handle("/v1/properties/", &methodHandler{
get: propertiesGet,
put: propertiesPut,
})
}
type methodHandler struct {
get http.HandlerFunc
post http.HandlerFunc
put http.HandlerFunc
delete http.HandlerFunc
}
func (m *methodHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer func() {
if r := recover(); r != nil {
if _, ok := r.(runtime.Error); ok {
//Try to log the error, then halt the instance
log.NewEntry("Runtime Error", r.(runtime.Error).Error())
buf := make([]byte, 1<<20)
fmt.Printf("Runtime Error: %s\n", buf[:runtime.Stack(buf, true)])
halt(r.(runtime.Error).Error())
}
errHandled(fmt.Errorf("Freehold panicked on %v and has recovered", r), w, nil)
return
}
}()
if m.get == nil {
m.get = four04
}
if m.post == nil {
m.post = four04
}
if m.put == nil {
m.put = four04
}
if m.delete == nil {
m.delete = four04
}
switch r.Method {
case "GET":
m.get(w, r)
return
case "POST":
m.post(w, r)
return
case "PUT":
m.put(w, r)
return
case "DELETE":
m.delete(w, r)
return
default:
four04(w, r)
return
}
}