Blame view
http_utility.go
2.99 KB
ea858b8a7 refactoring |
1 |
package webutility |
64041a2ea first commit |
2 3 |
import ( |
64041a2ea first commit |
4 |
"encoding/json" |
d2ddf82ef started on new rbac |
5 |
"net/http" |
64041a2ea first commit |
6 |
) |
d66628295 cleaned up |
7 8 9 10 11 |
const ( templateHttpErr500_EN = "An internal server error has occurred." templateHttpErr500_RS = "Došlo je do greške na serveru." templateHttpErr400_EN = "Bad request: invalid request body." templateHttpErr400_RS = "Neispravan zahtev." |
6620591d8 moved DeliverPayl... |
12 13 |
templateHttpErr404_EN = "Resource not found." templateHttpErr404_RS = "Objekat nije pronadjen." |
d66628295 cleaned up |
14 15 16 |
templateHttpErr401_EN = "Unauthorized request." templateHttpErr401_RS = "Neautorizovan zahtev." ) |
64041a2ea first commit |
17 |
|
33fd58161 minor changes, sh... |
18 |
type httpError struct { |
64041a2ea first commit |
19 20 21 22 23 24 25 26 |
Error []HttpErrorDesc `json:"error"` Request string `json:"request"` } type HttpErrorDesc struct { Lang string `json:"lang"` Desc string `json:"description"` } |
6620591d8 moved DeliverPayl... |
27 28 29 30 31 32 33 |
// DeliverPayload encodes payload as JSON to w. func DeliverPayload(w http.ResponseWriter, payload Payload) { // Don't write status OK in the headers here. Leave it up for the caller. // E.g. Status 201. json.NewEncoder(w).Encode(payload) payload.Data = nil } |
e1fbb41f9 added comments |
34 |
// ErrorResponse writes HTTP error to w. |
33fd58161 minor changes, sh... |
35 |
func ErrorResponse(w http.ResponseWriter, r *http.Request, code int, desc []HttpErrorDesc) { |
2d79a4120 Responses contain... |
36 |
err := httpError{desc, r.Method + " " + r.RequestURI} |
64041a2ea first commit |
37 38 39 |
w.WriteHeader(code) json.NewEncoder(w).Encode(err) } |
6620591d8 moved DeliverPayl... |
40 41 42 43 44 45 46 |
// NotFoundResponse writes HTTP error 404 to w. func NotFoundResponse(w http.ResponseWriter, req *http.Request) { ErrorResponse(w, req, http.StatusNotFound, []HttpErrorDesc{ {"en", templateHttpErr404_EN}, {"rs", templateHttpErr404_RS}, }) } |
e1fbb41f9 added comments |
47 |
// BadRequestResponse writes HTTP error 400 to w. |
33fd58161 minor changes, sh... |
48 49 |
func BadRequestResponse(w http.ResponseWriter, req *http.Request) { ErrorResponse(w, req, http.StatusBadRequest, []HttpErrorDesc{ |
d2ddf82ef started on new rbac |
50 51 |
{"en", templateHttpErr400_EN}, {"rs", templateHttpErr400_RS}, |
25e001550 exported everything |
52 |
}) |
64041a2ea first commit |
53 |
} |
e1fbb41f9 added comments |
54 |
// InternalSeverErrorResponse writes HTTP error 500 to w. |
33fd58161 minor changes, sh... |
55 56 |
func InternalServerErrorResponse(w http.ResponseWriter, req *http.Request) { ErrorResponse(w, req, http.StatusInternalServerError, []HttpErrorDesc{ |
d2ddf82ef started on new rbac |
57 58 |
{"en", templateHttpErr500_EN}, {"rs", templateHttpErr500_RS}, |
25e001550 exported everything |
59 |
}) |
64041a2ea first commit |
60 |
} |
e1fbb41f9 added comments |
61 |
// UnauthorizedError writes HTTP error 401 to w. |
33fd58161 minor changes, sh... |
62 |
func UnauthorizedResponse(w http.ResponseWriter, req *http.Request) { |
bc3671b26 refactoring token... |
63 |
w.Header().Set("WWW-Authenticate", "Bearer") |
33fd58161 minor changes, sh... |
64 |
ErrorResponse(w, req, http.StatusUnauthorized, []HttpErrorDesc{ |
d2ddf82ef started on new rbac |
65 66 |
{"en", templateHttpErr401_EN}, {"rs", templateHttpErr401_RS}, |
33fd58161 minor changes, sh... |
67 68 |
}) } |
e1fbb41f9 added comments |
69 |
// NotFoundHandler writes HTTP error 404 to w. |
25e001550 exported everything |
70 |
func NotFoundHandler(w http.ResponseWriter, req *http.Request) { |
d2ddf82ef started on new rbac |
71 72 73 74 |
SetDefaultHeaders(w) if req.Method == "OPTIONS" { return } |
33fd58161 minor changes, sh... |
75 |
ErrorResponse(w, req, http.StatusNotFound, []HttpErrorDesc{ |
d2ddf82ef started on new rbac |
76 77 |
{"en", "Not found."}, {"rs", "Traženi resurs ne postoji."}, |
25e001550 exported everything |
78 |
}) |
64041a2ea first commit |
79 |
} |
d2ddf82ef started on new rbac |
80 |
|
d66628295 cleaned up |
81 |
// SetDefaultHeaders set's default headers for an HTTP response. |
d2ddf82ef started on new rbac |
82 83 |
func SetDefaultHeaders(w http.ResponseWriter) { w.Header().Set("Access-Control-Allow-Origin", "*") |
d2ddf82ef started on new rbac |
84 |
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS") |
d2ddf82ef started on new rbac |
85 86 |
w.Header().Set("Access-Control-Allow-Headers", `Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization`) |
d2ddf82ef started on new rbac |
87 88 |
w.Header().Set("Content-Type", "application/json; charset=utf-8") } |