Blame view
http_utility.go
3.35 KB
64041a2ea first commit |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
package restutility import ( "net/http" "encoding/json" ) //// //// 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"` } func respondWithHttpError(w http.ResponseWriter, 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) } func respondWithHttpError400(w http.ResponseWriter, req *http.Request) { respondWithHttpError(w, req, http.StatusBadRequest, []HttpErrorDesc{ { Lang: "en", Desc: templateHttpErr400_EN, }, { Lang: "rs", Desc: templateHttpErr400_RS, }, }) } func respondWithHttpError500(w http.ResponseWriter, req *http.Request) { respondWithHttpError(w, req, http.StatusInternalServerError, []HttpErrorDesc{ { Lang: "en", Desc: templateHttpErr500_EN, }, { Lang: "rs", Desc: templateHttpErr500_RS, }, }) } func deliverPayload(w http.ResponseWriter, payload JSONPayload) { json.NewEncoder(w).Encode(payload) payload.Data = nil } //// //// HANDLER FUNC WRAPPER //// // wrapHandlerFunc is as wrapper function for route handlers. // Sets common headers and checks for token validity. func commonHttpWrap(fn http.HandlerFunc) http.HandlerFunc { 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 } if req.URL.Path != APIVersion + "/token/new" { 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 } } err := req.ParseForm() if err != nil { respondWithHttpError(w, req, http.StatusBadRequest, []HttpErrorDesc{ { Lang: "en", Desc: templateHttpErr400_EN, }, { Lang: "rs", Desc: templateHttpErr400_RS, }, }) return } fn(w, req) } } //// //// NOT FOUND HANDLER //// 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.", }, }) } |