Commit e77b75ec6c3e2651a1a4a311a193f0b2ef8c2a8e
1 parent
ea858b8a7b
Exists in
master
and in
1 other branch
proper LIKE formating
Showing
2 changed files
with
3 additions
and
4 deletions
Show diff stats
format_utility.go
1 | package webutility | 1 | package webutility |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "time" | 4 | "time" |
5 | "fmt" | 5 | "fmt" |
6 | ) | 6 | ) |
7 | 7 | ||
8 | func UnixToDate(unix int64) time.Time { | 8 | func UnixToDate(unix int64) time.Time { |
9 | return time.Unix(unix, 0) | 9 | return time.Unix(unix, 0) |
10 | } | 10 | } |
11 | 11 | ||
12 | func DateToUnix(date interface{}) int64 { | 12 | func DateToUnix(date interface{}) int64 { |
13 | if date != nil { | 13 | if date != nil { |
14 | t := date.(time.Time) | 14 | t := date.(time.Time) |
15 | return t.Unix() | 15 | return t.Unix() |
16 | 16 | ||
17 | } | 17 | } |
18 | return 0 | 18 | return 0 |
19 | } | 19 | } |
20 | 20 | ||
21 | func EqualQuotes(stmt string) string { | 21 | func EqualQuotes(stmt string) string { |
22 | if stmt != "" { | 22 | if stmt != "" { |
23 | stmt = fmt.Sprintf(" = '%s'", stmt) | 23 | stmt = fmt.Sprintf(" = '%s'", stmt) |
24 | } | 24 | } |
25 | return stmt | 25 | return stmt |
26 | } | 26 | } |
27 | 27 | ||
28 | func LikeQuotes(stmt string) string { | 28 | func LikeQuotes(stmt string) string { |
29 | if stmt != "" { | 29 | if stmt != "" { |
30 | stmt = fmt.Sprintf(" LIKE UPPER('%%s%')", stmt) | 30 | stmt = fmt.Sprintf(" LIKE UPPER('%s%s%s')", "%", stmt, "%") |
31 | } | 31 | } |
32 | return stmt | 32 | return stmt |
33 | } | 33 | } |
34 | 34 | ||
35 | 35 |
http_utility.go
1 | package webutility | 1 | package webutility |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "net/http" | 4 | "net/http" |
5 | "encoding/json" | 5 | "encoding/json" |
6 | ) | 6 | ) |
7 | 7 | ||
8 | const templateHttpErr500_EN = "An internal server error has occurred." | 8 | const templateHttpErr500_EN = "An internal server error has occurred." |
9 | const templateHttpErr500_RS = "Došlo je do greške na serveru." | 9 | const templateHttpErr500_RS = "Došlo je do greške na serveru." |
10 | const templateHttpErr400_EN = "Bad request: invalid request body." | 10 | const templateHttpErr400_EN = "Bad request: invalid request body." |
11 | const templateHttpErr400_RS = "Neispravan zahtev." | 11 | const templateHttpErr400_RS = "Neispravan zahtev." |
12 | const templateHttpErr401_EN = "Unauthorized request." | 12 | const templateHttpErr401_EN = "Unauthorized request." |
13 | const templateHttpErr401_RS = "Neautorizovan zahtev." | 13 | const templateHttpErr401_RS = "Neautorizovan zahtev." |
14 | 14 | ||
15 | type httpError struct { | 15 | type httpError struct { |
16 | Error []HttpErrorDesc `json:"error"` | 16 | Error []HttpErrorDesc `json:"error"` |
17 | Request string `json:"request"` | 17 | Request string `json:"request"` |
18 | } | 18 | } |
19 | 19 | ||
20 | type HttpErrorDesc struct { | 20 | type HttpErrorDesc struct { |
21 | Lang string `json:"lang"` | 21 | Lang string `json:"lang"` |
22 | Desc string `json:"description"` | 22 | Desc string `json:"description"` |
23 | } | 23 | } |
24 | 24 | ||
25 | func ErrorResponse(w http.ResponseWriter, r *http.Request, code int, desc []HttpErrorDesc) { | 25 | func ErrorResponse(w http.ResponseWriter, r *http.Request, code int, desc []HttpErrorDesc) { |
26 | err := httpError{ desc, r.Method + " " + r.URL.Path } | 26 | err := httpError{ desc, r.Method + " " + r.URL.Path } |
27 | w.WriteHeader(code) | 27 | w.WriteHeader(code) |
28 | json.NewEncoder(w).Encode(err) | 28 | json.NewEncoder(w).Encode(err) |
29 | } | 29 | } |
30 | 30 | ||
31 | func BadRequestResponse(w http.ResponseWriter, req *http.Request) { | 31 | func BadRequestResponse(w http.ResponseWriter, req *http.Request) { |
32 | ErrorResponse(w, req, http.StatusBadRequest, []HttpErrorDesc{ | 32 | ErrorResponse(w, req, http.StatusBadRequest, []HttpErrorDesc{ |
33 | { "en", templateHttpErr400_EN }, | 33 | { "en", templateHttpErr400_EN }, |
34 | { "rs", templateHttpErr400_RS }, | 34 | { "rs", templateHttpErr400_RS }, |
35 | }) | 35 | }) |
36 | } | 36 | } |
37 | 37 | ||
38 | func InternalServerErrorResponse(w http.ResponseWriter, req *http.Request) { | 38 | func InternalServerErrorResponse(w http.ResponseWriter, req *http.Request) { |
39 | ErrorResponse(w, req, http.StatusInternalServerError, []HttpErrorDesc{ | 39 | ErrorResponse(w, req, http.StatusInternalServerError, []HttpErrorDesc{ |
40 | { "en", templateHttpErr500_EN }, | 40 | { "en", templateHttpErr500_EN }, |
41 | { "rs", templateHttpErr500_RS }, | 41 | { "rs", templateHttpErr500_RS }, |
42 | }) | 42 | }) |
43 | } | 43 | } |
44 | 44 | ||
45 | func UnauthorizedResponse(w http.ResponseWriter, req *http.Request) { | 45 | func UnauthorizedResponse(w http.ResponseWriter, req *http.Request) { |
46 | ErrorResponse(w, req, http.StatusUnauthorized, []HttpErrorDesc{ | 46 | ErrorResponse(w, req, http.StatusUnauthorized, []HttpErrorDesc{ |
47 | { "en", templateHttpErr500_EN }, | 47 | { "en", templateHttpErr500_EN }, |
48 | { "rs", templateHttpErr500_RS }, | 48 | { "rs", templateHttpErr500_RS }, |
49 | }) | 49 | }) |
50 | } | 50 | } |
51 | 51 | ||
52 | // TODO: Add parameters to enable/disable roles authorization checks | ||
53 | // TODO: Check for content type | 52 | // TODO: Check for content type |
54 | // Sets common headers and checks for token validity. | 53 | // Sets common headers and checks for token validity. |
55 | func WrapHandler(handlerFunc http.HandlerFunc, needauth bool) http.HandlerFunc { | 54 | func WrapHandler(handlerFunc http.HandlerFunc, auth bool) http.HandlerFunc { |
56 | return func(w http.ResponseWriter, req *http.Request) { | 55 | return func(w http.ResponseWriter, req *http.Request) { |
57 | w.Header().Set("Access-Control-Allow-Origin", "*") | 56 | w.Header().Set("Access-Control-Allow-Origin", "*") |
58 | 57 | ||
59 | w.Header().Set("Access-Control-Allow-Methods", | 58 | w.Header().Set("Access-Control-Allow-Methods", |
60 | "POST, GET, PUT, DELETE, OPTIONS") | 59 | "POST, GET, PUT, DELETE, OPTIONS") |
61 | 60 | ||
62 | w.Header().Set("Access-Control-Allow-Headers", | 61 | w.Header().Set("Access-Control-Allow-Headers", |
63 | `Accept, Content-Type, Content-Length, | 62 | `Accept, Content-Type, Content-Length, |
64 | Accept-Encoding, X-CSRF-Token, Authorization`) | 63 | Accept-Encoding, X-CSRF-Token, Authorization`) |
65 | 64 | ||
66 | w.Header().Set("Content-Type", "application/json; charset=utf-8") | 65 | w.Header().Set("Content-Type", "application/json; charset=utf-8") |
67 | 66 | ||
68 | if req.Method == "OPTIONS" { | 67 | if req.Method == "OPTIONS" { |
69 | return | 68 | return |
70 | } | 69 | } |
71 | 70 | ||
72 | if needauth { | 71 | if auth { |
73 | token := req.Header.Get("Authorization") | 72 | token := req.Header.Get("Authorization") |
74 | if _, err := ParseAPIToken(token); err != nil { | 73 | if _, err := ParseAPIToken(token); err != nil { |
75 | UnauthorizedResponse(w, req) | 74 | UnauthorizedResponse(w, req) |
76 | return | 75 | return |
77 | } | 76 | } |
78 | } | 77 | } |
79 | 78 | ||
80 | err := req.ParseForm() | 79 | err := req.ParseForm() |
81 | if err != nil { | 80 | if err != nil { |
82 | BadRequestResponse(w, req) | 81 | BadRequestResponse(w, req) |
83 | return | 82 | return |
84 | } | 83 | } |
85 | 84 | ||
86 | // execute HandlerFunc | 85 | // execute HandlerFunc |
87 | handlerFunc(w, req) | 86 | handlerFunc(w, req) |
88 | } | 87 | } |
89 | } | 88 | } |
90 | 89 | ||
91 | func NotFoundHandler(w http.ResponseWriter, req *http.Request) { | 90 | func NotFoundHandler(w http.ResponseWriter, req *http.Request) { |
92 | ErrorResponse(w, req, http.StatusNotFound, []HttpErrorDesc{ | 91 | ErrorResponse(w, req, http.StatusNotFound, []HttpErrorDesc{ |
93 | { "en", "Not found." }, | 92 | { "en", "Not found." }, |
94 | { "rs", "Traženi resurs ne postoji." }, | 93 | { "rs", "Traženi resurs ne postoji." }, |
95 | }) | 94 | }) |
96 | } | 95 | } |
97 | 96 |