http.go
2.69 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
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
package webutility
import (
"encoding/json"
"fmt"
"net/http"
)
type ResponseWriterStatusRecorder struct {
http.ResponseWriter
status int
}
func WrapWithStatusRecorder(w http.ResponseWriter) *ResponseWriterStatusRecorder {
return &ResponseWriterStatusRecorder{w, 0}
}
func (r *ResponseWriterStatusRecorder) WriteHeader(code int) {
r.status = code
r.ResponseWriter.WriteHeader(code)
}
func (r *ResponseWriterStatusRecorder) Status() int {
return r.status
}
// NotFoundHandlerFunc writes HTTP error 404 to w.
func NotFoundHandlerFunc(w http.ResponseWriter, req *http.Request) {
SetDefaultHeaders(w)
if req.Method == "OPTIONS" {
return
}
NotFound(w, req, fmt.Sprintf("Resource you requested was not found: %s", req.URL.String()))
}
// SetDefaultHeaders set's default headers for an HTTP response.
func SetDefaultHeaders(w http.ResponseWriter) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
}
func GetLocale(req *http.Request, dflt string) string {
loc := req.FormValue("locale")
if loc == "" {
return dflt
}
return loc
}
// 2xx
func Success(w http.ResponseWriter, payload interface{}, code int) {
w.WriteHeader(code)
if payload != nil {
json.NewEncoder(w).Encode(payload)
}
}
// 200
func OK(w http.ResponseWriter, payload interface{}) {
Success(w, payload, http.StatusOK)
}
// 201
func Created(w http.ResponseWriter, payload interface{}) {
Success(w, payload, http.StatusCreated)
}
type weberror struct {
Request string `json:"request"`
Error string `json:"error"`
}
// 4xx; 5xx
func Error(w http.ResponseWriter, r *http.Request, code int, err string) {
werr := weberror{Error: err, Request: r.Method + " " + r.RequestURI}
w.WriteHeader(code)
json.NewEncoder(w).Encode(werr)
}
// 400
func BadRequest(w http.ResponseWriter, r *http.Request, err string) {
Error(w, r, http.StatusBadRequest, err)
}
// 401
func Unauthorized(w http.ResponseWriter, r *http.Request, err string) {
Error(w, r, http.StatusUnauthorized, err)
}
// 403
func Forbidden(w http.ResponseWriter, r *http.Request, err string) {
Error(w, r, http.StatusForbidden, err)
}
// 404
func NotFound(w http.ResponseWriter, r *http.Request, err string) {
Error(w, r, http.StatusNotFound, err)
}
// 409
func Conflict(w http.ResponseWriter, r *http.Request, err string) {
Error(w, r, http.StatusConflict, err)
}
// 500
func InternalServerError(w http.ResponseWriter, r *http.Request, err string) {
Error(w, r, http.StatusInternalServerError, err)
}