Blame view
http.go
2.69 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 |
) |
34436d11e in-out http logs ... |
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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 } |
ad8e9dd2a added middleware ... |
25 26 |
// NotFoundHandlerFunc writes HTTP error 404 to w. func NotFoundHandlerFunc(w http.ResponseWriter, req *http.Request) { |
9933169c8 localization support |
27 28 29 30 |
SetDefaultHeaders(w) if req.Method == "OPTIONS" { return } |
ad8e9dd2a added middleware ... |
31 |
NotFound(w, req, fmt.Sprintf("Resource you requested was not found: %s", req.URL.String())) |
9933169c8 localization support |
32 33 34 35 36 37 |
} // 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 ... |
38 |
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") |
9933169c8 localization support |
39 40 |
w.Header().Set("Content-Type", "application/json; charset=utf-8") } |
34436d11e in-out http logs ... |
41 |
func GetLocale(req *http.Request, dflt string) string { |
a205e8f40 changes |
42 43 44 45 46 47 |
loc := req.FormValue("locale") if loc == "" { return dflt } return loc } |
9933169c8 localization support |
48 |
// 2xx |
1b7dfab73 Payload changed t... |
49 |
func Success(w http.ResponseWriter, payload interface{}, code int) { |
9933169c8 localization support |
50 51 |
w.WriteHeader(code) if payload != nil { |
1b7dfab73 Payload changed t... |
52 |
json.NewEncoder(w).Encode(payload) |
9933169c8 localization support |
53 54 55 56 |
} } // 200 |
1b7dfab73 Payload changed t... |
57 |
func OK(w http.ResponseWriter, payload interface{}) { |
9933169c8 localization support |
58 59 60 61 |
Success(w, payload, http.StatusOK) } // 201 |
1b7dfab73 Payload changed t... |
62 |
func Created(w http.ResponseWriter, payload interface{}) { |
9933169c8 localization support |
63 64 |
Success(w, payload, http.StatusCreated) } |
3fffcb954 removed old http API |
65 66 67 68 |
type weberror struct { Request string `json:"request"` Error string `json:"error"` } |
9933169c8 localization support |
69 70 |
// 4xx; 5xx func Error(w http.ResponseWriter, r *http.Request, code int, err string) { |
3fffcb954 removed old http API |
71 |
werr := weberror{Error: err, Request: r.Method + " " + r.RequestURI} |
9933169c8 localization support |
72 73 74 75 76 77 78 79 |
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) } |
9933169c8 localization support |
80 81 82 83 84 85 86 87 88 |
// 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) } |
1b7dfab73 Payload changed t... |
89 90 91 92 93 94 |
// 404 func NotFound(w http.ResponseWriter, r *http.Request, err string) { Error(w, r, http.StatusNotFound, err) } // 409 |
9933169c8 localization support |
95 96 97 98 99 100 101 102 |
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) } |