-
Notifications
You must be signed in to change notification settings - Fork 5
/
handler.go
60 lines (50 loc) · 1.43 KB
/
handler.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
package api
import (
"encoding/json"
"net/http"
"github.com/ethpandaops/assertoor/pkg/coordinator/types"
"github.com/sirupsen/logrus"
)
// @title Assertoor API Documentation
// @version 1.0
// @description API for querying information about Assertoor tests
// @tag.name Test
// @tag.description All endpoints related to test definitions
// @tag.name TestRun
// @tag.description All endpoints related to test runs
type Response struct {
Status string `json:"status"`
Data interface{} `json:"data"`
}
//nolint:revive // ignore
type APIHandler struct {
logger logrus.FieldLogger
coordinator types.Coordinator
}
func NewAPIHandler(logger logrus.FieldLogger, coordinator types.Coordinator) *APIHandler {
return &APIHandler{
logger: logger,
coordinator: coordinator,
}
}
func (ah *APIHandler) sendErrorResponse(w http.ResponseWriter, route, message string, errorcode int) {
w.WriteHeader(errorcode)
j := json.NewEncoder(w)
response := &Response{}
response.Status = "ERROR: " + message
err := j.Encode(response)
if err != nil {
ah.logger.Errorf("error serializing json error for API %v route: %v", route, err)
}
}
func (ah *APIHandler) sendOKResponse(w http.ResponseWriter, route string, data interface{}) {
j := json.NewEncoder(w)
response := &Response{
Status: "OK",
Data: data,
}
err := j.Encode(response)
if err != nil {
ah.logger.Errorf("error serializing json data for API %v route: %v", route, err)
}
}