forked from danmux/floe
/
middleware.go
276 lines (224 loc) · 6.43 KB
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package server
import (
"compress/gzip"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"runtime/debug"
"strings"
"time"
"github.com/julienschmidt/httprouter"
"github.com/floeit/floe/hub"
"github.com/floeit/floe/log"
)
const (
rOK = http.StatusOK
rUnauth = http.StatusUnauthorized
rBad = http.StatusBadRequest
rNotFound = http.StatusNotFound
rErr = http.StatusInternalServerError
rCreated = http.StatusCreated
rConflict = http.StatusConflict
cookieName = "floe-sesh"
)
// AdminToken a configurable admin token for this host
var AdminToken string
type renderable interface{}
func decodeBody(rw http.ResponseWriter, r *http.Request, v interface{}) (bool, int, string) {
defer r.Body.Close()
dec := json.NewDecoder(r.Body)
if err := dec.Decode(v); err != nil {
return false, rBad, err.Error()
}
return true, 0, ""
}
func jsonResp(w http.ResponseWriter, code int, r interface{}) {
b, err := json.MarshalIndent(r, "", " ")
if err != nil {
log.Debug(err)
log.Debugf("%#v", r)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{"Status": "Fail", "Payload": "` + err.Error() + `"}`))
return
}
w.WriteHeader(code)
w.Write(b)
}
type context struct {
ps *httprouter.Params
sesh *session
hub *hub.Hub
}
type notFoundHandler struct{}
func (h notFoundHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
jsonResp(rw, rNotFound, wrapper{Message: "not found"})
}
type contextFunc func(rw http.ResponseWriter, r *http.Request, ctx *context) (int, string, renderable)
type wrapper struct {
Message string
Payload renderable
}
type handler struct {
hub *hub.Hub
}
// the boolean return is set true if the caller should produce its own response
func authRequest(rw http.ResponseWriter, r *http.Request) *session {
var code int
var sesh *session
tok := r.Header.Get("X-Floe-Auth")
if tok == "" {
log.Debug("checking cookie")
c, err := r.Cookie(cookieName)
if err != nil {
log.Warning("cookie problem", err)
} else {
tok = c.Value
}
}
if tok == "" {
code = rUnauth
jsonResp(rw, code, wrapper{Message: "missing session"})
return nil
}
log.Debug("checking token", tok, AdminToken)
// default to this agent for testing admin token
if tok == AdminToken {
log.Debug("found admin token", tok)
sesh = &session{
token: tok,
lastActive: time.Now(),
user: "Admin",
}
}
if sesh == nil {
sesh = goodToken(tok)
if sesh == nil {
code = rUnauth
jsonResp(rw, code, wrapper{Message: "invalid session"})
return nil
}
}
// refresh cookie
setCookie(rw, tok)
return sesh
}
func (h handler) mw(f contextFunc, auth bool) func(rw http.ResponseWriter, r *http.Request, ps httprouter.Params) {
fn := func(rw http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var code int
start := time.Now()
log.Debugf("req: %s %s", r.Method, r.URL.String())
defer func() {
log.Debugf("rsp: %v %s %d %s", time.Since(start), r.Method, code, r.URL.String())
}()
cors(rw, r)
// handler nil is the options catch all so the cors response above is all we need
if f == nil {
code = rOK
jsonResp(rw, code, "ok")
return
}
// authenticate session is needed
var sesh *session
if auth {
sesh = authRequest(rw, r)
if sesh == nil {
return
}
}
// got here then we are authenticated - so call the specific handler
ctx := &context{
ps: &ps,
sesh: sesh,
hub: h.hub,
}
code, msg, res := f(rw, r, ctx)
// code 0 means the function responded itself
if code == 0 {
return
}
if msg == "" && code == rOK {
msg = "OK"
}
reply := wrapper{
Message: msg,
Payload: res,
}
jsonResp(rw, code, reply)
}
return zipper(fn)
}
type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}
func (w gzipResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}
func zipper(fn func(rw http.ResponseWriter, r *http.Request, ps httprouter.Params)) func(rw http.ResponseWriter, r *http.Request, ps httprouter.Params) {
return func(rw http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
fn(rw, r, ps)
return
}
rw.Header().Set("Vary", "Accept-Encoding")
rw.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(rw)
defer gz.Close()
gzr := gzipResponseWriter{Writer: gz, ResponseWriter: rw}
fn(gzr, r, ps)
}
}
func serveFiles(r *httprouter.Router, path string, root http.FileSystem) {
if len(path) < 10 || path[len(path)-10:] != "/*filepath" {
panic("path must end with /*filepath in path '" + path + "'")
}
fileServer := http.FileServer(root)
r.GET(path, zipper(func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
req.URL.Path = ps.ByName("filepath")
fileServer.ServeHTTP(w, req)
}))
}
// setupTriggers goes through all the known trigger types to set up the associated routes
func (h handler) setupPushes(basePath string, r *httprouter.Router, hub *hub.Hub) {
for subPath, t := range pushes {
authenticated := t.RequiresAuth()
// TODO consider parameterised paths
g := t.GetHandler(hub.Queue())
if g != nil {
r.GET(basePath+subPath, h.mw(adaptSub(hub, g), authenticated))
}
p := t.PostHandler(hub.Queue())
if p != nil {
r.POST(basePath+subPath, h.mw(adaptSub(hub, p), authenticated))
}
}
}
func adaptSub(hub *hub.Hub, handle httprouter.Handle) contextFunc {
return func(w http.ResponseWriter, req *http.Request, ctx *context) (int, string, renderable) {
handle(w, req, *ctx.ps)
return 0, "", nil // each subscriber handler is responsible for the response
}
}
func setCookie(rw http.ResponseWriter, tok string) {
expiration := time.Now().Add(seshLifetime)
cookie := http.Cookie{Name: cookieName, Value: tok, Expires: expiration, Path: "/"}
http.SetCookie(rw, &cookie)
}
func cors(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
rw.Header().Set("Access-Control-Allow-Origin", "*")
rw.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE")
rw.Header().Set("Access-Control-Allow-Headers", strings.Join(r.Header["Access-Control-Request-Headers"], ","))
}
func panicHandler(rw http.ResponseWriter, r *http.Request, v interface{}) {
log.Error("PANIC in ", r.URL.String())
log.Error(v)
stack := debug.Stack()
jsonResp(rw, http.StatusInternalServerError, string(stack))
// send it to stderr
fmt.Fprintf(os.Stderr, string(stack))
// this sends it to the client....
// fmt.Fprintf(rw, f, err, )
}