Blame view
http_utility.go
2.3 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 |
) |
64041a2ea first commit |
7 8 9 10 |
const templateHttpErr500_EN = "An internal server error has occurred." const templateHttpErr500_RS = "Došlo je do greške na serveru." const templateHttpErr400_EN = "Bad request: invalid request body." const templateHttpErr400_RS = "Neispravan zahtev." |
33fd58161 minor changes, sh... |
11 12 |
const templateHttpErr401_EN = "Unauthorized request." const templateHttpErr401_RS = "Neautorizovan zahtev." |
64041a2ea first commit |
13 |
|
33fd58161 minor changes, sh... |
14 |
type httpError struct { |
64041a2ea first commit |
15 16 17 18 19 20 21 22 |
Error []HttpErrorDesc `json:"error"` Request string `json:"request"` } type HttpErrorDesc struct { Lang string `json:"lang"` Desc string `json:"description"` } |
e1fbb41f9 added comments |
23 |
// ErrorResponse writes HTTP error to w. |
33fd58161 minor changes, sh... |
24 |
func ErrorResponse(w http.ResponseWriter, r *http.Request, code int, desc []HttpErrorDesc) { |
d2ddf82ef started on new rbac |
25 |
err := httpError{desc, r.Method + " " + r.URL.Path} |
64041a2ea first commit |
26 27 28 |
w.WriteHeader(code) json.NewEncoder(w).Encode(err) } |
e1fbb41f9 added comments |
29 |
// BadRequestResponse writes HTTP error 400 to w. |
33fd58161 minor changes, sh... |
30 31 |
func BadRequestResponse(w http.ResponseWriter, req *http.Request) { ErrorResponse(w, req, http.StatusBadRequest, []HttpErrorDesc{ |
d2ddf82ef started on new rbac |
32 33 |
{"en", templateHttpErr400_EN}, {"rs", templateHttpErr400_RS}, |
25e001550 exported everything |
34 |
}) |
64041a2ea first commit |
35 |
} |
e1fbb41f9 added comments |
36 |
// InternalSeverErrorResponse writes HTTP error 500 to w. |
33fd58161 minor changes, sh... |
37 38 |
func InternalServerErrorResponse(w http.ResponseWriter, req *http.Request) { ErrorResponse(w, req, http.StatusInternalServerError, []HttpErrorDesc{ |
d2ddf82ef started on new rbac |
39 40 |
{"en", templateHttpErr500_EN}, {"rs", templateHttpErr500_RS}, |
25e001550 exported everything |
41 |
}) |
64041a2ea first commit |
42 |
} |
e1fbb41f9 added comments |
43 |
// UnauthorizedError writes HTTP error 401 to w. |
33fd58161 minor changes, sh... |
44 45 |
func UnauthorizedResponse(w http.ResponseWriter, req *http.Request) { ErrorResponse(w, req, http.StatusUnauthorized, []HttpErrorDesc{ |
d2ddf82ef started on new rbac |
46 47 |
{"en", templateHttpErr401_EN}, {"rs", templateHttpErr401_RS}, |
33fd58161 minor changes, sh... |
48 49 |
}) } |
e1fbb41f9 added comments |
50 |
// NotFoundHandler writes HTTP error 404 to w. |
25e001550 exported everything |
51 |
func NotFoundHandler(w http.ResponseWriter, req *http.Request) { |
d2ddf82ef started on new rbac |
52 53 54 55 |
SetDefaultHeaders(w) if req.Method == "OPTIONS" { return } |
33fd58161 minor changes, sh... |
56 |
ErrorResponse(w, req, http.StatusNotFound, []HttpErrorDesc{ |
d2ddf82ef started on new rbac |
57 58 |
{"en", "Not found."}, {"rs", "Traženi resurs ne postoji."}, |
25e001550 exported everything |
59 |
}) |
64041a2ea first commit |
60 |
} |
d2ddf82ef started on new rbac |
61 62 63 64 65 66 67 68 69 70 71 |
func SetDefaultHeaders(w http.ResponseWriter) { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS") w.Header().Set("Access-Control-Allow-Headers", `Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization`) w.Header().Set("Content-Type", "application/json; charset=utf-8") } |