Blame view

http_utility.go 2.45 KB
ea858b8a7   Marko Tikvić   refactoring
1
  package webutility
64041a2ea   Marko Tikvić   first commit
2
3
  
  import (
64041a2ea   Marko Tikvić   first commit
4
  	"encoding/json"
d2ddf82ef   Marko Tikvić   started on new rbac
5
  	"net/http"
64041a2ea   Marko Tikvić   first commit
6
  )
d66628295   Marko Tikvić   cleaned up
7
8
9
10
11
12
13
14
  const (
  	templateHttpErr500_EN = "An internal server error has occurred."
  	templateHttpErr500_RS = "Došlo je do greške na serveru."
  	templateHttpErr400_EN = "Bad request: invalid request body."
  	templateHttpErr400_RS = "Neispravan zahtev."
  	templateHttpErr401_EN = "Unauthorized request."
  	templateHttpErr401_RS = "Neautorizovan zahtev."
  )
64041a2ea   Marko Tikvić   first commit
15

33fd58161   markotikvic   minor changes, sh...
16
  type httpError struct {
64041a2ea   Marko Tikvić   first commit
17
18
19
20
21
22
23
24
  	Error   []HttpErrorDesc `json:"error"`
  	Request string          `json:"request"`
  }
  
  type HttpErrorDesc struct {
  	Lang string `json:"lang"`
  	Desc string `json:"description"`
  }
e1fbb41f9   Marko Tikvić   added comments
25
  // ErrorResponse writes HTTP error to w.
33fd58161   markotikvic   minor changes, sh...
26
  func ErrorResponse(w http.ResponseWriter, r *http.Request, code int, desc []HttpErrorDesc) {
2d79a4120   Marko Tikvić   Responses contain...
27
28
  	//err := httpError{desc, r.Method + " " + r.URL.Path}
  	err := httpError{desc, r.Method + " " + r.RequestURI}
64041a2ea   Marko Tikvić   first commit
29
30
31
  	w.WriteHeader(code)
  	json.NewEncoder(w).Encode(err)
  }
e1fbb41f9   Marko Tikvić   added comments
32
  // BadRequestResponse writes HTTP error 400 to w.
33fd58161   markotikvic   minor changes, sh...
33
34
  func BadRequestResponse(w http.ResponseWriter, req *http.Request) {
  	ErrorResponse(w, req, http.StatusBadRequest, []HttpErrorDesc{
d2ddf82ef   Marko Tikvić   started on new rbac
35
36
  		{"en", templateHttpErr400_EN},
  		{"rs", templateHttpErr400_RS},
25e001550   Marko Tikvić   exported everything
37
  	})
64041a2ea   Marko Tikvić   first commit
38
  }
e1fbb41f9   Marko Tikvić   added comments
39
  // InternalSeverErrorResponse writes HTTP error 500 to w.
33fd58161   markotikvic   minor changes, sh...
40
41
  func InternalServerErrorResponse(w http.ResponseWriter, req *http.Request) {
  	ErrorResponse(w, req, http.StatusInternalServerError, []HttpErrorDesc{
d2ddf82ef   Marko Tikvić   started on new rbac
42
43
  		{"en", templateHttpErr500_EN},
  		{"rs", templateHttpErr500_RS},
25e001550   Marko Tikvić   exported everything
44
  	})
64041a2ea   Marko Tikvić   first commit
45
  }
e1fbb41f9   Marko Tikvić   added comments
46
  // UnauthorizedError writes HTTP error 401 to w.
33fd58161   markotikvic   minor changes, sh...
47
  func UnauthorizedResponse(w http.ResponseWriter, req *http.Request) {
bc3671b26   Marko Tikvić   refactoring token...
48
  	w.Header().Set("WWW-Authenticate", "Bearer")
33fd58161   markotikvic   minor changes, sh...
49
  	ErrorResponse(w, req, http.StatusUnauthorized, []HttpErrorDesc{
d2ddf82ef   Marko Tikvić   started on new rbac
50
51
  		{"en", templateHttpErr401_EN},
  		{"rs", templateHttpErr401_RS},
33fd58161   markotikvic   minor changes, sh...
52
53
  	})
  }
e1fbb41f9   Marko Tikvić   added comments
54
  // NotFoundHandler writes HTTP error 404 to w.
25e001550   Marko Tikvić   exported everything
55
  func NotFoundHandler(w http.ResponseWriter, req *http.Request) {
d2ddf82ef   Marko Tikvić   started on new rbac
56
57
58
59
  	SetDefaultHeaders(w)
  	if req.Method == "OPTIONS" {
  		return
  	}
33fd58161   markotikvic   minor changes, sh...
60
  	ErrorResponse(w, req, http.StatusNotFound, []HttpErrorDesc{
d2ddf82ef   Marko Tikvić   started on new rbac
61
62
  		{"en", "Not found."},
  		{"rs", "Traženi resurs ne postoji."},
25e001550   Marko Tikvić   exported everything
63
  	})
64041a2ea   Marko Tikvić   first commit
64
  }
d2ddf82ef   Marko Tikvić   started on new rbac
65

d66628295   Marko Tikvić   cleaned up
66
  // SetDefaultHeaders set's default headers for an HTTP response.
d2ddf82ef   Marko Tikvić   started on new rbac
67
68
  func SetDefaultHeaders(w http.ResponseWriter) {
  	w.Header().Set("Access-Control-Allow-Origin", "*")
d2ddf82ef   Marko Tikvić   started on new rbac
69
  	w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
d2ddf82ef   Marko Tikvić   started on new rbac
70
71
  	w.Header().Set("Access-Control-Allow-Headers", `Accept, Content-Type,
  		Content-Length, Accept-Encoding, X-CSRF-Token, Authorization`)
d2ddf82ef   Marko Tikvić   started on new rbac
72
73
  	w.Header().Set("Content-Type", "application/json; charset=utf-8")
  }