-
Notifications
You must be signed in to change notification settings - Fork 2
/
goldencheetahCloudDB.go
299 lines (245 loc) · 11.9 KB
/
goldencheetahCloudDB.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* Copyright (c) 2015, 2016, 2017, 2020 Joern Rischmueller (joern.rm@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"net/http"
"os"
"google.golang.org/appengine"
"github.com/emicklei/go-restful" // @Version Tag v2.11.2
)
func main() {
appengine.Main()
}
// init the Webserver within the GAE framework
func init() {
ws := new(restful.WebService)
// ----------------------------------------------------------------------------------
// setup the gcharts endpoints - processing see "entity_gcharts.go"
// ----------------------------------------------------------------------------------
ws.
Path("/v1").
Doc("Manage GCharts").
Consumes(restful.MIME_JSON).
Produces(restful.MIME_JSON) // you can specify this per route as well
ws.Route(ws.POST("/gchart/").Filter(basicAuthenticate).Filter(filterCloudDBStatus).To(insertGChart).
// docs
Doc("creates a gchart").
Operation("createGChart").
Reads(GChartPostAPIv1{})) // from the request
ws.Route(ws.PUT("/gchart/").Filter(basicAuthenticate).Filter(filterCloudDBStatus).To(updateGChart).
// docs
Doc("updates a gchart").
Operation("updatedGChart").
Reads(GChartPostAPIv1{})) // from the request
ws.Route(ws.GET("/gchart/{id}").Filter(basicAuthenticate).Filter(filterCloudDBStatus).To(getGChartById).
// docs
Doc("get a gchart").
Operation("getGChartbyId").
Param(ws.PathParameter("id", "identifier of the gchart").DataType("string")).
Writes(GChartGetAPIv1{})) // on the response
ws.Route(ws.PUT("/gchartuse/{id}").Filter(basicAuthenticate).Filter(filterCloudDBStatus).To(incrementGChartUsageById).
// docs
Doc("increments the DL use counter for a chart by 1").
Operation("incrementUsageCounterById").
Param(ws.PathParameter("id", "identifier of the gchart").DataType("string")))
ws.Route(ws.DELETE("/gchart/{id}").Filter(basicAuthenticate).Filter(filterCloudDBStatus).To(deleteGChartById).
// docs
Doc("delete a gchart by setting the deleted status").
Operation("deleteGChartbyId").
Param(ws.PathParameter("id", "identifier of the chart").DataType("string")))
ws.Route(ws.PUT("/gchartcuration/{id}").Filter(basicAuthenticate).Filter(filterCloudDBStatus).To(curateGChartById).
// docs
Doc("set the curation status of the gchart to {newStatus} which must be 'true' or 'false' ").
Operation("updateGChartCurationStatus").
Param(ws.PathParameter("id", "identifier of the gchart").DataType("string")).
Param(ws.QueryParameter("newStatus", "true/false curation status").DataType("bool")))
// Endpoint for GChartHeader only (no JPG or Definition)
ws.Route(ws.GET("/gchartheader").Filter(basicAuthenticate).Filter(filterCloudDBStatus).To(getGChartHeader).
// docs
Doc("gets a collection of gcharts header - in buckets of x charts - table sort is new to old").
Operation("getGChartHeader").
Param(ws.QueryParameter("dateFrom", "Date of last change").DataType("string")).
Writes(GChartAPIv1HeaderOnlyList{})) // on the response
// Count Chart Headers to be retrieved
ws.Route(ws.GET("/gchartheader/count").Filter(basicAuthenticate).Filter(filterCloudDBStatus).To(getGChartHeaderCount).
// docs
Doc("gets the number of gchart headers for testing,... selection").
Operation("getGChartHeader").
Param(ws.QueryParameter("dateFrom", "Date of last change").DataType("string")))
// ----------------------------------------------------------------------------------
// setup the usermetric endpoints - processing see "entity_usermetric.go"
// ----------------------------------------------------------------------------------
ws.
Path("/v1").
Doc("Manage User Metrics").
Consumes(restful.MIME_JSON).
Produces(restful.MIME_JSON) // you can specify this per route as well
ws.Route(ws.POST("/usermetric/").Filter(basicAuthenticate).Filter(filterCloudDBStatus).To(insertUserMetric).
// docs
Doc("creates a usermetric").
Operation("createUserMetric").
Reads(UserMetricAPIv1{})) // from the request
ws.Route(ws.PUT("/usermetric/").Filter(basicAuthenticate).Filter(filterCloudDBStatus).To(updateUserMetric).
// docs
Doc("updates a usermetric").
Operation("updateUserMetric").
Reads(UserMetricAPIv1{})) // from the request
ws.Route(ws.GET("/usermetric/{id}").Filter(basicAuthenticate).Filter(filterCloudDBStatus).To(getUserMetricById).
// docs
Doc("get a usermetric").
Operation("getUserMetricbyId").
Param(ws.PathParameter("id", "identifier of the user metric").DataType("string")).
Writes(UserMetricAPIv1{})) // on the response
ws.Route(ws.DELETE("/usermetric/{id}").Filter(basicAuthenticate).Filter(filterCloudDBStatus).To(deleteUserMetricById).
// docs
Doc("delete a usermetric by setting the deleted status").
Operation("deleteUserMetricbyId").
Param(ws.PathParameter("id", "identifier of the usermetric").DataType("string")))
ws.Route(ws.PUT("/usermetriccuration/{id}").Filter(basicAuthenticate).Filter(filterCloudDBStatus).To(curateUserMetricById).
// docs
Doc("set the curation status of the usermetric to {newStatus} which must be 'true' or 'false' ").
Operation("updateUserMetricCurationStatus").
Param(ws.PathParameter("id", "identifier of the usermetric").DataType("string")).
Param(ws.QueryParameter("newStatus", "true/false curation status").DataType("bool")))
// Endpoint for Header only
ws.Route(ws.GET("/usermetricheader").Filter(basicAuthenticate).Filter(filterCloudDBStatus).To(getUserMetricHeader).
// docs
Doc("gets a collection of usermetric header - in buckets of x headers - table sort is new to old").
Operation("getUserMetricHeader").
Param(ws.QueryParameter("dateFrom", "Date of last change").DataType("string")).
Writes(UserMetricAPIv1HeaderOnlyList{})) // on the response
// Count Chart Headers to be retrieved
ws.Route(ws.GET("/usermetricheader/count").Filter(basicAuthenticate).Filter(filterCloudDBStatus).To(getUserMetricHeaderCount).
// docs
Doc("gets the number of usermetric headers for testing,... selection").
Operation("getUserMetricHeader").
Param(ws.QueryParameter("dateFrom", "Date of last change").DataType("string")))
// ----------------------------------------------------------------------------------
// setup the curator endpoints - processing see "entity_curator.go"
// ----------------------------------------------------------------------------------
ws.Route(ws.GET("/curator").Filter(basicAuthenticate).To(getCurator).
// docs
Doc("gets a collection of curators").
Operation("getCurator").
Param(ws.QueryParameter("curatorId", "UUid of the Curator").DataType("string")).
Writes(CuratorAPIv1List{})) // on the response
ws.Route(ws.POST("/curator").Filter(basicAuthenticate).To(insertCurator).
// docs
Doc("creates a curator").
Operation("createCurator").
Reads(CuratorAPIv1{})) // from the request
// ----------------------------------------------------------------------------------
// setup the status endpoints - processing see "entity_status.go"
// ----------------------------------------------------------------------------------
ws.Route(ws.POST("/status").Filter(basicAuthenticate).To(insertStatus).
// docs
Doc("creates a new status entity").
Operation("createStatus").
Reads(StatusEntityPostAPIv1{})) // from the request
ws.Route(ws.GET("/status").Filter(basicAuthenticate).To(getStatus).
// docs
Doc("gets a collection of status").
Operation("getStatus").
Param(ws.QueryParameter("dateFrom", "Status Validity").DataType("string")).
Writes(StatusEntityGetAPIv1List{})) // on the response
ws.Route(ws.GET("/status/latest").Filter(basicAuthenticate).To(getCurrentStatus).
// docs
Doc("gets the current/latest status").
Operation("getStatus").
Writes(StatusEntityGetAPIv1{})) // on the response
ws.Route(ws.GET("/statustext/{id}").Filter(basicAuthenticate).To(getStatusTextById).
// docs
Doc("gets the text for a specific status entity").
Operation("getStatusText").
Param(ws.PathParameter("id", "identifier of the version text").DataType("string")).
Writes(StatusEntityGetTextAPIv1{})) // on the response
// ----------------------------------------------------------------------------------
// setup the version endpoints - processing see "entity_version.go"
// ----------------------------------------------------------------------------------
ws.Route(ws.POST("/version").Filter(basicAuthenticate).To(insertVersion).
// docs
Doc("creates a new version entity").
Operation("createVersion").
Reads(VersionEntityPostAPIv1{})) // from the request
ws.Route(ws.GET("/version").Filter(basicAuthenticate).To(getVersion).
// docs
Doc("gets a collection of versions").
Operation("getVersion").
Param(ws.QueryParameter("version", "Version").DataType("string")).
Writes(VersionEntityGetAPIv1List{})) // on the response
ws.Route(ws.GET("/version/latest").Filter(basicAuthenticate).To(getLatestVersion).
// docs
Doc("gets the latest version").
Operation("getVersion").
Writes(VersionEntityGetAPIv1{})) // on the response
// ----------------------------------------------------------------------------------
// setup the telemetry endpoints - processing see "entity_telemetry.go"
// ----------------------------------------------------------------------------------
ws.Route(ws.PUT("/telemetry").Filter(basicAuthenticate).To(upsertTelemetry).
// docs
Doc("stores location,... of the call based on IP adress").
Operation("post telemetry data").
Reads(TelemetryEntityPostAPIv1{})) // from the request
ws.Route(ws.GET("/telemetry").Filter(basicAuthenticate).To(getTelemetry).
// docs
Doc("gets a collection of versions").
Operation("get All Telemetry Data").
Param(ws.QueryParameter("createdAfter", "Telemetry created after").DataType("string")).
Param(ws.QueryParameter("updatedAfter", "Telemetry last updated after").DataType("string")).
Param(ws.QueryParameter("os", "Operating System").DataType("string")).
Param(ws.QueryParameter("version", "GoldenCheetah Version").DataType("string")).
Writes(TelemetryEntityGetAPIv1List{})) // on the response
// all routes defined - let's go
restful.Add(ws)
} // init()
// global declarations
const basicauth = "Basic_Auth"
const authorization = "Authorization"
const dateTimeLayout = "2006-01-02T15:04:05Z"
const (
http_UnprocessableEntity = 422
)
const status_unprocessable = "Error - CloudDB Status does not allow processing the request"
func basicAuthenticate(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) {
headerClientId := req.Request.Header.Get(authorization)
if secretClientId := os.Getenv(basicauth); secretClientId != "" {
if fmt.Sprint("Basic ",secretClientId) != headerClientId {
resp.AddHeader("WWW-Authenticate", "Basic realm=Protected Area")
resp.WriteErrorString(http.StatusUnauthorized, "Not Authorized")
return
}
} else {
resp.AddHeader("WWW-Authenticate", "Basic realm=Protected Area")
resp.WriteErrorString(http.StatusInternalServerError, "Authorization configuration missing on Server")
return
}
chain.ProcessFilter(req, resp)
} // basicAuthenticate
func filterCloudDBStatus(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) {
ctx := appengine.NewContext(req.Request)
if internalGetCurrentStatus(ctx) != Status_Ok {
addPlainTextError(resp, http_UnprocessableEntity, status_unprocessable)
return
}
chain.ProcessFilter(req, resp)
}
// Convenience functions for error handling
func addPlainTextError( r *restful.Response, httpStatus int, errorReason string ) {
r.AddHeader("Content-Type", "text/plain")
r.WriteErrorString(httpStatus, errorReason)
}