Blame view

http_utility.go 2.61 KB
64041a2ea   Marko Tikvić   first commit
1
2
3
4
5
6
  package restutility
  
  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"`
  }
33fd58161   markotikvic   minor changes, sh...
23
24
  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
25
26
27
  	w.WriteHeader(code)
  	json.NewEncoder(w).Encode(err)
  }
33fd58161   markotikvic   minor changes, sh...
28
29
30
31
  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
32
  	})
64041a2ea   Marko Tikvić   first commit
33
  }
33fd58161   markotikvic   minor changes, sh...
34
35
36
37
  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
38
  	})
64041a2ea   Marko Tikvić   first commit
39
  }
33fd58161   markotikvic   minor changes, sh...
40
41
42
43
44
45
46
47
48
  func UnauthorizedResponse(w http.ResponseWriter, req *http.Request) {
  	ErrorResponse(w, req, http.StatusUnauthorized, []HttpErrorDesc{
  		{ "en", templateHttpErr500_EN },
  		{ "rs", templateHttpErr500_RS },
  	})
  }
  
  // TODO: Add parameters to enable/disable roles authorization checks
  // TODO: Check for content type
64041a2ea   Marko Tikvić   first commit
49
  // Sets common headers and checks for token validity.
33fd58161   markotikvic   minor changes, sh...
50
  func WrapHandler(handlerFunc http.HandlerFunc, needauth bool) http.HandlerFunc {
64041a2ea   Marko Tikvić   first commit
51
  	return func(w http.ResponseWriter, req *http.Request) {
64041a2ea   Marko Tikvić   first commit
52
  		w.Header().Set("Access-Control-Allow-Origin", "*")
33fd58161   markotikvic   minor changes, sh...
53

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

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