-
Notifications
You must be signed in to change notification settings - Fork 49
/
rest.go
213 lines (168 loc) · 4.83 KB
/
rest.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* EliasDB
*
* Copyright 2016 Matthias Ladkau. All rights reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package api
import (
"net/http"
"strings"
"github.com/krotik/common/datautil"
"github.com/krotik/eliasdb/cluster"
"github.com/krotik/eliasdb/ecal"
"github.com/krotik/eliasdb/graph"
"github.com/krotik/eliasdb/graph/graphstorage"
)
/*
APIVersion is the version of the REST API
*/
const APIVersion = "1.0.0"
/*
APIRoot is the root directory for the REST API
*/
const APIRoot = "/db"
/*
APISchemes is a list of supported protocol schemes
*/
var APISchemes = []string{"https"}
/*
APIHost is the host definition for the REST API
*/
var APIHost = "localhost:9090"
/*
GeneralEndpointMap contains general endpoints which should always be available
*/
var GeneralEndpointMap = map[string]RestEndpointInst{
EndpointAbout: AboutEndpointInst,
EndpointSwagger: SwaggerEndpointInst,
}
/*
RestEndpointInst models a factory function for REST endpoint handlers.
*/
type RestEndpointInst func() RestEndpointHandler
/*
RestEndpointHandler models a REST endpoint handler.
*/
type RestEndpointHandler interface {
/*
HandleGET handles a GET request.
*/
HandleGET(w http.ResponseWriter, r *http.Request, resources []string)
/*
HandlePOST handles a POST request.
*/
HandlePOST(w http.ResponseWriter, r *http.Request, resources []string)
/*
HandlePUT handles a PUT request.
*/
HandlePUT(w http.ResponseWriter, r *http.Request, resources []string)
/*
HandleDELETE handles a DELETE request.
*/
HandleDELETE(w http.ResponseWriter, r *http.Request, resources []string)
/*
SwaggerDefs is used to describe the endpoint in swagger.
*/
SwaggerDefs(s map[string]interface{})
}
/*
GM is the GraphManager instance which should be used by the REST API.
*/
var GM *graph.Manager
/*
SI is the ScriptingInterpreter instance which is working with the api.GM GraphManager instance.
*/
var SI *ecal.ScriptingInterpreter
/*
GS is the GraphStorage instance which should be used by the REST API.
*/
var GS graphstorage.Storage
/*
DD is the DistributedStorage instance which should be used by the REST API.
(Only available if clustering is enabled.)
*/
var DD *cluster.DistributedStorage
/*
DDLog is a ringbuffer containing cluster related logs.
(Only available if clustering is enabled.)
*/
var DDLog *datautil.RingBuffer
/*
Map of all registered endpoint handlers.
*/
var registered = map[string]RestEndpointInst{}
/*
HandleFunc to use for registering handlers
Should be of type: func(pattern string, handler func(http.ResponseWriter, *http.Request))
*/
var HandleFunc = http.HandleFunc
/*
RegisterRestEndpoints registers all given REST endpoint handlers.
*/
func RegisterRestEndpoints(endpointInsts map[string]RestEndpointInst) {
for url, endpointInst := range endpointInsts {
registered[url] = endpointInst
HandleFunc(url, func() func(w http.ResponseWriter, r *http.Request) {
var handlerURL = url
var handlerInst = endpointInst
return func(w http.ResponseWriter, r *http.Request) {
// Create a new handler instance
handler := handlerInst()
// Handle request in appropriate method
res := strings.TrimSpace(r.URL.Path[len(handlerURL):])
if len(res) > 0 && res[len(res)-1] == '/' {
res = res[:len(res)-1]
}
var resources []string
if res != "" {
resources = strings.Split(res, "/")
}
switch r.Method {
case "GET":
handler.HandleGET(w, r, resources)
case "POST":
handler.HandlePOST(w, r, resources)
case "PUT":
handler.HandlePUT(w, r, resources)
case "DELETE":
handler.HandleDELETE(w, r, resources)
default:
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
}
}
}())
}
}
/*
DefaultEndpointHandler represents the default endpoint handler.
*/
type DefaultEndpointHandler struct {
}
/*
HandleGET is a method stub returning an error.
*/
func (de *DefaultEndpointHandler) HandleGET(w http.ResponseWriter, r *http.Request, resources []string) {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
}
/*
HandlePOST is a method stub returning an error.
*/
func (de *DefaultEndpointHandler) HandlePOST(w http.ResponseWriter, r *http.Request, resources []string) {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
}
/*
HandlePUT is a method stub returning an error.
*/
func (de *DefaultEndpointHandler) HandlePUT(w http.ResponseWriter, r *http.Request, resources []string) {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
}
/*
HandleDELETE is a method stub returning an error.
*/
func (de *DefaultEndpointHandler) HandleDELETE(w http.ResponseWriter, r *http.Request, resources []string) {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
}