Blame view
http_utility.go
2.54 KB
ea858b8a7 refactoring |
1 |
package webutility |
64041a2ea first commit |
2 3 4 5 6 |
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 |
func UnauthorizedResponse(w http.ResponseWriter, req *http.Request) { ErrorResponse(w, req, http.StatusUnauthorized, []HttpErrorDesc{ { "en", templateHttpErr500_EN }, { "rs", templateHttpErr500_RS }, }) } |
33fd58161 minor changes, sh... |
46 |
// TODO: Check for content type |
64041a2ea first commit |
47 |
// Sets common headers and checks for token validity. |
e77b75ec6 proper LIKE forma... |
48 |
func WrapHandler(handlerFunc http.HandlerFunc, auth bool) http.HandlerFunc { |
64041a2ea first commit |
49 |
return func(w http.ResponseWriter, req *http.Request) { |
64041a2ea first commit |
50 |
w.Header().Set("Access-Control-Allow-Origin", "*") |
33fd58161 minor changes, sh... |
51 |
|
64041a2ea first commit |
52 |
w.Header().Set("Access-Control-Allow-Methods", |
33fd58161 minor changes, sh... |
53 |
"POST, GET, PUT, DELETE, OPTIONS") |
64041a2ea first commit |
54 |
w.Header().Set("Access-Control-Allow-Headers", |
4b4ea384f hmm |
55 56 |
`Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization`) |
33fd58161 minor changes, sh... |
57 |
|
64041a2ea first commit |
58 59 60 61 62 |
w.Header().Set("Content-Type", "application/json; charset=utf-8") if req.Method == "OPTIONS" { return } |
e77b75ec6 proper LIKE forma... |
63 |
if auth { |
33fd58161 minor changes, sh... |
64 65 |
token := req.Header.Get("Authorization") if _, err := ParseAPIToken(token); err != nil { |
4b4ea384f hmm |
66 |
UnauthorizedResponse(w, req) |
33fd58161 minor changes, sh... |
67 |
return |
64041a2ea first commit |
68 69 70 71 72 |
} } err := req.ParseForm() if err != nil { |
33fd58161 minor changes, sh... |
73 |
BadRequestResponse(w, req) |
64041a2ea first commit |
74 75 |
return } |
4a51e54d7 simplified |
76 77 |
// execute HandlerFunc |
90f4ed079 sped up loadTable() |
78 |
handlerFunc(w, req) |
64041a2ea first commit |
79 80 |
} } |
25e001550 exported everything |
81 |
func NotFoundHandler(w http.ResponseWriter, req *http.Request) { |
33fd58161 minor changes, sh... |
82 83 84 |
ErrorResponse(w, req, http.StatusNotFound, []HttpErrorDesc{ { "en", "Not found." }, { "rs", "Traženi resurs ne postoji." }, |
25e001550 exported everything |
85 |
}) |
64041a2ea first commit |
86 |
} |