Commit 6620591d867ec0a4fc4a3dcc5ed206061d829b1c

Authored by Marko Tikvić
1 parent 62a69bedaa
Exists in master and in 1 other branch v2

moved DeliverPaylaod to http_utility

... ... @@ -25,13 +25,15 @@ type Role struct {
25 25  
26 26 // TokenClaims are JWT token claims.
27 27 type TokenClaims struct {
28   - Token string `json:"access_token"`
29   - TokenType string `json:"token_type"`
30   - Username string `json:"username"`
31   - Role string `json:"role"`
32   - RoleID uint32 `json:"role_id"`
33   - ExpiresIn int64 `json:"expires_in"`
34   - jwt.StandardClaims // extending a struct
  28 + Token string `json:"access_token"`
  29 + TokenType string `json:"token_type"`
  30 + Username string `json:"username"`
  31 + Role string `json:"role"`
  32 + RoleID uint32 `json:"role_id"`
  33 + ExpiresIn int64 `json:"expires_in"`
  34 +
  35 + // extending a struct
  36 + jwt.StandardClaims
35 37 }
36 38  
37 39 // CredentialsStruct is an instace of username/password values.
... ...
... ... @@ -10,6 +10,8 @@ const (
10 10 templateHttpErr500_RS = "Došlo je do greške na serveru."
11 11 templateHttpErr400_EN = "Bad request: invalid request body."
12 12 templateHttpErr400_RS = "Neispravan zahtev."
  13 + templateHttpErr404_EN = "Resource not found."
  14 + templateHttpErr404_RS = "Objekat nije pronadjen."
13 15 templateHttpErr401_EN = "Unauthorized request."
14 16 templateHttpErr401_RS = "Neautorizovan zahtev."
15 17 )
... ... @@ -24,14 +26,29 @@ type HttpErrorDesc struct {
24 26 Desc string `json:"description"`
25 27 }
26 28  
  29 +// DeliverPayload encodes payload as JSON to w.
  30 +func DeliverPayload(w http.ResponseWriter, payload Payload) {
  31 + // Don't write status OK in the headers here. Leave it up for the caller.
  32 + // E.g. Status 201.
  33 + json.NewEncoder(w).Encode(payload)
  34 + payload.Data = nil
  35 +}
  36 +
27 37 // ErrorResponse writes HTTP error to w.
28 38 func ErrorResponse(w http.ResponseWriter, r *http.Request, code int, desc []HttpErrorDesc) {
29   - //err := httpError{desc, r.Method + " " + r.URL.Path}
30 39 err := httpError{desc, r.Method + " " + r.RequestURI}
31 40 w.WriteHeader(code)
32 41 json.NewEncoder(w).Encode(err)
33 42 }
34 43  
  44 +// NotFoundResponse writes HTTP error 404 to w.
  45 +func NotFoundResponse(w http.ResponseWriter, req *http.Request) {
  46 + ErrorResponse(w, req, http.StatusNotFound, []HttpErrorDesc{
  47 + {"en", templateHttpErr404_EN},
  48 + {"rs", templateHttpErr404_RS},
  49 + })
  50 +}
  51 +
35 52 // BadRequestResponse writes HTTP error 400 to w.
36 53 func BadRequestResponse(w http.ResponseWriter, req *http.Request) {
37 54 ErrorResponse(w, req, http.StatusBadRequest, []HttpErrorDesc{
... ...
... ... @@ -88,12 +88,6 @@ func NewPayload(r *http.Request, table string) Payload {
88 88 return pload
89 89 }
90 90  
91   -// DeliverPayload encodes payload to w.
92   -func DeliverPayload(w http.ResponseWriter, payload Payload) {
93   - json.NewEncoder(w).Encode(payload)
94   - payload.Data = nil
95   -}
96   -
97 91 // translations returns a slice of translations for a payload/table of ptype type.
98 92 func translations(ptype string) []Translation {
99 93 var translations []Translation
... ...