Blame view

http.go 3.49 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
  )
707782344   Marko Tikvić   lint; vet
8
  // StatusRecorder ...
f38e87cf4   Marko Tikvić   status recorder
9
10
  type StatusRecorder struct {
  	writer http.ResponseWriter
34436d11e   Marko Tikvić   in-out http logs ...
11
  	status int
f38e87cf4   Marko Tikvić   status recorder
12
  	size   int
34436d11e   Marko Tikvić   in-out http logs ...
13
  }
707782344   Marko Tikvić   lint; vet
14
  // NewStatusRecorder ...
f38e87cf4   Marko Tikvić   status recorder
15
16
17
18
19
20
  func NewStatusRecorder(w http.ResponseWriter) *StatusRecorder {
  	return &StatusRecorder{
  		writer: w,
  		status: 0,
  		size:   0,
  	}
34436d11e   Marko Tikvić   in-out http logs ...
21
  }
707782344   Marko Tikvić   lint; vet
22
  // WriteHeader is a wrapper http.ResponseWriter interface
f38e87cf4   Marko Tikvić   status recorder
23
  func (r *StatusRecorder) WriteHeader(code int) {
34436d11e   Marko Tikvić   in-out http logs ...
24
  	r.status = code
f38e87cf4   Marko Tikvić   status recorder
25
26
  	r.writer.WriteHeader(code)
  }
707782344   Marko Tikvić   lint; vet
27
  // Write is a wrapper for http.ResponseWriter interface
f38e87cf4   Marko Tikvić   status recorder
28
29
30
  func (r *StatusRecorder) Write(in []byte) (int, error) {
  	r.size = len(in)
  	return r.writer.Write(in)
34436d11e   Marko Tikvić   in-out http logs ...
31
  }
707782344   Marko Tikvić   lint; vet
32
  // Header is a wrapper for http.ResponseWriter interface
f38e87cf4   Marko Tikvić   status recorder
33
34
35
  func (r *StatusRecorder) Header() http.Header {
  	return r.writer.Header()
  }
707782344   Marko Tikvić   lint; vet
36
  // Status ...
f38e87cf4   Marko Tikvić   status recorder
37
  func (r *StatusRecorder) Status() int {
34436d11e   Marko Tikvić   in-out http logs ...
38
39
  	return r.status
  }
707782344   Marko Tikvić   lint; vet
40
  // Size ...
f38e87cf4   Marko Tikvić   status recorder
41
42
43
  func (r *StatusRecorder) Size() int {
  	return r.size
  }
ad8e9dd2a   Marko Tikvić   added middleware ...
44
45
  // NotFoundHandlerFunc writes HTTP error 404 to w.
  func NotFoundHandlerFunc(w http.ResponseWriter, req *http.Request) {
9933169c8   Marko Tikvić   localization support
46
47
48
49
  	SetDefaultHeaders(w)
  	if req.Method == "OPTIONS" {
  		return
  	}
ad8e9dd2a   Marko Tikvić   added middleware ...
50
  	NotFound(w, req, fmt.Sprintf("Resource you requested was not found: %s", req.URL.String()))
9933169c8   Marko Tikvić   localization support
51
  }
cacf57bd4   Marko Tikvić   merging with /uti...
52
53
54
55
56
57
58
59
60
61
62
  func SetContentType(w http.ResponseWriter, ctype string) {
  	w.Header().Set("Content-Type", ctype)
  }
  
  func SetResponseStatus(w http.ResponseWriter, status int) {
  	w.WriteHeader(status)
  }
  
  func WriteResponse(w http.ResponseWriter, content []byte) {
  	w.Write(content)
  }
9933169c8   Marko Tikvić   localization support
63
64
65
66
  // 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 ...
67
  	w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
cacf57bd4   Marko Tikvić   merging with /uti...
68
  	SetContentType(w, "application/json; charset=utf-8")
9933169c8   Marko Tikvić   localization support
69
  }
