Blame view
http_utility.go
3.33 KB
64041a2ea first commit |
1 2 3 4 5 6 |
package restutility import ( "net/http" "encoding/json" ) |
4a51e54d7 simplified |
7 8 |
var _apiVersion = "" var _authEndPoint = "" |
25e001550 exported everything |
9 10 11 |
func SetApiVersion(ver string) string { _apiVersion = ver |
b291ac8c4 clened up |
12 |
return _apiVersion |
25e001550 exported everything |
13 |
} |
90fd36e9b resolved some dep... |
14 |
|
4a51e54d7 simplified |
15 16 17 |
func SetAuthEndpoint(ep string) { _authEndPoint = ep } |
64041a2ea first commit |
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
//// //// ERROR UTILITY //// 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." type HttpError struct { Error []HttpErrorDesc `json:"error"` Request string `json:"request"` } type HttpErrorDesc struct { Lang string `json:"lang"` Desc string `json:"description"` } |
25e001550 exported everything |
36 |
func RespondWithHttpError(w http.ResponseWriter, |
64041a2ea first commit |
37 38 39 40 41 42 43 44 45 46 47 |
req *http.Request, code int, httpErr []HttpErrorDesc) { err := HttpError{ Error: httpErr, Request: req.Method + " " + req.URL.Path, } w.WriteHeader(code) json.NewEncoder(w).Encode(err) } |
25e001550 exported everything |
48 49 50 51 52 |
func RespondWithHttpError400(w http.ResponseWriter, req *http.Request) { RespondWithHttpError(w, req, http.StatusBadRequest, []HttpErrorDesc{ {Lang: "en", Desc: templateHttpErr400_EN}, {Lang: "rs", Desc: templateHttpErr400_RS}, }) |
64041a2ea first commit |
53 |
} |
25e001550 exported everything |
54 55 56 57 58 |
func RespondWithHttpError500(w http.ResponseWriter, req *http.Request) { RespondWithHttpError(w, req, http.StatusInternalServerError, []HttpErrorDesc{ {Lang: "en", Desc: templateHttpErr500_EN}, {Lang: "rs", Desc: templateHttpErr500_RS}, }) |
64041a2ea first commit |
59 |
} |
64041a2ea first commit |
60 61 62 |
//// //// HANDLER FUNC WRAPPER //// |
ecec68b18 updated todo list |
63 |
//TODO: Add parameters to enable/disable token and roles authorization checks |
64041a2ea first commit |
64 |
// Sets common headers and checks for token validity. |
6f4b8a711 token response ch... |
65 |
func ProcessHeaders(fn http.HandlerFunc, authEnabled bool) http.HandlerFunc { |
64041a2ea first commit |
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
return func(w http.ResponseWriter, req *http.Request) { // @TODO: check Content-type header (must be application/json) // ctype := w.Header.Get("Content-Type") // if req.Method != "GET" && ctype != "application/json" { // replyWithHttpError(w, req, http.StatusBadRequest, // "Not a supported content type: " + ctype) // } 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") if req.Method == "OPTIONS" { return } |
6f4b8a711 token response ch... |
93 |
if authEnabled { |
4a51e54d7 simplified |
94 95 96 97 98 99 100 101 102 103 |
if req.URL.Path != _apiVersion + _authEndPoint { token := req.Header.Get("Authorization") if _, err := ParseAPIToken(token); err != nil { RespondWithHttpError(w, req, http.StatusUnauthorized, []HttpErrorDesc{ {Lang: "en", Desc: "Unauthorized request."}, {Lang: "rs", Desc: "Neautorizovani zahtev."}, }) return } |
64041a2ea first commit |
104 105 106 107 108 |
} } err := req.ParseForm() if err != nil { |
25e001550 exported everything |
109 |
RespondWithHttpError(w, req, http.StatusBadRequest, |
64041a2ea first commit |
110 |
[]HttpErrorDesc{ |
25e001550 exported everything |
111 112 |
{Lang: "en", Desc: templateHttpErr400_EN}, {Lang: "rs", Desc: templateHttpErr400_RS}, |
64041a2ea first commit |
113 114 115 |
}) return } |
4a51e54d7 simplified |
116 117 |
// execute HandlerFunc |
64041a2ea first commit |
118 119 120 121 122 123 124 |
fn(w, req) } } //// //// NOT FOUND HANDLER //// |
25e001550 exported everything |
125 126 127 128 129 |
func NotFoundHandler(w http.ResponseWriter, req *http.Request) { RespondWithHttpError(w, req, http.StatusNotFound, []HttpErrorDesc{ {Lang: "en", Desc: "Not found."}, {Lang: "rs", Desc: "Traženi resurs ne postoji."}, }) |
64041a2ea first commit |
130 |
} |