http_utility.go
2.95 KB
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
package webutility
import (
"net/http"
"encoding/json"
)
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."
const templateHttpErr401_EN = "Unauthorized request."
const templateHttpErr401_RS = "Neautorizovan zahtev."
type httpError struct {
Error []HttpErrorDesc `json:"error"`
Request string `json:"request"`
}
type HttpErrorDesc struct {
Lang string `json:"lang"`
Desc string `json:"description"`
}
// ErrorResponse writes HTTP error to w.
func ErrorResponse(w http.ResponseWriter, r *http.Request, code int, desc []HttpErrorDesc) {
err := httpError{ desc, r.Method + " " + r.URL.Path }
w.WriteHeader(code)
json.NewEncoder(w).Encode(err)
}
// BadRequestResponse writes HTTP error 400 to w.
func BadRequestResponse(w http.ResponseWriter, req *http.Request) {
ErrorResponse(w, req, http.StatusBadRequest, []HttpErrorDesc{
{ "en", templateHttpErr400_EN },
{ "rs", templateHttpErr400_RS },
})
}
// InternalSeverErrorResponse writes HTTP error 500 to w.
func InternalServerErrorResponse(w http.ResponseWriter, req *http.Request) {
ErrorResponse(w, req, http.StatusInternalServerError, []HttpErrorDesc{
{ "en", templateHttpErr500_EN },
{ "rs", templateHttpErr500_RS },
})
}
// UnauthorizedError writes HTTP error 401 to w.
func UnauthorizedResponse(w http.ResponseWriter, req *http.Request) {
ErrorResponse(w, req, http.StatusUnauthorized, []HttpErrorDesc{
{ "en", templateHttpErr401_EN },
{ "rs", templateHttpErr401_RS },
})
}
// TODO: Check for content type
// WrapHandler sets common headers, checks for token validity and performs access control checks.
// If authentication passes it calls the handlerFunc.
func WrapHandler(handlerFunc http.HandlerFunc, authorizedRoles []string) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
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 authorizedRoles != nil {
token := req.Header.Get("Authorization")
claims, err := ParseAPIToken(token);
if err != nil || !roleAuthorized(authorizedRoles, claims) {
UnauthorizedResponse(w, req)
return
}
}
err := req.ParseForm()
if err != nil {
BadRequestResponse(w, req)
return
}
// execute HandlerFunc
handlerFunc(w, req)
}
}
// NotFoundHandler writes HTTP error 404 to w.
func NotFoundHandler(w http.ResponseWriter, req *http.Request) {
ErrorResponse(w, req, http.StatusNotFound, []HttpErrorDesc{
{ "en", "Not found." },
{ "rs", "Traženi resurs ne postoji." },
})
}