-
Notifications
You must be signed in to change notification settings - Fork 4
/
httputils.go
70 lines (57 loc) · 1.58 KB
/
httputils.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
package main
import (
"encoding/json"
"log"
"net/http"
)
// APIResponseBase contains the basic fields of a response from the APIs.
type APIResponseBase struct {
Success bool `json:"success"`
Status int `json:"status"`
Log string `json:"log,omitempty"`
User string `json:"user"`
}
// GetStatus returns the status of the response.
func (r *APIResponseBase) GetStatus() int {
return r.Status
}
// GetUser returns the user in the response object.
func (r *APIResponseBase) GetUser() string {
return r.User
}
// SetUser sets the user in the response object.
func (r *APIResponseBase) SetUser(user string) {
r.User = user
}
// APIResponse represents the response to an API call.
type APIResponse interface {
GetStatus() int
GetUser() string
SetUser(string)
}
// respond response returns a JSON response to the client.
func (s *server) respond(w http.ResponseWriter, r *http.Request, resp APIResponse) {
if resp.GetUser() == "" && r.URL != nil && r.URL.User != nil {
resp.SetUser(r.URL.User.Username())
}
status := resp.GetStatus()
if status == 0 {
status = http.StatusOK
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(status)
encoder := json.NewEncoder(w)
err := encoder.Encode(resp)
if err != nil {
log.Printf("error writing response: %v", err)
}
}
// error servers an error response to the client.
func (s *server) error(w http.ResponseWriter, r *http.Request, err error) {
log.Printf("returning error response: %v", err)
s.respond(w, r, &APIResponseBase{
Success: false,
Status: http.StatusInternalServerError,
Log: err.Error(),
})
}