-
Notifications
You must be signed in to change notification settings - Fork 229
/
worker.go
109 lines (90 loc) · 2.53 KB
/
worker.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
package types
import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/forbole/bdjuno/v4/modules/actions/logging"
"github.com/rs/zerolog/log"
)
// ActionsWorker represents the worker that is used to handle Hasura actions queries
type ActionsWorker struct {
mux *http.ServeMux
context *Context
}
// NewActionsWorker returns a new ActionsWorker instance
func NewActionsWorker(context *Context) *ActionsWorker {
return &ActionsWorker{
mux: http.NewServeMux(),
context: context,
}
}
// RegisterHandler registers the provided handler to be used on each call to the provided path
func (w *ActionsWorker) RegisterHandler(path string, handler ActionHandler) {
log.Debug().Str("action", path).Msg("registering actions handler")
w.mux.HandleFunc(path, func(writer http.ResponseWriter, request *http.Request) {
start := time.Now()
// Set the content type
writer.Header().Set("Content-Type", "application/json")
// Read the body
reqBody, err := io.ReadAll(request.Body)
if err != nil {
http.Error(writer, "invalid payload", http.StatusBadRequest)
return
}
defer request.Body.Close()
// Get the actions payload
var payload Payload
err = json.Unmarshal(reqBody, &payload)
if err != nil {
http.Error(writer, "invalid payload: failed to unmarshal json", http.StatusInternalServerError)
return
}
// Handle the request
res, err := handler(w.context, &payload)
if err != nil {
logging.ErrorCounter(path)
w.handleError(writer, path, err)
return
}
// Marshal the response
data, err := json.Marshal(res)
if err != nil {
logging.ErrorCounter(path)
w.handleError(writer, path, err)
return
}
// Prometheus
logging.SuccessCounter(path)
logging.ReponseTimeBuckets(path, start)
// Write the response
writer.Write(data)
})
}
// handleError allows to handle the given error by writing it to the provided writer
func (w *ActionsWorker) handleError(writer http.ResponseWriter, path string, err error) {
log.Error().Str("action", path).
Err(err).Msg("error while executing action")
errorObject := GraphQLError{
Message: err.Error(),
}
errorBody, err := json.Marshal(errorObject)
if err != nil {
panic(err)
}
writer.WriteHeader(http.StatusBadRequest)
writer.Write(errorBody)
}
// Start starts the worker
func (w *ActionsWorker) Start(host string, port uint) {
server := &http.Server{
Addr: fmt.Sprintf("%s:%d", host, port),
Handler: w.mux,
ReadHeaderTimeout: 3 * time.Second,
}
err := server.ListenAndServe()
if err != nil {
panic(err)
}
}