Blame view
http_utility.go
2.64 KB
64041a2ea first commit |
1 2 3 4 5 6 |
package restutility import ( "net/http" "encoding/json" ) |
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"` } |
33fd58161 minor changes, sh... |
23 24 |
func ErrorResponse(w http.ResponseWriter, r *http.Request, code int, desc []HttpErrorDesc) { err := httpError{ desc, r.Method + " " + r.URL.Path } |
64041a2ea first commit |
25 26 27 |
w.WriteHeader(code) json.NewEncoder(w).Encode(err) } |
33fd58161 minor changes, sh... |
28 29 30 31 |
func BadRequestResponse(w http.ResponseWriter, req *http.Request) { ErrorResponse(w, req, http.StatusBadRequest, []HttpErrorDesc{ { "en", templateHttpErr400_EN }, { "rs", templateHttpErr400_RS }, |
25e001550 exported everything |
32 |
}) |
64041a2ea first commit |
33 |
} |
33fd58161 minor changes, sh... |
34 35 36 37 |
func InternalServerErrorResponse(w http.ResponseWriter, req *http.Request) { ErrorResponse(w, req, http.StatusInternalServerError, []HttpErrorDesc{ { "en", templateHttpErr500_EN }, { "rs", templateHttpErr500_RS }, |
25e001550 exported everything |
38 |
}) |
64041a2ea first commit |
39 |
} |
33fd58161 minor changes, sh... |
40 41 42 43 44 45 46 47 48 |
func UnauthorizedResponse(w http.ResponseWriter, req *http.Request) { ErrorResponse(w, req, http.StatusUnauthorized, []HttpErrorDesc{ { "en", templateHttpErr500_EN }, { "rs", templateHttpErr500_RS }, }) } // TODO: Add parameters to enable/disable roles authorization checks // TODO: Check for content type |
64041a2ea first commit |
49 |
// Sets common headers and checks for token validity. |
33fd58161 minor changes, sh... |
50 |
func WrapHandler(handlerFunc http.HandlerFunc, needauth bool) http.HandlerFunc { |
64041a2ea first commit |
51 |
return func(w http.ResponseWriter, req *http.Request) { |
64041a2ea first commit |
52 |
w.Header().Set("Access-Control-Allow-Origin", "*") |
33fd58161 minor changes, sh... |
53 |
|
64041a2ea first commit |
54 |
w.Header().Set("Access-Control-Allow-Methods", |
33fd58161 minor changes, sh... |
55 |
"POST, GET, PUT, DELETE, OPTIONS") |
64041a2ea first commit |
56 |
w.Header().Set("Access-Control-Allow-Headers", |
33fd58161 minor changes, sh... |
57 58 |
"Accept, Content-Type, Content-Length, " "Accept-Encoding, X-CSRF-Token, Authorization") |
64041a2ea first commit |
59 60 61 62 63 |
w.Header().Set("Content-Type", "application/json; charset=utf-8") if req.Method == "OPTIONS" { return } |
33fd58161 minor changes, sh... |
64 65 66 67 68 |
if needauth { token := req.Header.Get("Authorization") if _, err := ParseAPIToken(token); err != nil { UnathorizedResponse(w, req, http.StatusUnauthorized) return |
64041a2ea first commit |
69 70 71 72 73 |
} } err := req.ParseForm() if err != nil { |
33fd58161 minor changes, sh... |
74 |
BadRequestResponse(w, req) |
64041a2ea first commit |
75 76 |
return } |
4a51e54d7 simplified |
77 78 |
// execute HandlerFunc |
90f4ed079 sped up loadTable() |
79 |
handlerFunc(w, req) |
64041a2ea first commit |
80 81 |
} } |
25e001550 exported everything |
82 |
func NotFoundHandler(w http.ResponseWriter, req *http.Request) { |
33fd58161 minor changes, sh... |
83 84 85 |
ErrorResponse(w, req, http.StatusNotFound, []HttpErrorDesc{ { "en", "Not found." }, { "rs", "Traženi resurs ne postoji." }, |
25e001550 exported everything |
86 |
}) |
64041a2ea first commit |
87 |
} |