Blame view

http_utility.go 3.1 KB
64041a2ea   Marko Tikvić   first commit
1
2
3
4
5
6
  package restutility
  
  import (
  	"net/http"
  	"encoding/json"
  )
25e001550   Marko Tikvić   exported everything
7
8
9
10
  var _apiVersion = "/api/v1"
  
  func SetApiVersion(ver string) string {
  	_apiVersion = ver
b291ac8c4   Marko Tikvić   clened up
11
  	return _apiVersion
25e001550   Marko Tikvić   exported everything
12
  }
90fd36e9b   Marko Tikvić   resolved some dep...
13

64041a2ea   Marko Tikvić   first commit
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  ////
  //// ERROR UTILITY
  ////
  
  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."
  
  type HttpError struct {
  	Error   []HttpErrorDesc `json:"error"`
  	Request string          `json:"request"`
  }
  
  type HttpErrorDesc struct {
  	Lang string `json:"lang"`
  	Desc string `json:"description"`
  }
25e001550   Marko Tikvić   exported everything
32
  func RespondWithHttpError(w http.ResponseWriter,
64041a2ea   Marko Tikvić   first commit
33
34
35
36
37
38
39
40
41
42
43
  	req *http.Request,
  	code int,
  	httpErr []HttpErrorDesc) {
  
  	err := HttpError{
  		Error: httpErr,
  		Request: req.Method + " " + req.URL.Path,
  	}
  	w.WriteHeader(code)
  	json.NewEncoder(w).Encode(err)
  }
25e001550   Marko Tikvić   exported everything
44
45
46
47
48
  func RespondWithHttpError400(w http.ResponseWriter, req *http.Request) {
  	RespondWithHttpError(w, req, http.StatusBadRequest, []HttpErrorDesc{
  		{Lang: "en", Desc: templateHttpErr400_EN},
  		{Lang: "rs", Desc: templateHttpErr400_RS},
  	})
64041a2ea   Marko Tikvić   first commit
49
  }
25e001550   Marko Tikvić   exported everything
50
51
52
53
54
  func RespondWithHttpError500(w http.ResponseWriter, req *http.Request) {
  	RespondWithHttpError(w, req, http.StatusInternalServerError, []HttpErrorDesc{
  		{Lang: "en", Desc: templateHttpErr500_EN},
  		{Lang: "rs", Desc: templateHttpErr500_RS},
  	})
64041a2ea   Marko Tikvić   first commit
55
  }
64041a2ea   Marko Tikvić   first commit
56
57
58
  ////
  //// HANDLER FUNC WRAPPER
  ////
64041a2ea   Marko Tikvić   first commit
59
  // Sets common headers and checks for token validity.
8dbe745c3   Marko Tikvić   merged tables uti...
60
  func ProcessHeaders(fn http.HandlerFunc) http.HandlerFunc {
64041a2ea   Marko Tikvić   first commit
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  	return func(w http.ResponseWriter, req *http.Request) {
  //		@TODO: check Content-type header (must be application/json)
  //		ctype := w.Header.Get("Content-Type")
  //		if req.Method != "GET" && ctype != "application/json" {
  //			replyWithHttpError(w, req, http.StatusBadRequest,
  //				"Not a supported content type: " + ctype)
  //		}
  
  		w.Header().Set("Access-Control-Allow-Origin", "*")
  		w.Header().Set("Access-Control-Allow-Methods",
  			`POST,
  			GET,
  			PUT,
  			DELETE,
  			OPTIONS`)
  		w.Header().Set("Access-Control-Allow-Headers",
  			`Accept,
  			Content-Type,
  			Content-Length,
  			Accept-Encoding,
  			X-CSRF-Token,
  			Authorization`)
  		w.Header().Set("Content-Type", "application/json; charset=utf-8")
  
  		if req.Method == "OPTIONS" {
  			return
  		}
25e001550   Marko Tikvić   exported everything
88
  		if req.URL.Path != _apiVersion + "/token/new" {
64041a2ea   Marko Tikvić   first commit
89
  			token := req.Header.Get("Authorization")
25e001550   Marko Tikvić   exported everything
90
91
  			if _, err := ParseAPIToken(token); err != nil {
  				RespondWithHttpError(w, req, http.StatusUnauthorized,
64041a2ea   Marko Tikvić   first commit
92
  					[]HttpErrorDesc{
25e001550   Marko Tikvić   exported everything
93
94
  						{Lang: "en", Desc: "Unauthorized request."},
  						{Lang: "rs", Desc: "Neautorizovani zahtev."},
64041a2ea   Marko Tikvić   first commit
95
96
97
98
99
100
101
  					})
  				return
  			}
  		}
  
  		err := req.ParseForm()
  		if err != nil {
25e001550   Marko Tikvić   exported everything
102
  			RespondWithHttpError(w, req, http.StatusBadRequest,
64041a2ea   Marko Tikvić   first commit
103
  				[]HttpErrorDesc{
25e001550   Marko Tikvić   exported everything
104
105
  					{Lang: "en", Desc: templateHttpErr400_EN},
  					{Lang: "rs", Desc: templateHttpErr400_RS},
64041a2ea   Marko Tikvić   first commit
106
107
108
109
110
111
112
113
114
115
  				})
  			return
  		}
  		fn(w, req)
  	}
  }
  
  ////
  //// NOT FOUND HANDLER
  ////
25e001550   Marko Tikvić   exported everything
116
117
118
119
120
  func NotFoundHandler(w http.ResponseWriter, req *http.Request) {
  	RespondWithHttpError(w, req, http.StatusNotFound, []HttpErrorDesc{
  		{Lang: "en", Desc: "Not found."},
  		{Lang: "rs", Desc: "Traženi resurs ne postoji."},
  	})
64041a2ea   Marko Tikvić   first commit
121
  }