-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
193 lines (176 loc) · 5.8 KB
/
handlers.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
package main
// handlers module
//
// Copyright (c) 2023 - Valentin Kuznetsov <[email protected]>
//
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"
srvConfig "github.com/CHESSComputing/golib/config"
services "github.com/CHESSComputing/golib/services"
"github.com/gin-gonic/gin"
)
// DataHandler provives access to GET / end-point
func DataHandler(c *gin.Context) {
rec := services.Response("DataDiscovery", http.StatusOK, services.OK, nil)
c.JSON(http.StatusOK, rec)
}
// NRecordsHandler return total number of records for given query
func NRecordsHandler(c *gin.Context) {
r := c.Request
defer r.Body.Close()
data, err := io.ReadAll(r.Body)
if err != nil {
rec := services.Response("DataDiscovery", http.StatusBadRequest, services.ReaderError, err)
c.JSON(http.StatusBadRequest, rec)
return
}
// keep query data for multiple search requests
query := data
// decompose query into service ones
qlMap, err := _qlMgr.ServiceQueries(string(query))
if err != nil {
rec := services.Response("DataDiscovery", http.StatusBadRequest, services.QueryError, err)
c.JSON(http.StatusBadRequest, rec)
return
}
if Verbose > 0 {
log.Printf("QL service map %+v", qlMap)
}
// to proceed obtain valid token
_httpReadRequest.GetToken()
// TODO: implement logic to search across different services like
// MetaData, DataBookkeeping, ScanService, etc.
// so far we query MetaData service
// get number of total records for our query
rurl := fmt.Sprintf("%s/count", srvConfig.Config.Services.MetaDataURL)
resp, err := _httpReadRequest.Post(rurl, "application/json", bytes.NewBuffer(query))
if err != nil {
rec := services.Response("DataDiscovery", http.StatusBadRequest, services.ServiceError, err)
c.JSON(http.StatusBadRequest, rec)
return
}
// read respnose from downstream service
defer resp.Body.Close()
data, err = io.ReadAll(resp.Body)
if err != nil {
rec := services.Response("DataDiscovery", http.StatusBadRequest, services.ReaderError, err)
c.JSON(http.StatusBadRequest, rec)
return
}
var nrecords int
err = json.Unmarshal(data, &nrecords)
if err != nil {
log.Printf("provided data: %v failed to unmarshal to int data-type\n", string(data))
rec := services.Response("DataDiscovery", http.StatusBadRequest, services.UnmarshalError, err)
c.JSON(http.StatusBadRequest, rec)
return
}
rec := services.ServiceResponse{
HttpCode: http.StatusOK,
Service: "DataDiscovery",
Results: services.ServiceResults{NRecords: nrecords},
Timestamp: time.Now().String(),
}
c.JSON(http.StatusOK, rec)
}
// SearchHandler provives access to GET / end-point
func SearchHandler(c *gin.Context) {
r := c.Request
defer r.Body.Close()
data, err := io.ReadAll(r.Body)
if err != nil {
rec := services.Response("DataDiscovery", http.StatusBadRequest, services.ReaderError, err)
c.JSON(http.StatusBadRequest, rec)
return
}
// keep query data for multiple search requests
query := data
// decompose query into service ones
qlMap, err := _qlMgr.ServiceQueries(string(query))
if err != nil {
rec := services.Response("DataDiscovery", http.StatusBadRequest, services.QueryError, err)
c.JSON(http.StatusBadRequest, rec)
return
}
if Verbose > 0 {
log.Printf("QL service map %+v", qlMap)
}
// to proceed obtain valid token
_httpReadRequest.GetToken()
// TODO: implement logic to search across different services like
// MetaData, DataBookkeeping, ScanService, etc.
// so far we query MetaData service
// get number of total records for our query
rurl := fmt.Sprintf("%s/count", srvConfig.Config.Services.MetaDataURL)
resp, err := _httpReadRequest.Post(rurl, "application/json", bytes.NewBuffer(query))
if err != nil {
rec := services.Response("DataDiscovery", http.StatusBadRequest, services.ServiceError, err)
c.JSON(http.StatusBadRequest, rec)
return
}
// read response from downstream service
defer resp.Body.Close()
data, err = io.ReadAll(resp.Body)
if err != nil {
rec := services.Response("DataDiscovery", http.StatusBadRequest, services.ReaderError, err)
c.JSON(http.StatusBadRequest, rec)
return
}
var nrecords int
err = json.Unmarshal(data, &nrecords)
if err != nil {
log.Printf("provided data: %v failed to unmarshal to int data-type\n", string(data))
rec := services.Response("DataDiscovery", http.StatusBadRequest, services.UnmarshalError, err)
c.JSON(http.StatusBadRequest, rec)
return
}
if nrecords == 0 {
rec := services.ServiceResponse{
HttpCode: http.StatusOK,
Service: "DataDiscovery",
Results: services.ServiceResults{NRecords: nrecords},
Timestamp: time.Now().String(),
}
c.JSON(http.StatusOK, rec)
return
}
// get results records
rurl = fmt.Sprintf("%s/search", srvConfig.Config.Services.MetaDataURL)
resp, err = _httpReadRequest.Post(rurl, "application/json", bytes.NewBuffer(query))
if err != nil {
rec := services.Response("DataDiscovery", http.StatusBadRequest, services.ServiceError, err)
c.JSON(http.StatusBadRequest, rec)
return
}
// read respnose from downstream service
defer resp.Body.Close()
data, err = io.ReadAll(resp.Body)
if err != nil {
rec := services.Response("DataDiscovery", http.StatusBadRequest, services.ReaderError, err)
c.JSON(http.StatusBadRequest, rec)
return
}
// each service response contains only list of records or error record
// therefore we'll wrap it in service response record
var records []map[string]any
err = json.Unmarshal(data, &records)
if err != nil {
log.Println("ERROR: response", string(data))
rec := services.Response("DataDiscovery", http.StatusBadRequest, services.UnmarshalError, err)
c.JSON(http.StatusBadRequest, rec)
return
}
rec := services.ServiceResponse{
HttpCode: http.StatusOK,
Service: "DataDiscovery",
Results: services.ServiceResults{NRecords: nrecords, Records: records},
Timestamp: time.Now().String(),
}
c.JSON(http.StatusOK, rec)
}