-
Notifications
You must be signed in to change notification settings - Fork 2
/
responses.go
37 lines (31 loc) · 1.05 KB
/
responses.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
package clover
import (
"context"
"encoding/json"
"net/http"
)
// errorResponse is our response payload for an error
type errorResponse struct {
Message string `json:"message"`
Error string `json:"error"`
}
// dataResponse is our payload for data responses
type dataResponse struct {
Message string `json:"message"`
Data interface{} `json:"data"`
}
func writeDataResponse(ctx context.Context, w http.ResponseWriter, statusCode int, message string, data interface{}) error {
return writeJSONResponse(ctx, w, statusCode, dataResponse{message, data})
}
func writeErrorResponse(ctx context.Context, w http.ResponseWriter, statusCode int, message string, err error) error {
errorResponse := errorResponse{
Message: message,
Error: err.Error(),
}
return writeJSONResponse(ctx, w, statusCode, errorResponse)
}
func writeJSONResponse(ctx context.Context, w http.ResponseWriter, statusCode int, response interface{}) error {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
return json.NewEncoder(w).Encode(response)
}