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