Commit 68d4dbbd12824698b8503b1a445ba2addb5cf5de

Authored by Marko Tikvić
1 parent 5f1346d2d7
Exists in master and in 1 other branch v2

http pre proc

Showing 1 changed file with 1 additions and 1 deletions   Show diff stats
1 package restutility 1 package restutility
2 2
3 import ( 3 import (
4 "net/http" 4 "net/http"
5 "encoding/json" 5 "encoding/json"
6 ) 6 )
7 7
8 var _apiVersion = "/api/v1" 8 var _apiVersion = "/api/v1"
9 var _authEndPoint = "/token" 9 var _authEndPoint = "/token"
10 10
11 func SetApiVersion(ver string) string { 11 func SetApiVersion(ver string) string {
12 _apiVersion = ver 12 _apiVersion = ver
13 return _apiVersion 13 return _apiVersion
14 } 14 }
15 15
16 func SetAuthEndpoint(ep string) { 16 func SetAuthEndpoint(ep string) {
17 _authEndPoint = ep 17 _authEndPoint = ep
18 } 18 }
19 19
20 const templateHttpErr500_EN = "An internal server error has occurred." 20 const templateHttpErr500_EN = "An internal server error has occurred."
21 const templateHttpErr500_RS = "Došlo je do greške na serveru." 21 const templateHttpErr500_RS = "Došlo je do greške na serveru."
22 const templateHttpErr400_EN = "Bad request: invalid request body." 22 const templateHttpErr400_EN = "Bad request: invalid request body."
23 const templateHttpErr400_RS = "Neispravan zahtev." 23 const templateHttpErr400_RS = "Neispravan zahtev."
24 24
25 type HttpError struct { 25 type HttpError struct {
26 Error []HttpErrorDesc `json:"error"` 26 Error []HttpErrorDesc `json:"error"`
27 Request string `json:"request"` 27 Request string `json:"request"`
28 } 28 }
29 29
30 type HttpErrorDesc struct { 30 type HttpErrorDesc struct {
31 Lang string `json:"lang"` 31 Lang string `json:"lang"`
32 Desc string `json:"description"` 32 Desc string `json:"description"`
33 } 33 }
34 34
35 func RespondWithHttpError(w http.ResponseWriter, 35 func RespondWithHttpError(w http.ResponseWriter,
36 req *http.Request, 36 req *http.Request,
37 code int, 37 code int,
38 httpErr []HttpErrorDesc) { 38 httpErr []HttpErrorDesc) {
39 39
40 err := HttpError{ 40 err := HttpError{
41 Error: httpErr, 41 Error: httpErr,
42 Request: req.Method + " " + req.URL.Path, 42 Request: req.Method + " " + req.URL.Path,
43 } 43 }
44 w.WriteHeader(code) 44 w.WriteHeader(code)
45 json.NewEncoder(w).Encode(err) 45 json.NewEncoder(w).Encode(err)
46 } 46 }
47 47
48 func RespondWithHttpError400(w http.ResponseWriter, req *http.Request) { 48 func RespondWithHttpError400(w http.ResponseWriter, req *http.Request) {
49 RespondWithHttpError(w, req, http.StatusBadRequest, []HttpErrorDesc{ 49 RespondWithHttpError(w, req, http.StatusBadRequest, []HttpErrorDesc{
50 {Lang: "en", Desc: templateHttpErr400_EN}, 50 {Lang: "en", Desc: templateHttpErr400_EN},
51 {Lang: "rs", Desc: templateHttpErr400_RS}, 51 {Lang: "rs", Desc: templateHttpErr400_RS},
52 }) 52 })
53 } 53 }
54 54
55 func RespondWithHttpError500(w http.ResponseWriter, req *http.Request) { 55 func RespondWithHttpError500(w http.ResponseWriter, req *http.Request) {
56 RespondWithHttpError(w, req, http.StatusInternalServerError, []HttpErrorDesc{ 56 RespondWithHttpError(w, req, http.StatusInternalServerError, []HttpErrorDesc{
57 {Lang: "en", Desc: templateHttpErr500_EN}, 57 {Lang: "en", Desc: templateHttpErr500_EN},
58 {Lang: "rs", Desc: templateHttpErr500_RS}, 58 {Lang: "rs", Desc: templateHttpErr500_RS},
59 }) 59 })
60 } 60 }
61 61
62 //TODO: Add parameters to enable/disable roles authorization checks 62 //TODO: Add parameters to enable/disable roles authorization checks
63 // Sets common headers and checks for token validity. 63 // Sets common headers and checks for token validity.
64 func ProcessHeaders(handlerFunc http.HandlerFunc, authEnabled bool) http.HandlerFunc { 64 func HttpPreProc(handlerFunc http.HandlerFunc, authEnabled bool) http.HandlerFunc {
65 return func(w http.ResponseWriter, req *http.Request) { 65 return func(w http.ResponseWriter, req *http.Request) {
66 // @TODO: check Content-type header (must be application/json) 66 // @TODO: check Content-type header (must be application/json)
67 // ctype := w.Header.Get("Content-Type") 67 // ctype := w.Header.Get("Content-Type")
68 // if req.Method != "GET" && ctype != "application/json" { 68 // if req.Method != "GET" && ctype != "application/json" {
69 // replyWithHttpError(w, req, http.StatusBadRequest, 69 // replyWithHttpError(w, req, http.StatusBadRequest,
70 // "Not a supported content type: " + ctype) 70 // "Not a supported content type: " + ctype)
71 // } 71 // }
72 72
73 w.Header().Set("Access-Control-Allow-Origin", "*") 73 w.Header().Set("Access-Control-Allow-Origin", "*")
74 w.Header().Set("Access-Control-Allow-Methods", 74 w.Header().Set("Access-Control-Allow-Methods",
75 `POST, 75 `POST,
76 GET, 76 GET,
77 PUT, 77 PUT,
78 DELETE, 78 DELETE,
79 OPTIONS`) 79 OPTIONS`)
80 w.Header().Set("Access-Control-Allow-Headers", 80 w.Header().Set("Access-Control-Allow-Headers",
81 `Accept, 81 `Accept,
82 Content-Type, 82 Content-Type,
83 Content-Length, 83 Content-Length,
84 Accept-Encoding, 84 Accept-Encoding,
85 X-CSRF-Token, 85 X-CSRF-Token,
86 Authorization`) 86 Authorization`)
87 w.Header().Set("Content-Type", "application/json; charset=utf-8") 87 w.Header().Set("Content-Type", "application/json; charset=utf-8")
88 88
89 if req.Method == "OPTIONS" { 89 if req.Method == "OPTIONS" {
90 return 90 return
91 } 91 }
92 92
93 if authEnabled { 93 if authEnabled {
94 if req.URL.Path != _apiVersion + _authEndPoint { 94 if req.URL.Path != _apiVersion + _authEndPoint {
95 token := req.Header.Get("Authorization") 95 token := req.Header.Get("Authorization")
96 if _, err := ParseAPIToken(token); err != nil { 96 if _, err := ParseAPIToken(token); err != nil {
97 RespondWithHttpError(w, req, http.StatusUnauthorized, 97 RespondWithHttpError(w, req, http.StatusUnauthorized,
98 []HttpErrorDesc{ 98 []HttpErrorDesc{
99 {Lang: "en", Desc: "Unauthorized request."}, 99 {Lang: "en", Desc: "Unauthorized request."},
100 {Lang: "rs", Desc: "Neautorizovani zahtev."}, 100 {Lang: "rs", Desc: "Neautorizovani zahtev."},
101 }) 101 })
102 return 102 return
103 } 103 }
104 } 104 }
105 } 105 }
106 106
107 err := req.ParseForm() 107 err := req.ParseForm()
108 if err != nil { 108 if err != nil {
109 RespondWithHttpError400(w, req) 109 RespondWithHttpError400(w, req)
110 return 110 return
111 } 111 }
112 112
113 // execute HandlerFunc 113 // execute HandlerFunc
114 handlerFunc(w, req) 114 handlerFunc(w, req)
115 } 115 }
116 } 116 }
117 117
118 func NotFoundHandler(w http.ResponseWriter, req *http.Request) { 118 func NotFoundHandler(w http.ResponseWriter, req *http.Request) {
119 RespondWithHttpError(w, req, http.StatusNotFound, []HttpErrorDesc{ 119 RespondWithHttpError(w, req, http.StatusNotFound, []HttpErrorDesc{
120 {Lang: "en", Desc: "Not found."}, 120 {Lang: "en", Desc: "Not found."},
121 {Lang: "rs", Desc: "Traženi resurs ne postoji."}, 121 {Lang: "rs", Desc: "Traženi resurs ne postoji."},
122 }) 122 })
123 } 123 }
124 124