Blame view

http.go 4.42 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
e2880d3fb   Marko Tikvić   integraded golloger
13
  	data   []byte
34436d11e   Marko Tikvić   in-out http logs ...
14
  }
707782344   Marko Tikvić   lint; vet
15
  // NewStatusRecorder ...
f38e87cf4   Marko Tikvić   status recorder
16
17
18
19
20
  func NewStatusRecorder(w http.ResponseWriter) *StatusRecorder {
  	return &StatusRecorder{
  		writer: w,
  		status: 0,
  		size:   0,
e2880d3fb   Marko Tikvić   integraded golloger
21
  		data:   nil,
f38e87cf4   Marko Tikvić   status recorder
22
  	}
34436d11e   Marko Tikvić   in-out http logs ...
23
  }
707782344   Marko Tikvić   lint; vet
24
  // WriteHeader is a wrapper http.ResponseWriter interface
f38e87cf4   Marko Tikvić   status recorder
25
  func (r *StatusRecorder) WriteHeader(code int) {
34436d11e   Marko Tikvić   in-out http logs ...
26
  	r.status = code
f38e87cf4   Marko Tikvić   status recorder
27
28
  	r.writer.WriteHeader(code)
  }
707782344   Marko Tikvić   lint; vet
29
  // Write is a wrapper for http.ResponseWriter interface
f38e87cf4   Marko Tikvić   status recorder
30
31
  func (r *StatusRecorder) Write(in []byte) (int, error) {
  	r.size = len(in)
e2880d3fb   Marko Tikvić   integraded golloger
32
33
34
35
  	if r.status >= 400 {
  		r.data = make([]byte, len(in))
  		copy(r.data, in)
  	}
f38e87cf4   Marko Tikvić   status recorder
36
  	return r.writer.Write(in)
34436d11e   Marko Tikvić   in-out http logs ...
37
  }
707782344   Marko Tikvić   lint; vet
38
  // Header is a wrapper for http.ResponseWriter interface
f38e87cf4   Marko Tikvić   status recorder
39
40
41
  func (r *StatusRecorder) Header() http.Header {
  	return r.writer.Header()
  }
707782344   Marko Tikvić   lint; vet
42
  // Status ...
f38e87cf4   Marko Tikvić   status recorder
43
  func (r *StatusRecorder) Status() int {
34436d11e   Marko Tikvić   in-out http logs ...
44
45
  	return r.status
  }
707782344   Marko Tikvić   lint; vet
46
  // Size ...
f38e87cf4   Marko Tikvić   status recorder
47
48
49
  func (r *StatusRecorder) Size() int {
  	return r.size
  }
e2880d3fb   Marko Tikvić   integraded golloger
50
51
52
53
  // Size ...
  func (r *StatusRecorder) Data() []byte {
  	return r.data
  }
ad8e9dd2a   Marko Tikvić   added middleware ...
54
55
  // NotFoundHandlerFunc writes HTTP error 404 to w.
  func NotFoundHandlerFunc(w http.ResponseWriter, req *http.Request) {
65d214f47   Marko Tikvić   improved middlewa...
56
57
  	SetAccessControlHeaders(w)
  	SetContentType(w, "application/json")
ad8e9dd2a   Marko Tikvić   added middleware ...
58
  	NotFound(w, req, fmt.Sprintf("Resource you requested was not found: %s", req.URL.String()))
9933169c8   Marko Tikvić   localization support
59
  }
65d214f47   Marko Tikvić   improved middlewa...
60
  // SetContentType must be called before SetResponseStatus (w.WriteHeader) (?)
cacf57bd4   Marko Tikvić   merging with /uti...
61
62
63
  func SetContentType(w http.ResponseWriter, ctype string) {
  	w.Header().Set("Content-Type", ctype)
  }
46b2215eb   Marko Tikvić   lintfix
64
  // SetResponseStatus ...
cacf57bd4   Marko Tikvić   merging with /uti...
65
66
67
  func SetResponseStatus(w http.ResponseWriter, status int) {
  	w.WriteHeader(status)
  }
46b2215eb   Marko Tikvić   lintfix
68
  // WriteResponse ...
cacf57bd4   Marko Tikvić   merging with /uti...
69
70
71
  func WriteResponse(w http.ResponseWriter, content []byte) {
  	w.Write(content)
  }
65d214f47   Marko Tikvić   improved middlewa...
72
73
  // SetAccessControlHeaders set's default headers for an HTTP response.
  func SetAccessControlHeaders(w http.ResponseWriter) {
9933169c8   Marko Tikvić   localization support
74
75
  	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 ...
76
  	w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
9933169c8   Marko Tikvić   localization support
77
  }
707782344   Marko Tikvić   lint; vet
78
  // GetLocale ...
34436d11e   Marko Tikvić   in-out http logs ...
79
  func GetLocale(req *http.Request, dflt string) string {
a205e8f40   Marko Tikvić   changes
80
81
82
83
84
85
  	loc := req.FormValue("locale")
  	if loc == "" {
  		return dflt
  	}
  	return loc
  }
707782344   Marko Tikvić   lint; vet
86
  // Success ...
