-
Notifications
You must be signed in to change notification settings - Fork 2
/
resp.go
100 lines (88 loc) · 2.8 KB
/
resp.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
package http_server
import (
"encoding/json"
"fmt"
"net/http"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
)
type HTTPResponse struct {
StatusCode int `json:"statusCode"`
Header http.Header `json:"header"`
Content []byte `json:"content"`
}
// SendResponse forwards a response to the client
func SendResponse(w http.ResponseWriter, resp HTTPResponse) {
for key, values := range resp.Header {
for _, v := range values {
w.Header().Add(key, v)
}
}
w.WriteHeader(resp.StatusCode)
_, err := w.Write(resp.Content)
if err != nil {
log.Errorf("unable to write response: %s", err)
}
}
type errorLog struct {
Uid uuid.UUID `json:"uuid,omitempty"`
Path string `json:"path,omitempty"`
Error string `json:"error,omitempty"`
Status string `json:"status,omitempty"`
}
// ClientError is a wrapper for http.Error that additionally logs uuid, request URL path, error message and status
// with logging level "warning"
func ClientError(uid uuid.UUID, r *http.Request, w http.ResponseWriter, errMsg string, code int) {
errLog, _ := json.Marshal(errorLog{
Uid: uid,
Path: r.URL.Path,
Error: errMsg,
Status: fmt.Sprintf("%d %s", code, http.StatusText(code)),
})
log.Warnf("ClientError: %s", errLog)
http.Error(w, errMsg, code)
}
// ServerError is a wrapper for http.Error that additionally logs uuid, request URL path, error message and status
// with logging level "error". The error message is not sent to the client.
func ServerError(uid uuid.UUID, r *http.Request, w http.ResponseWriter, errMsg string, code int) {
errLog, _ := json.Marshal(errorLog{
Uid: uid,
Path: r.URL.Path,
Error: errMsg,
Status: fmt.Sprintf("%d %s", code, http.StatusText(code)),
})
log.Errorf("ServerError: %s", errLog)
http.Error(w, http.StatusText(code), code)
}
// Health is a liveness probe.
func Health(server string) http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Server", server)
w.Header().Set("Content-Type", TextType)
w.WriteHeader(http.StatusOK)
_, err := w.Write(append([]byte(http.StatusText(http.StatusOK)), '\n'))
if err != nil {
log.Errorf("unable to write response: %s", err)
}
}
}
// Ready is a readiness probe.
func Ready(server string, readinessChecks []func() error) http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
status := http.StatusOK
for _, isReady := range readinessChecks {
if err := isReady(); err != nil {
log.Warnf("readiness probe failed: %v", err)
status = http.StatusServiceUnavailable
break
}
}
w.Header().Set("Server", server)
w.Header().Set("Content-Type", TextType)
w.WriteHeader(status)
_, err := w.Write(append([]byte(http.StatusText(status)), '\n'))
if err != nil {
log.Errorf("unable to write response: %s", err)
}
}
}