-
Notifications
You must be signed in to change notification settings - Fork 316
/
http.go
152 lines (133 loc) · 3.63 KB
/
http.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
package http
import (
"errors"
"net/http"
"github.com/go-chi/chi/v5"
jsoniter "github.com/json-iterator/go"
"github.com/rudderlabs/rudder-go-kit/logger"
"github.com/rudderlabs/rudder-server/services/rsources"
)
func NewHandler(service rsources.JobService, logger logger.Logger) http.Handler {
h := &handler{
service: service,
logger: logger,
}
srvMux := chi.NewRouter()
srvMux.Get("/{job_run_id}", h.getStatus)
srvMux.Delete("/{job_run_id}", h.delete)
srvMux.Get("/{job_run_id}/failed-records", h.failedRecords)
return srvMux
}
type handler struct {
logger logger.Logger
service rsources.JobService
}
func (h *handler) delete(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
jobRunId, taskRunId, sourceId := getQueryParams(r)
if jobRunId == "" {
http.Error(w, "job_run_id not found", http.StatusBadRequest)
return
}
err := h.service.Delete(ctx, jobRunId, rsources.JobFilter{TaskRunID: taskRunId, SourceID: sourceId})
if err != nil {
httpStatus := http.StatusInternalServerError
switch {
case errors.Is(err, rsources.ErrStatusNotFound):
httpStatus = http.StatusNotFound
case errors.Is(err, rsources.ErrSourceNotCompleted):
httpStatus = http.StatusBadRequest
}
http.Error(w, err.Error(), httpStatus)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (h *handler) getStatus(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var jobRunId string
var taskRunId, sourceId []string
jobRunId, taskRunId, sourceId = getQueryParams(r)
if jobRunId == "" {
http.Error(w, "job_run_id not found", http.StatusBadRequest)
return
}
jobStatus, err := h.service.GetStatus(
ctx,
jobRunId,
rsources.JobFilter{
TaskRunID: taskRunId,
SourceID: sourceId,
})
if err != nil {
if errors.Is(err, rsources.ErrStatusNotFound) {
http.Error(w, err.Error(), http.StatusNotFound)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-store")
w.WriteHeader(http.StatusOK)
err = marshalAndWriteResponse(w, jobStatus)
if err != nil {
h.logger.Errorf("error while marshalling and writing response body: %v", err)
}
}
func (h *handler) failedRecords(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var jobRunId string
var taskRunId, sourceId []string
jobRunId, taskRunId, sourceId = getQueryParams(r)
if jobRunId == "" {
http.Error(w, "job_run_id not found", http.StatusBadRequest)
return
}
failedRecords, err := h.service.GetFailedRecords(
ctx, jobRunId, rsources.JobFilter{
TaskRunID: taskRunId,
SourceID: sourceId,
},
)
if err != nil {
httpStatus := http.StatusInternalServerError
if errors.Is(err, rsources.ErrOperationNotSupported) {
httpStatus = http.StatusBadRequest
}
http.Error(w, err.Error(), httpStatus)
return
}
err = marshalAndWriteResponse(w, failedRecords)
if err != nil {
h.logger.Errorf("error while marshalling and writing response body: %v", err)
}
}
func getQueryParams(r *http.Request) (jobRunID string, taskRunID, sourceID []string) {
jobRunID = chi.URLParam(r, "job_run_id")
if jobRunID == "" {
return
}
tID, okTID := r.URL.Query()["task_run_id"]
if okTID {
if len(tID) > 0 {
taskRunID = tID
}
}
sID, okSID := r.URL.Query()["source_id"]
if okSID {
if len(sID) > 0 {
sourceID = sID
}
}
return jobRunID, taskRunID, sourceID
}
func marshalAndWriteResponse(w http.ResponseWriter, response interface{}) (err error) {
body, err := jsoniter.Marshal(response)
if err != nil {
http.Error(w, "internal server error", http.StatusInternalServerError)
return err
}
_, err = w.Write(body)
return err
}