1b7dfab73   Marko Tikvić   Payload changed t...
87
  func Success(w http.ResponseWriter, payload interface{}, code int) {
9933169c8   Marko Tikvić   localization support
88
89
  	w.WriteHeader(code)
  	if payload != nil {
1b7dfab73   Marko Tikvić   Payload changed t...
90
  		json.NewEncoder(w).Encode(payload)
9933169c8   Marko Tikvić   localization support
91
92
  	}
  }
707782344   Marko Tikvić   lint; vet
93
  // OK ...
1b7dfab73   Marko Tikvić   Payload changed t...
94
  func OK(w http.ResponseWriter, payload interface{}) {
65d214f47   Marko Tikvić   improved middlewa...
95
  	SetContentType(w, "application/json")
9933169c8   Marko Tikvić   localization support
96
97
  	Success(w, payload, http.StatusOK)
  }
707782344   Marko Tikvić   lint; vet
98
  // Created ...
1b7dfab73   Marko Tikvić   Payload changed t...
99
  func Created(w http.ResponseWriter, payload interface{}) {
65d214f47   Marko Tikvić   improved middlewa...
100
  	SetContentType(w, "application/json")
9933169c8   Marko Tikvić   localization support
101
102
  	Success(w, payload, http.StatusCreated)
  }
3fffcb954   Marko Tikvić   removed old http API
103
104
105
  type weberror struct {
  	Request string `json:"request"`
  	Error   string `json:"error"`
832243a5d   Marko Tikvić   todo
106
  	//Code    int64  `json:"code"` TODO
3fffcb954   Marko Tikvić   removed old http API
107
  }
707782344   Marko Tikvić   lint; vet
108
  // Error ...
9933169c8   Marko Tikvić   localization support
109
  func Error(w http.ResponseWriter, r *http.Request, code int, err string) {
3fffcb954   Marko Tikvić   removed old http API
110
  	werr := weberror{Error: err, Request: r.Method + " " + r.RequestURI}
9933169c8   Marko Tikvić   localization support
111
112
113
  	w.WriteHeader(code)
  	json.NewEncoder(w).Encode(werr)
  }
707782344   Marko Tikvić   lint; vet
114
  // BadRequest ...
9933169c8   Marko Tikvić   localization support
115
  func BadRequest(w http.ResponseWriter, r *http.Request, err string) {
65d214f47   Marko Tikvić   improved middlewa...
116
  	SetContentType(w, "application/json")
9933169c8   Marko Tikvić   localization support
117
118
  	Error(w, r, http.StatusBadRequest, err)
  }
707782344   Marko Tikvić   lint; vet
119
  // Unauthorized ...
9933169c8   Marko Tikvić   localization support
120
  func Unauthorized(w http.ResponseWriter, r *http.Request, err string) {
65d214f47   Marko Tikvić   improved middlewa...
121
  	SetContentType(w, "application/json")
9933169c8   Marko Tikvić   localization support
122
123
  	Error(w, r, http.StatusUnauthorized, err)
  }
707782344   Marko Tikvić   lint; vet
124
  // Forbidden ...
9933169c8   Marko Tikvić   localization support
125
  func Forbidden(w http.ResponseWriter, r *http.Request, err string) {
65d214f47   Marko Tikvić   improved middlewa...
126
  	SetContentType(w, "application/json")
9933169c8   Marko Tikvić   localization support
127
128
  	Error(w, r, http.StatusForbidden, err)
  }
707782344   Marko Tikvić   lint; vet
129
  // NotFound ...
1b7dfab73   Marko Tikvić   Payload changed t...
130
  func NotFound(w http.ResponseWriter, r *http.Request, err string) {
65d214f47   Marko Tikvić   improved middlewa...
131
  	SetContentType(w, "application/json")
1b7dfab73   Marko Tikvić   Payload changed t...
132
133
  	Error(w, r, http.StatusNotFound, err)
  }
707782344   Marko Tikvić   lint; vet
134
  // Conflict ...
9933169c8   Marko Tikvić   localization support
135
  func Conflict(w http.ResponseWriter, r *http.Request, err string) {
65d214f47   Marko Tikvić   improved middlewa...
136
  	SetContentType(w, "application/json")
9933169c8   Marko Tikvić   localization support
137
138
  	Error(w, r, http.StatusConflict, err)
  }
707782344   Marko Tikvić   lint; vet
139
  // InternalServerError ...
9933169c8   Marko Tikvić   localization support
140
  func InternalServerError(w http.ResponseWriter, r *http.Request, err string) {
65d214f47   Marko Tikvić   improved middlewa...
141
  	SetContentType(w, "application/json")
9933169c8   Marko Tikvić   localization support
142
143
  	Error(w, r, http.StatusInternalServerError, err)
  }
3173b06a4   Marko Tikvić   SplitString()
144
145
146
147
148
149
150
151
152
153
154
155
  
  func SetHeader(r *http.Request, key, val string) {
  	r.Header.Set(key, val)
  }
  
  func AddHeader(r *http.Request, key, val string) {
  	r.Header.Add(key, val)
  }
  
  func GetHeader(r *http.Request, key string) string {
  	return r.Header.Get(key)
  }
25b39d69c   Marko Tikvić   NewServer renamed...
156

1b51eed04   Marko Tikvić   pdf helper
157
  func ClientUTCOffset(req *http.Request) int64 {
25b39d69c   Marko Tikvić   NewServer renamed...
158
159
  	return StringToInt64(GetHeader(req, "X-Timezone-Offset"))
  }