Blame view

http.go 2.99 KB
ea858b8a7   Marko Tikvić   refactoring
1
  package webutility
64041a2ea   Marko Tikvić   first commit
2
3
  
  import (
64041a2ea   Marko Tikvić   first commit
4
  	"encoding/json"
ad8e9dd2a   Marko Tikvić   added middleware ...
5
  	"fmt"
d2ddf82ef   Marko Tikvić   started on new rbac
6
  	"net/http"
64041a2ea   Marko Tikvić   first commit
7
  )
f38e87cf4   Marko Tikvić   status recorder
8
9
  type StatusRecorder struct {
  	writer http.ResponseWriter
34436d11e   Marko Tikvić   in-out http logs ...
10
  	status int
f38e87cf4   Marko Tikvić   status recorder
11
  	size   int
34436d11e   Marko Tikvić   in-out http logs ...
12
  }
f38e87cf4   Marko Tikvić   status recorder
13
14
15
16
17
18
  func NewStatusRecorder(w http.ResponseWriter) *StatusRecorder {
  	return &StatusRecorder{
  		writer: w,
  		status: 0,
  		size:   0,
  	}
34436d11e   Marko Tikvić   in-out http logs ...
19
  }
f38e87cf4   Marko Tikvić   status recorder
20
21
  // http.ResponseWriter interface
  func (r *StatusRecorder) WriteHeader(code int) {
34436d11e   Marko Tikvić   in-out http logs ...
22
  	r.status = code
f38e87cf4   Marko Tikvić   status recorder
23
24
25
26
27
28
29
  	r.writer.WriteHeader(code)
  }
  
  // http.ResponseWriter interface
  func (r *StatusRecorder) Write(in []byte) (int, error) {
  	r.size = len(in)
  	return r.writer.Write(in)
34436d11e   Marko Tikvić   in-out http logs ...
30
  }
f38e87cf4   Marko Tikvić   status recorder
31
32
33
34
35
36
  // http.ResponseWriter interface
  func (r *StatusRecorder) Header() http.Header {
  	return r.writer.Header()
  }
  
  func (r *StatusRecorder) Status() int {
34436d11e   Marko Tikvić   in-out http logs ...
37
38
  	return r.status
  }
f38e87cf4   Marko Tikvić   status recorder
39
40
41
  func (r *StatusRecorder) Size() int {
  	return r.size
  }
ad8e9dd2a   Marko Tikvić   added middleware ...
42
43
  // NotFoundHandlerFunc writes HTTP error 404 to w.
  func NotFoundHandlerFunc(w http.ResponseWriter, req *http.Request) {
9933169c8   Marko Tikvić   localization support
44
45
46
47
  	SetDefaultHeaders(w)
  	if req.Method == "OPTIONS" {
  		return
  	}
ad8e9dd2a   Marko Tikvić   added middleware ...
48
  	NotFound(w, req, fmt.Sprintf("Resource you requested was not found: %s", req.URL.String()))
9933169c8   Marko Tikvić   localization support
49
50
51
52
53
54
  }
  
  // SetDefaultHeaders set's default headers for an HTTP response.
  func SetDefaultHeaders(w http.ResponseWriter) {
  	w.Header().Set("Access-Control-Allow-Origin", "*")
  	w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
34436d11e   Marko Tikvić   in-out http logs ...
55
  	w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
9933169c8   Marko Tikvić   localization support
56
57
  	w.Header().Set("Content-Type", "application/json; charset=utf-8")
  }
34436d11e   Marko Tikvić   in-out http logs ...
58
  func GetLocale(req *http.Request, dflt string) string {
a205e8f40   Marko Tikvić   changes
59
60
61
62
63
64
  	loc := req.FormValue("locale")
  	if loc == "" {
  		return dflt
  	}
  	return loc
  }
9933169c8   Marko Tikvić   localization support
65
  // 2xx
1b7dfab73   Marko Tikvić   Payload changed t...
66
  func Success(w http.ResponseWriter, payload interface{}, code int) {
9933169c8   Marko Tikvić   localization support
67
68
  	w.WriteHeader(code)
  	if payload != nil {
1b7dfab73   Marko Tikvić   Payload changed t...
69
  		json.NewEncoder(w).Encode(payload)
9933169c8   Marko Tikvić   localization support
70
71
72
73
  	}
  }
  
  // 200
1b7dfab73   Marko Tikvić   Payload changed t...
74
  func OK(w http.ResponseWriter, payload interface{}) {
9933169c8   Marko Tikvić   localization support
75
76
77
78
  	Success(w, payload, http.StatusOK)
  }
  
  // 201
1b7dfab73   Marko Tikvić   Payload changed t...
79
  func Created(w http.ResponseWriter, payload interface{}) {
9933169c8   Marko Tikvić   localization support
80
81
  	Success(w, payload, http.StatusCreated)
  }
3fffcb954   Marko Tikvić   removed old http API
82
83
84
85
  type weberror struct {
  	Request string `json:"request"`
  	Error   string `json:"error"`
  }
9933169c8   Marko Tikvić   localization support
86
87
  // 4xx; 5xx
  func Error(w http.ResponseWriter, r *http.Request, code int, err string) {
3fffcb954   Marko Tikvić   removed old http API
88
  	werr := weberror{Error: err, Request: r.Method + " " + r.RequestURI}
9933169c8   Marko Tikvić   localization support
89
90
91
92
93
94
95
96
  	w.WriteHeader(code)
  	json.NewEncoder(w).Encode(werr)
  }
  
  // 400
  func BadRequest(w http.ResponseWriter, r *http.Request, err string) {
  	Error(w, r, http.StatusBadRequest, err)
  }
9933169c8   Marko Tikvić   localization support
97
98
99
100
101
102
103
104
105
  // 401
  func Unauthorized(w http.ResponseWriter, r *http.Request, err string) {
  	Error(w, r, http.StatusUnauthorized, err)
  }
  
  // 403
  func Forbidden(w http.ResponseWriter, r *http.Request, err string) {
  	Error(w, r, http.StatusForbidden, err)
  }
1b7dfab73   Marko Tikvić   Payload changed t...
106
107
108
109
110
111
  // 404
  func NotFound(w http.ResponseWriter, r *http.Request, err string) {
  	Error(w, r, http.StatusNotFound, err)
  }
  
  // 409
9933169c8   Marko Tikvić   localization support
112
113
114
115
116
117
118
119
  func Conflict(w http.ResponseWriter, r *http.Request, err string) {
  	Error(w, r, http.StatusConflict, err)
  }
  
  // 500
  func InternalServerError(w http.ResponseWriter, r *http.Request, err string) {
  	Error(w, r, http.StatusInternalServerError, err)
  }