-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers.go
More file actions
350 lines (327 loc) · 8.72 KB
/
handlers.go
File metadata and controls
350 lines (327 loc) · 8.72 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package main
// DBS HTTP handlers
// Copyright (c) 2023 - Valentin Kuznetsov <vkuznet@gmail.com>
//
import (
"bytes"
"compress/gzip"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
"github.com/CHESSComputing/DataBookkeeping/dbs"
"github.com/CHESSComputing/golib/utils"
"github.com/gin-gonic/gin"
)
type NameRequest struct {
Name string `uri:"name" json:"name"`
}
// ChildHandler provides access to /child and /child/:name end-point
func ChildHandler(c *gin.Context) {
ApiHandler(c, "child")
}
// ParentHandler provides access to /parent and /parent/:name end-point
func ParentHandler(c *gin.Context) {
ApiHandler(c, "parent")
}
// FileHandler provides access to /files and /file/:name end-point
func FileHandler(c *gin.Context) {
ApiHandler(c, "file")
}
// OsinfoHandler provides access to /osinfo end-point
func OsinfoHandler(c *gin.Context) {
ApiHandler(c, "osinfo")
}
// EnvironmentHandler provides access to /environments end-point
func EnvironmentHandler(c *gin.Context) {
ApiHandler(c, "environment")
}
// ScriptHandler provides access to /scripts end-point
func ScriptHandler(c *gin.Context) {
ApiHandler(c, "script")
}
// ConfigHandler provides access to /configs end-point
func ConfigHandler(c *gin.Context) {
ApiHandler(c, "config")
}
// PackageHandler provides access to /packages end-point
func PackageHandler(c *gin.Context) {
ApiHandler(c, "package")
}
// DatasetHandler provides access to GET /datasets and /dataset/:name end-point
func DatasetHandler(c *gin.Context) {
ApiHandler(c, "dataset")
}
// ProvenanceHandler provides access to /provenance and /provenance/:did end-point
func ProvenanceHandler(c *gin.Context) {
ApiHandler(c, "provenance")
}
// ApiHandler represents generic API handler for GET/POST/PUT/DELETE requests of a specific API
func ApiHandler(c *gin.Context, api string) {
r := c.Request
if r.Method == "POST" {
DBSPostHandler(c, api)
} else if r.Method == "PUT" {
DBSPutHandler(c, api)
} else if r.Method == "DELETE" {
DBSDeleteHandler(c, api)
} else {
DBSGetHandler(c, api)
}
}
// helper function to get DBS API
func getApi(c *gin.Context, a string) (*dbs.API, error) {
r := c.Request
w := c.Writer
// define default separate as comma (used in JSON records)
sep := ","
if r.Header.Get("Accept") == "application/ndjson" {
sep = "" // no separator will be used to yield NDJSON
}
if sep != "" {
w.Header().Add("Content-Type", "application/json")
} else {
w.Header().Add("Content-Type", "application/ndjson")
}
var api *dbs.API
params := make(map[string]any)
if r.Method == "GET" {
// for example /file?dataset=/x/y/z we'll parse URL query
// r.URL.Query() returns map[string][]string
for k, values := range r.URL.Query() {
var vals []string
for _, v := range values {
vals = append(vals, v)
}
params[k] = vals
}
}
if r.Method == "GET" || r.Method == "DELETE" {
api = &dbs.API{
Writer: w,
Params: params,
Separator: sep,
Api: a,
ContentType: r.Header.Get("Content-Type"),
}
} else { // all other HTTP requests POST/PUT may contain payload
headerContentType := r.Header.Get("Content-Type")
if headerContentType != "application/json" {
msg := fmt.Sprintf("unsupported Content-Type: '%s'", headerContentType)
e := dbs.Error(dbs.ContentTypeErr, dbs.ContentTypeErrorCode, msg, "web.DBSPostHandler")
responseMsg(w, r, e, http.StatusUnsupportedMediaType)
return nil, errors.New(msg)
}
defer r.Body.Close()
// var params dbs.Record
if Verbose > 0 {
dn, _ := r.Header["Cms-Authn-Dn"]
log.Printf("DBSPostHandler: API=%s, dn=%s, uri=%s", a, dn, requestURI(r))
}
cby := createBy(r)
body := r.Body
// handle gzip content encoding
if r.Header.Get("Content-Encoding") == "gzip" {
r.Header.Del("Content-Length")
reader, err := gzip.NewReader(r.Body)
if err != nil {
msg := "unable to get gzip reader"
log.Println(msg, err)
e := dbs.Error(err, dbs.ReaderErrorCode, msg, "web.DBSPostHandler")
responseMsg(w, r, e, http.StatusInternalServerError)
return nil, errors.New(msg)
}
body = utils.GzipReader{reader, r.Body}
} else {
data, err := io.ReadAll(r.Body)
if err != nil {
msg := "unable to get io reader"
log.Println(msg, err)
e := dbs.Error(err, dbs.ReaderErrorCode, msg, "web.DBSPostHandler")
responseMsg(w, r, e, http.StatusInternalServerError)
return nil, errors.New(msg)
}
body = ioutil.NopCloser(bytes.NewBuffer(data))
}
api = &dbs.API{
Reader: body,
Writer: w,
Params: params,
Separator: sep,
CreateBy: cby,
Api: a,
ContentType: r.Header.Get("Content-Type"),
}
}
/*
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
w.Header().Set("Content-Encoding", "gzip")
api.ContentType = "gzip"
gw := gzip.NewWriter(w)
defer gw.Close()
api.Writer = utils.GzipWriter{GzipWriter: gw, Writer: w}
}
*/
// many APIs carry /api/*name RESTful end-point and we need to
// get name out of it
var rest NameRequest
if err := c.ShouldBindUri(&rest); err == nil {
if a == "dataset" && rest.Name != "" {
api.Params["dataset"] = rest.Name
} else if a == "file" && rest.Name != "" {
api.Params["file"] = rest.Name
} else if a == "parent" && rest.Name != "" {
api.Params["did"] = rest.Name
} else if a == "child" && rest.Name != "" {
api.Params["did"] = rest.Name
}
}
if Verbose > 0 {
log.Println("Call DBS API", api.String())
}
return api, nil
}
// DBSGetHandler is a generic Get handler to call DBS Get APIs.
//
//gocyclo:ignore
func DBSGetHandler(c *gin.Context, a string) {
r := c.Request
w := c.Writer
api, err := getApi(c, a)
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
w.Header().Set("Content-Encoding", "gzip")
api.ContentType = "gzip"
gw := gzip.NewWriter(w)
defer gw.Close()
api.Writer = utils.GzipWriter{GzipWriter: gw, Writer: w}
}
if err != nil {
responseMsg(w, r, err, http.StatusBadRequest)
}
if a == "dataset" {
err = api.GetDataset()
} else if a == "provenance" {
err = api.GetProvenance()
} else if a == "file" {
err = api.GetFile()
} else if a == "child" {
err = api.GetChild()
} else if a == "parent" {
err = api.GetParent()
} else if a == "osinfo" {
err = api.GetOsInfo()
} else if a == "environment" {
err = api.GetEnvironment()
} else if a == "script" {
err = api.GetScript()
} else if a == "config" {
err = api.GetConfig()
} else if a == "package" {
err = api.GetPackage()
} else {
err = dbs.NotImplementedApiErr
}
if err != nil {
responseMsg(w, r, err, http.StatusBadRequest)
return
}
}
// POST handler
//
// DBSPostHandler is a generic handler to call DBS Post APIs
//
//gocyclo:ignore
func DBSPostHandler(c *gin.Context, a string) {
r := c.Request
w := c.Writer
api, err := getApi(c, a)
if err != nil {
responseMsg(w, r, err, http.StatusBadRequest)
}
if a == "dataset" {
err = api.InsertDataset()
} else if a == "file" {
err = api.InsertFile()
} else if a == "parent" {
err = api.InsertParent()
} else if a == "osinfo" {
err = api.InsertOsInfo()
} else if a == "environment" {
err = api.InsertEnvironment()
} else if a == "script" {
err = api.InsertScript()
} else if a == "config" {
err = api.InsertConfig()
} else if a == "provenance" {
err = api.InsertProvenance()
} else {
err = dbs.NotImplementedApiErr
}
if err != nil {
responseMsg(w, r, err, http.StatusBadRequest)
return
}
}
// DBSPutHandler is a generic handler to call DBS put APIs
//
//gocyclo:ignore
func DBSPutHandler(c *gin.Context, a string) {
r := c.Request
w := c.Writer
api, err := getApi(c, a)
if err != nil {
responseMsg(w, r, err, http.StatusBadRequest)
}
if a == "dataset" {
err = api.UpdateDataset()
} else if a == "file" {
err = api.UpdateFile()
} else if a == "Parent" {
err = api.UpdateParent()
} else if a == "osinfo" {
err = api.UpdateOsInfo()
} else if a == "environment" {
err = api.UpdateEnvironment()
} else if a == "script" {
err = api.UpdateScript()
} else {
err = dbs.NotImplementedApiErr
}
if err != nil {
responseMsg(w, r, err, http.StatusBadRequest)
return
}
}
// DBSDeleteHandler is a generic handler to call DBS delete APIs
//
//gocyclo:ignore
func DBSDeleteHandler(c *gin.Context, a string) {
r := c.Request
w := c.Writer
api, err := getApi(c, a)
if err != nil {
responseMsg(w, r, err, http.StatusBadRequest)
}
if a == "dataset" {
err = api.DeleteDataset()
} else if a == "file" {
err = api.DeleteFile()
} else if a == "parent" {
err = api.DeleteParent()
} else if a == "osinfo" {
err = api.DeleteOsInfo()
} else if a == "environment" {
err = api.DeleteEnvironment()
} else if a == "script" {
err = api.DeleteScript()
} else {
err = dbs.NotImplementedApiErr
}
if err != nil {
responseMsg(w, r, err, http.StatusBadRequest)
return
}
}