Blame view

http_utility.go 2.95 KB
ea858b8a7   Marko Tikvić   refactoring
1
  package webutility
64041a2ea   Marko Tikvić   first commit
2
3
4
5
6
  
  import (
  	"net/http"
  	"encoding/json"
  )
64041a2ea   Marko Tikvić   first commit
7
8
9
10
  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."
33fd58161   markotikvic   minor changes, sh...
11
12
  const templateHttpErr401_EN = "Unauthorized request."
  const templateHttpErr401_RS = "Neautorizovan zahtev."
64041a2ea   Marko Tikvić   first commit
13

33fd58161   markotikvic   minor changes, sh...
14
  type httpError struct {
64041a2ea   Marko Tikvić   first commit
15
16
17
18
19
20
21
22
  	Error   []HttpErrorDesc `json:"error"`
  	Request string          `json:"request"`
  }
  
  type HttpErrorDesc struct {
  	Lang string `json:"lang"`
  	Desc string `json:"description"`
  }
e1fbb41f9   Marko Tikvić   added comments
23
  // ErrorResponse writes HTTP error to w.
33fd58161   markotikvic   minor changes, sh...
24
25
  func ErrorResponse(w http.ResponseWriter, r *http.Request, code int, desc []HttpErrorDesc) {
  	err := httpError{ desc, r.Method + " " + r.URL.Path }
64041a2ea   Marko Tikvić   first commit
26
27
28
  	w.WriteHeader(code)
  	json.NewEncoder(w).Encode(err)
  }
e1fbb41f9   Marko Tikvić   added comments
29
  // BadRequestResponse writes HTTP error 400 to w.
33fd58161   markotikvic   minor changes, sh...
30
31
32
33
  func BadRequestResponse(w http.ResponseWriter, req *http.Request) {
  	ErrorResponse(w, req, http.StatusBadRequest, []HttpErrorDesc{
  		{ "en", templateHttpErr400_EN },
  		{ "rs", templateHttpErr400_RS },
25e001550   Marko Tikvić   exported everything
34
  	})
64041a2ea   Marko Tikvić   first commit
35
  }
e1fbb41f9   Marko Tikvić   added comments
36
  // InternalSeverErrorResponse writes HTTP error 500 to w.
33fd58161   markotikvic   minor changes, sh...
37
38
39
40
  func InternalServerErrorResponse(w http.ResponseWriter, req *http.Request) {
  	ErrorResponse(w, req, http.StatusInternalServerError, []HttpErrorDesc{
  		{ "en", templateHttpErr500_EN },
  		{ "rs", templateHttpErr500_RS },
25e001550   Marko Tikvić   exported everything
41
  	})
64041a2ea   Marko Tikvić   first commit
42
  }
e1fbb41f9   Marko Tikvić   added comments
43
  // UnauthorizedError writes HTTP error 401 to w.
33fd58161   markotikvic   minor changes, sh...
44
45
  func UnauthorizedResponse(w http.ResponseWriter, req *http.Request) {
  	ErrorResponse(w, req, http.StatusUnauthorized, []HttpErrorDesc{
c47161efb   Marko Tikvić   fixed bug: Unauth...
46
47
  		{ "en", templateHttpErr401_EN },
  		{ "rs", templateHttpErr401_RS },
33fd58161   markotikvic   minor changes, sh...
48
49
  	})
  }
33fd58161   markotikvic   minor changes, sh...
50
  // TODO: Check for content type
33d137a67   Marko Tikvić   Functional role c...
51
  // WrapHandler sets common headers, checks for token validity and performs access control checks.
3a5383589   Marko Tikvić   Improved document...
52
  // If authentication passes it calls the handlerFunc.
33d137a67   Marko Tikvić   Functional role c...
53
  func WrapHandler(handlerFunc http.HandlerFunc, authorizedRoles []string) http.HandlerFunc {
64041a2ea   Marko Tikvić   first commit
54
  	return func(w http.ResponseWriter, req *http.Request) {
64041a2ea   Marko Tikvić   first commit
55
  		w.Header().Set("Access-Control-Allow-Origin", "*")
33fd58161   markotikvic   minor changes, sh...
56

64041a2ea   Marko Tikvić   first commit
57
  		w.Header().Set("Access-Control-Allow-Methods",
33fd58161   markotikvic   minor changes, sh...
58
  			"POST, GET, PUT, DELETE, OPTIONS")
64041a2ea   Marko Tikvić   first commit
59
  		w.Header().Set("Access-Control-Allow-Headers",
4b4ea384f   Marko Tikvić   hmm
60
61
  			`Accept, Content-Type, Content-Length,
  			Accept-Encoding, X-CSRF-Token, Authorization`)
33fd58161   markotikvic   minor changes, sh...
62

64041a2ea   Marko Tikvić   first commit
63
64
65
66
67
  		w.Header().Set("Content-Type", "application/json; charset=utf-8")
  
  		if req.Method == "OPTIONS" {
  			return
  		}
33d137a67   Marko Tikvić   Functional role c...
68
  		if authorizedRoles != nil {
33fd58161   markotikvic   minor changes, sh...
69
  			token := req.Header.Get("Authorization")
33d137a67   Marko Tikvić   Functional role c...
70
71
  			claims, err := ParseAPIToken(token);
  			if err != nil || !roleAuthorized(authorizedRoles, claims) {
4b4ea384f   Marko Tikvić   hmm
72
  				UnauthorizedResponse(w, req)
33fd58161   markotikvic   minor changes, sh...
73
  				return
64041a2ea   Marko Tikvić   first commit
74
75
76
77
78
  			}
  		}
  
  		err := req.ParseForm()
  		if err != nil {
33fd58161   markotikvic   minor changes, sh...
79
  			BadRequestResponse(w, req)
64041a2ea   Marko Tikvić   first commit
80
81
  			return
  		}
4a51e54d7   Marko Tikvić   simplified
82
83
  
  		// execute HandlerFunc
90f4ed079   Marko Tikvić   sped up loadTable()
84
  		handlerFunc(w, req)
64041a2ea   Marko Tikvić   first commit
85
86
  	}
  }
e1fbb41f9   Marko Tikvić   added comments
87
  // NotFoundHandler writes HTTP error 404 to w.
25e001550   Marko Tikvić   exported everything
88
  func NotFoundHandler(w http.ResponseWriter, req *http.Request) {
33fd58161   markotikvic   minor changes, sh...
89
90
91
  	ErrorResponse(w, req, http.StatusNotFound, []HttpErrorDesc{
  		{ "en", "Not found." },
  		{ "rs", "Traženi resurs ne postoji." },
25e001550   Marko Tikvić   exported everything
92
  	})
64041a2ea   Marko Tikvić   first commit
93
  }