Blame view
http.go
3.72 KB
ea858b8a7 refactoring |
1 |
package webutility |
64041a2ea first commit |
2 3 |
import ( |
64041a2ea first commit |
4 |
"encoding/json" |
ad8e9dd2a added middleware ... |
5 |
"fmt" |
11933054a Added Get, Post a... |
6 |
"io" |
d2ddf82ef started on new rbac |
7 |
"net/http" |
64041a2ea first commit |
8 |
) |
707782344 lint; vet |
9 |
// StatusRecorder ... |
f38e87cf4 status recorder |
10 11 |
type StatusRecorder struct { writer http.ResponseWriter |
34436d11e in-out http logs ... |
12 |
status int |
f38e87cf4 status recorder |
13 |
size int |
34436d11e in-out http logs ... |
14 |
} |
707782344 lint; vet |
15 |
// NewStatusRecorder ... |
f38e87cf4 status recorder |
16 17 18 19 20 21 |
func NewStatusRecorder(w http.ResponseWriter) *StatusRecorder { return &StatusRecorder{ writer: w, status: 0, size: 0, } |
34436d11e in-out http logs ... |
22 |
} |
707782344 lint; vet |
23 |
// WriteHeader is a wrapper http.ResponseWriter interface |
f38e87cf4 status recorder |
24 |
func (r *StatusRecorder) WriteHeader(code int) { |
34436d11e in-out http logs ... |
25 |
r.status = code |
f38e87cf4 status recorder |
26 27 |
r.writer.WriteHeader(code) } |
707782344 lint; vet |
28 |
// Write is a wrapper for http.ResponseWriter interface |
f38e87cf4 status recorder |
29 30 31 |
func (r *StatusRecorder) Write(in []byte) (int, error) { r.size = len(in) return r.writer.Write(in) |
34436d11e in-out http logs ... |
32 |
} |
707782344 lint; vet |
33 |
// Header is a wrapper for http.ResponseWriter interface |
f38e87cf4 status recorder |
34 35 36 |
func (r *StatusRecorder) Header() http.Header { return r.writer.Header() } |
707782344 lint; vet |
37 |
// Status ... |
f38e87cf4 status recorder |
38 |
func (r *StatusRecorder) Status() int { |
34436d11e in-out http logs ... |
39 40 |
return r.status } |
707782344 lint; vet |
41 |
// Size ... |
f38e87cf4 status recorder |
42 43 44 |
func (r *StatusRecorder) Size() int { return r.size } |
ad8e9dd2a added middleware ... |
45 46 |
// NotFoundHandlerFunc writes HTTP error 404 to w. func NotFoundHandlerFunc(w http.ResponseWriter, req *http.Request) { |
9933169c8 localization support |
47 48 49 50 |
SetDefaultHeaders(w) if req.Method == "OPTIONS" { return } |
ad8e9dd2a added middleware ... |
51 |
NotFound(w, req, fmt.Sprintf("Resource you requested was not found: %s", req.URL.String())) |
9933169c8 localization support |
52 |
} |
46b2215eb lintfix |
53 |
// SetContentType ... |
cacf57bd4 merging with /uti... |
54 55 56 |
func SetContentType(w http.ResponseWriter, ctype string) { w.Header().Set("Content-Type", ctype) } |
46b2215eb lintfix |
57 |
// SetResponseStatus ... |
cacf57bd4 merging with /uti... |
58 59 60 |
func SetResponseStatus(w http.ResponseWriter, status int) { w.WriteHeader(status) } |
46b2215eb lintfix |
61 |
// WriteResponse ... |
cacf57bd4 merging with /uti... |
62 63 64 |
func WriteResponse(w http.ResponseWriter, content []byte) { w.Write(content) } |
9933169c8 localization support |
65 66 67 68 |
// 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") |
34436d11e in-out http logs ... |
69 |
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") |
cacf57bd4 merging with /uti... |
70 |
SetContentType(w, "application/json; charset=utf-8") |
9933169c8 localization support |
71 |
} |
707782344 lint; vet |
72 |
// GetLocale ... |
34436d11e in-out http logs ... |
73 |
func GetLocale(req *http.Request, dflt string) string { |
a205e8f40 changes |
74 75 76 77 78 79 |
loc := req.FormValue("locale") if loc == "" { return dflt } return loc } |
707782344 lint; vet |
80 |
// Success ... |
1b7dfab73 Payload changed t... |
81 |
func Success(w http.ResponseWriter, payload interface{}, code int) { |
9933169c8 localization support |
82 83 |
w.WriteHeader(code) if payload != nil { |
1b7dfab73 Payload changed t... |
84 |
json.NewEncoder(w).Encode(payload) |
9933169c8 localization support |
85 86 |
} } |
707782344 lint; vet |
87 |
// OK ... |
1b7dfab73 Payload changed t... |
88 |
func OK(w http.ResponseWriter, payload interface{}) { |
9933169c8 localization support |
89 90 |
Success(w, payload, http.StatusOK) } |
707782344 lint; vet |
91 |
// Created ... |
1b7dfab73 Payload changed t... |
92 |
func Created(w http.ResponseWriter, payload interface{}) { |
9933169c8 localization support |
93 94 |
Success(w, payload, http.StatusCreated) } |
3fffcb954 removed old http API |
95 96 97 98 |
type weberror struct { Request string `json:"request"` Error string `json:"error"` } |
707782344 lint; vet |
99 |
// Error ... |
9933169c8 localization support |
100 |
func Error(w http.ResponseWriter, r *http.Request, code int, err string) { |
3fffcb954 removed old http API |
101 |
werr := weberror{Error: err, Request: r.Method + " " + r.RequestURI} |
9933169c8 localization support |
102 103 104 |
w.WriteHeader(code) json.NewEncoder(w).Encode(werr) } |
707782344 lint; vet |
105 |
// BadRequest ... |
9933169c8 localization support |
106 107 108 |
func BadRequest(w http.ResponseWriter, r *http.Request, err string) { Error(w, r, http.StatusBadRequest, err) } |
707782344 lint; vet |
109 |
// Unauthorized ... |
9933169c8 localization support |
110 111 112 |
func Unauthorized(w http.ResponseWriter, r *http.Request, err string) { Error(w, r, http.StatusUnauthorized, err) } |
707782344 lint; vet |
113 |
// Forbidden ... |
9933169c8 localization support |
114 115 116 |
func Forbidden(w http.ResponseWriter, r *http.Request, err string) { Error(w, r, http.StatusForbidden, err) } |
707782344 lint; vet |
117 |
// NotFound ... |
1b7dfab73 Payload changed t... |
118 119 120 |
func NotFound(w http.ResponseWriter, r *http.Request, err string) { Error(w, r, http.StatusNotFound, err) } |
707782344 lint; vet |
121 |
// Conflict ... |
9933169c8 localization support |
122 123 124 |
func Conflict(w http.ResponseWriter, r *http.Request, err string) { Error(w, r, http.StatusConflict, err) } |
707782344 lint; vet |
125 |
// InternalServerError ... |
9933169c8 localization support |
126 127 128 |
func InternalServerError(w http.ResponseWriter, r *http.Request, err string) { Error(w, r, http.StatusInternalServerError, err) } |
46b2215eb lintfix |
129 |
|
11933054a Added Get, Post a... |
130 131 132 133 134 |
// DecodeJSON decodes JSON data from r to v. // Returns an error if it fails. func DecodeJSON(r io.Reader, v interface{}) error { return json.NewDecoder(r).Decode(v) } |