diff --git a/auth_utility.go b/auth_utility.go index 3fc4939..c70a893 100644 --- a/auth_utility.go +++ b/auth_utility.go @@ -25,13 +25,15 @@ type Role struct { // TokenClaims are JWT token claims. type TokenClaims struct { - Token string `json:"access_token"` - TokenType string `json:"token_type"` - Username string `json:"username"` - Role string `json:"role"` - RoleID uint32 `json:"role_id"` - ExpiresIn int64 `json:"expires_in"` - jwt.StandardClaims // extending a struct + Token string `json:"access_token"` + TokenType string `json:"token_type"` + Username string `json:"username"` + Role string `json:"role"` + RoleID uint32 `json:"role_id"` + ExpiresIn int64 `json:"expires_in"` + + // extending a struct + jwt.StandardClaims } // CredentialsStruct is an instace of username/password values. diff --git a/http_utility.go b/http_utility.go index e1f1328..99aa827 100644 --- a/http_utility.go +++ b/http_utility.go @@ -10,6 +10,8 @@ const ( templateHttpErr500_RS = "Došlo je do greške na serveru." templateHttpErr400_EN = "Bad request: invalid request body." templateHttpErr400_RS = "Neispravan zahtev." + templateHttpErr404_EN = "Resource not found." + templateHttpErr404_RS = "Objekat nije pronadjen." templateHttpErr401_EN = "Unauthorized request." templateHttpErr401_RS = "Neautorizovan zahtev." ) @@ -24,14 +26,29 @@ type HttpErrorDesc struct { Desc string `json:"description"` } +// DeliverPayload encodes payload as JSON to w. +func DeliverPayload(w http.ResponseWriter, payload Payload) { + // Don't write status OK in the headers here. Leave it up for the caller. + // E.g. Status 201. + json.NewEncoder(w).Encode(payload) + payload.Data = nil +} + // 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} err := httpError{desc, r.Method + " " + r.RequestURI} w.WriteHeader(code) json.NewEncoder(w).Encode(err) } +// NotFoundResponse writes HTTP error 404 to w. +func NotFoundResponse(w http.ResponseWriter, req *http.Request) { + ErrorResponse(w, req, http.StatusNotFound, []HttpErrorDesc{ + {"en", templateHttpErr404_EN}, + {"rs", templateHttpErr404_RS}, + }) +} + // BadRequestResponse writes HTTP error 400 to w. func BadRequestResponse(w http.ResponseWriter, req *http.Request) { ErrorResponse(w, req, http.StatusBadRequest, []HttpErrorDesc{ diff --git a/json_utility.go b/json_utility.go index 515a5dc..80a733a 100644 --- a/json_utility.go +++ b/json_utility.go @@ -88,12 +88,6 @@ func NewPayload(r *http.Request, table string) Payload { return pload } -// DeliverPayload encodes payload to w. -func DeliverPayload(w http.ResponseWriter, payload Payload) { - json.NewEncoder(w).Encode(payload) - payload.Data = nil -} - // translations returns a slice of translations for a payload/table of ptype type. func translations(ptype string) []Translation { var translations []Translation