Blame view
http.go
4.25 KB
ea858b8a7 refactoring |
1 |
package webutility |
64041a2ea first commit |
2 3 |
import ( |
64041a2ea first commit |
4 |
"encoding/json" |
ad8e9dd2a added middleware ... |
5 |
"fmt" |
d2ddf82ef started on new rbac |
6 |
"net/http" |
64041a2ea first commit |
7 |
) |
707782344 lint; vet |
8 |
// StatusRecorder ... |
f38e87cf4 status recorder |
9 10 |
type StatusRecorder struct { writer http.ResponseWriter |
34436d11e in-out http logs ... |
11 |
status int |
f38e87cf4 status recorder |
12 |
size int |
34436d11e in-out http logs ... |
13 |
} |
707782344 lint; vet |
14 |
// NewStatusRecorder ... |
f38e87cf4 status recorder |
15 16 17 18 19 20 |
func NewStatusRecorder(w http.ResponseWriter) *StatusRecorder { return &StatusRecorder{ writer: w, status: 0, size: 0, } |
34436d11e in-out http logs ... |
21 |
} |
707782344 lint; vet |
22 |
// WriteHeader is a wrapper http.ResponseWriter interface |
f38e87cf4 status recorder |
23 |
func (r *StatusRecorder) WriteHeader(code int) { |
34436d11e in-out http logs ... |
24 |
r.status = code |
f38e87cf4 status recorder |
25 26 |
r.writer.WriteHeader(code) } |
707782344 lint; vet |
27 |
// Write is a wrapper for http.ResponseWriter interface |
f38e87cf4 status recorder |
28 29 30 |
func (r *StatusRecorder) Write(in []byte) (int, error) { r.size = len(in) return r.writer.Write(in) |
34436d11e in-out http logs ... |
31 |
} |
707782344 lint; vet |
32 |
// Header is a wrapper for http.ResponseWriter interface |
f38e87cf4 status recorder |
33 34 35 |
func (r *StatusRecorder) Header() http.Header { return r.writer.Header() } |
707782344 lint; vet |
36 |
// Status ... |
f38e87cf4 status recorder |
37 |
func (r *StatusRecorder) Status() int { |
34436d11e in-out http logs ... |
38 39 |
return r.status } |
707782344 lint; vet |
40 |
// Size ... |
f38e87cf4 status recorder |
41 42 43 |
func (r *StatusRecorder) Size() int { return r.size } |
ad8e9dd2a added middleware ... |
44 45 |
// NotFoundHandlerFunc writes HTTP error 404 to w. func NotFoundHandlerFunc(w http.ResponseWriter, req *http.Request) { |
65d214f47 improved middlewa... |
46 47 |
SetAccessControlHeaders(w) SetContentType(w, "application/json") |
ad8e9dd2a added middleware ... |
48 |
NotFound(w, req, fmt.Sprintf("Resource you requested was not found: %s", req.URL.String())) |
9933169c8 localization support |
49 |
} |
65d214f47 improved middlewa... |
50 |
// SetContentType must be called before SetResponseStatus (w.WriteHeader) (?) |
cacf57bd4 merging with /uti... |
51 52 53 |
func SetContentType(w http.ResponseWriter, ctype string) { w.Header().Set("Content-Type", ctype) } |
46b2215eb lintfix |
54 |
// SetResponseStatus ... |
cacf57bd4 merging with /uti... |
55 56 57 |
func SetResponseStatus(w http.ResponseWriter, status int) { w.WriteHeader(status) } |
46b2215eb lintfix |
58 |
// WriteResponse ... |
cacf57bd4 merging with /uti... |
59 60 61 |
func WriteResponse(w http.ResponseWriter, content []byte) { w.Write(content) } |
65d214f47 improved middlewa... |
62 63 |
// SetAccessControlHeaders set's default headers for an HTTP response. func SetAccessControlHeaders(w http.ResponseWriter) { |
9933169c8 localization support |
64 65 |
w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS") |
34436d11e in-out http logs ... |
66 |
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") |
9933169c8 localization support |
67 |
} |
707782344 lint; vet |
68 |
// GetLocale ... |
34436d11e in-out http logs ... |
69 |
func GetLocale(req *http.Request, dflt string) string { |
a205e8f40 changes |
70 71 72 73 74 75 |
loc := req.FormValue("locale") if loc == "" { return dflt } return loc } |
707782344 lint; vet |
76 |
// Success ... |
1b7dfab73 Payload changed t... |
77 |
func Success(w http.ResponseWriter, payload interface{}, code int) { |
9933169c8 localization support |
78 79 |
w.WriteHeader(code) if payload != nil { |
1b7dfab73 Payload changed t... |
80 |
json.NewEncoder(w).Encode(payload) |
9933169c8 localization support |
81 82 |
} } |
707782344 lint; vet |
83 |
// OK ... |
1b7dfab73 Payload changed t... |
84 |
func OK(w http.ResponseWriter, payload interface{}) { |
65d214f47 improved middlewa... |
85 |
SetContentType(w, "application/json") |
9933169c8 localization support |
86 87 |
Success(w, payload, http.StatusOK) } |
707782344 lint; vet |
88 |
// Created ... |
1b7dfab73 Payload changed t... |
89 |
func Created(w http.ResponseWriter, payload interface{}) { |
65d214f47 improved middlewa... |
90 |
SetContentType(w, "application/json") |
9933169c8 localization support |
91 92 |
Success(w, payload, http.StatusCreated) } |
3fffcb954 removed old http API |
93 94 95 |
type weberror struct { Request string `json:"request"` Error string `json:"error"` |
832243a5d todo |
96 |
//Code int64 `json:"code"` TODO |
3fffcb954 removed old http API |
97 |
} |
707782344 lint; vet |
98 |
// Error ... |
9933169c8 localization support |
99 |
func Error(w http.ResponseWriter, r *http.Request, code int, err string) { |
3fffcb954 removed old http API |
100 |
werr := weberror{Error: err, Request: r.Method + " " + r.RequestURI} |
9933169c8 localization support |
101 102 103 |
w.WriteHeader(code) json.NewEncoder(w).Encode(werr) } |
707782344 lint; vet |
104 |
// BadRequest ... |
9933169c8 localization support |
105 |
func BadRequest(w http.ResponseWriter, r *http.Request, err string) { |
65d214f47 improved middlewa... |
106 |
SetContentType(w, "application/json") |
9933169c8 localization support |
107 108 |
Error(w, r, http.StatusBadRequest, err) } |
707782344 lint; vet |
109 |
// Unauthorized ... |
9933169c8 localization support |
110 |
func Unauthorized(w http.ResponseWriter, r *http.Request, err string) { |
65d214f47 improved middlewa... |
111 |
SetContentType(w, "application/json") |
9933169c8 localization support |
112 113 |
Error(w, r, http.StatusUnauthorized, err) } |
707782344 lint; vet |
114 |
// Forbidden ... |
9933169c8 localization support |
115 |
func Forbidden(w http.ResponseWriter, r *http.Request, err string) { |
65d214f47 improved middlewa... |
116 |
SetContentType(w, "application/json") |
9933169c8 localization support |
117 118 |
Error(w, r, http.StatusForbidden, err) } |
707782344 lint; vet |
119 |
// NotFound ... |
1b7dfab73 Payload changed t... |
120 |
func NotFound(w http.ResponseWriter, r *http.Request, err string) { |
65d214f47 improved middlewa... |
121 |
SetContentType(w, "application/json") |
1b7dfab73 Payload changed t... |
122 123 |
Error(w, r, http.StatusNotFound, err) } |
707782344 lint; vet |
124 |
// Conflict ... |
9933169c8 localization support |
125 |
func Conflict(w http.ResponseWriter, r *http.Request, err string) { |
65d214f47 improved middlewa... |
126 |
SetContentType(w, "application/json") |
9933169c8 localization support |
127 128 |
Error(w, r, http.StatusConflict, err) } |
707782344 lint; vet |
129 |
// InternalServerError ... |
9933169c8 localization support |
130 |
func InternalServerError(w http.ResponseWriter, r *http.Request, err string) { |
65d214f47 improved middlewa... |
131 |
SetContentType(w, "application/json") |
9933169c8 localization support |
132 133 |
Error(w, r, http.StatusInternalServerError, err) } |
3173b06a4 SplitString() |
134 135 136 137 138 139 140 141 142 143 144 145 |
func SetHeader(r *http.Request, key, val string) { r.Header.Set(key, val) } func AddHeader(r *http.Request, key, val string) { r.Header.Add(key, val) } func GetHeader(r *http.Request, key string) string { return r.Header.Get(key) } |
25b39d69c NewServer renamed... |
146 |
|
1b51eed04 pdf helper |
147 |
func ClientUTCOffset(req *http.Request) int64 { |
25b39d69c NewServer renamed... |
148 149 |
return StringToInt64(GetHeader(req, "X-Timezone-Offset")) } |