707782344   Marko Tikvić   lint; vet
70
  // GetLocale ...
34436d11e   Marko Tikvić   in-out http logs ...
71
  func GetLocale(req *http.Request, dflt string) string {
a205e8f40   Marko Tikvić   changes
72
73
74
75
76
77
  	loc := req.FormValue("locale")
  	if loc == "" {
  		return dflt
  	}
  	return loc
  }
707782344   Marko Tikvić   lint; vet
78
  // Success ...
1b7dfab73   Marko Tikvić   Payload changed t...
79
  func Success(w http.ResponseWriter, payload interface{}, code int) {
9933169c8   Marko Tikvić   localization support
80
81
  	w.WriteHeader(code)
  	if payload != nil {
1b7dfab73   Marko Tikvić   Payload changed t...
82
  		json.NewEncoder(w).Encode(payload)
9933169c8   Marko Tikvić   localization support
83
84
  	}
  }
707782344   Marko Tikvić   lint; vet
85
  // OK ...
1b7dfab73   Marko Tikvić   Payload changed t...
86
  func OK(w http.ResponseWriter, payload interface{}) {
9933169c8   Marko Tikvić   localization support
87
88
  	Success(w, payload, http.StatusOK)
  }
707782344   Marko Tikvić   lint; vet
89
  // Created ...
1b7dfab73   Marko Tikvić   Payload changed t...
90
  func Created(w http.ResponseWriter, payload interface{}) {
9933169c8   Marko Tikvić   localization support
91
92
  	Success(w, payload, http.StatusCreated)
  }
3fffcb954   Marko Tikvić   removed old http API
93
94
95
96
  type weberror struct {
  	Request string `json:"request"`
  	Error   string `json:"error"`
  }
707782344   Marko Tikvić   lint; vet
97
  // Error ...
9933169c8   Marko Tikvić   localization support
98
  func Error(w http.ResponseWriter, r *http.Request, code int, err string) {
3fffcb954   Marko Tikvić   removed old http API
99
  	werr := weberror{Error: err, Request: r.Method + " " + r.RequestURI}
9933169c8   Marko Tikvić   localization support
100
101
102
  	w.WriteHeader(code)
  	json.NewEncoder(w).Encode(werr)
  }
707782344   Marko Tikvić   lint; vet
103
  // BadRequest ...
9933169c8   Marko Tikvić   localization support
104
105
106
  func BadRequest(w http.ResponseWriter, r *http.Request, err string) {
  	Error(w, r, http.StatusBadRequest, err)
  }
707782344   Marko Tikvić   lint; vet
107
  // Unauthorized ...
9933169c8   Marko Tikvić   localization support
108
109
110
  func Unauthorized(w http.ResponseWriter, r *http.Request, err string) {
  	Error(w, r, http.StatusUnauthorized, err)
  }
707782344   Marko Tikvić   lint; vet
111
  // Forbidden ...
9933169c8   Marko Tikvić   localization support
112
113
114
  func Forbidden(w http.ResponseWriter, r *http.Request, err string) {
  	Error(w, r, http.StatusForbidden, err)
  }
707782344   Marko Tikvić   lint; vet
115
  // NotFound ...
1b7dfab73   Marko Tikvić   Payload changed t...
116
117
118
  func NotFound(w http.ResponseWriter, r *http.Request, err string) {
  	Error(w, r, http.StatusNotFound, err)
  }
707782344   Marko Tikvić   lint; vet
119
  // Conflict ...
9933169c8   Marko Tikvić   localization support
120
121
122
  func Conflict(w http.ResponseWriter, r *http.Request, err string) {
  	Error(w, r, http.StatusConflict, err)
  }
707782344   Marko Tikvić   lint; vet
123
  // InternalServerError ...
9933169c8   Marko Tikvić   localization support
124
125
126
  func InternalServerError(w http.ResponseWriter, r *http.Request, err string) {
  	Error(w, r, http.StatusInternalServerError, err)
  }