-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp.go
More file actions
executable file
·39 lines (33 loc) · 1.21 KB
/
http.go
File metadata and controls
executable file
·39 lines (33 loc) · 1.21 KB
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
package helpers
import (
"context"
"encoding/json"
"errors"
"net/http"
)
var Response = &response{}
type response struct {
}
// SendOkayResponse sends an Okay http response
func (r *response) SendOkayResponse(ctx context.Context, statusCode int, w http.ResponseWriter) error {
return r.SendResponse(ctx, w, statusCode, map[string]string{})
}
// SendErrorResponse sends an Error http response
func (r *response) SendErrorResponse(ctx context.Context, w http.ResponseWriter, statusCode int, err error) error {
value, ok := err.(Error)
if ok {
return r.SendResponse(ctx, w, statusCode, map[string]string{"error": value.Error(), "rawError": value.RawError()})
}
if err == nil {
err = errors.New("")
}
return r.SendResponse(ctx, w, statusCode, map[string]string{"error": err.Error(), "rawError": ""})
}
// SendResponse sends an http response
func (r *response) SendResponse(ctx context.Context, w http.ResponseWriter, statusCode int, body interface{}) error {
w.Header().Set("Cache-Control", "no-store")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
Logger.LogInfo(GetRequestID(ctx), "Response", map[string]interface{}{"statusCode": statusCode})
return json.NewEncoder(w).Encode(body)
}