/
server.go
345 lines (328 loc) · 12.2 KB
/
server.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Code generated by goa v3.2.4, DO NOT EDIT.
//
// notifications HTTP server
//
// Command:
// $ goa gen github.com/fieldkit/cloud/server/api/design
package server
import (
"context"
"net/http"
"regexp"
notifications "github.com/fieldkit/cloud/server/api/gen/notifications"
"github.com/gorilla/websocket"
goahttp "goa.design/goa/v3/http"
goa "goa.design/goa/v3/pkg"
"goa.design/plugins/v3/cors"
)
// Server lists the notifications service endpoint HTTP handlers.
type Server struct {
Mounts []*MountPoint
Listen http.Handler
Seen http.Handler
CORS http.Handler
}
// ErrorNamer is an interface implemented by generated error structs that
// exposes the name of the error as defined in the design.
type ErrorNamer interface {
ErrorName() string
}
// MountPoint holds information about the mounted endpoints.
type MountPoint struct {
// Method is the name of the service method served by the mounted HTTP handler.
Method string
// Verb is the HTTP method used to match requests to the mounted handler.
Verb string
// Pattern is the HTTP request path pattern used to match requests to the
// mounted handler.
Pattern string
}
// New instantiates HTTP handlers for all the notifications service endpoints
// using the provided encoder and decoder. The handlers are mounted on the
// given mux using the HTTP verb and path defined in the design. errhandler is
// called whenever a response fails to be encoded. formatter is used to format
// errors returned by the service methods prior to encoding. Both errhandler
// and formatter are optional and can be nil.
func New(
e *notifications.Endpoints,
mux goahttp.Muxer,
decoder func(*http.Request) goahttp.Decoder,
encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
errhandler func(context.Context, http.ResponseWriter, error),
formatter func(err error) goahttp.Statuser,
upgrader goahttp.Upgrader,
configurer *ConnConfigurer,
) *Server {
if configurer == nil {
configurer = &ConnConfigurer{}
}
return &Server{
Mounts: []*MountPoint{
{"Listen", "GET", "/notifications"},
{"Seen", "POST", "/notifications/seen"},
{"CORS", "OPTIONS", "/notifications"},
{"CORS", "OPTIONS", "/notifications/seen"},
},
Listen: NewListenHandler(e.Listen, mux, decoder, encoder, errhandler, formatter, upgrader, configurer.ListenFn),
Seen: NewSeenHandler(e.Seen, mux, decoder, encoder, errhandler, formatter),
CORS: NewCORSHandler(),
}
}
// Service returns the name of the service served.
func (s *Server) Service() string { return "notifications" }
// Use wraps the server handlers with the given middleware.
func (s *Server) Use(m func(http.Handler) http.Handler) {
s.Listen = m(s.Listen)
s.Seen = m(s.Seen)
s.CORS = m(s.CORS)
}
// Mount configures the mux to serve the notifications endpoints.
func Mount(mux goahttp.Muxer, h *Server) {
MountListenHandler(mux, h.Listen)
MountSeenHandler(mux, h.Seen)
MountCORSHandler(mux, h.CORS)
}
// MountListenHandler configures the mux to serve the "notifications" service
// "listen" endpoint.
func MountListenHandler(mux goahttp.Muxer, h http.Handler) {
f, ok := handleNotificationsOrigin(h).(http.HandlerFunc)
if !ok {
f = func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r)
}
}
mux.Handle("GET", "/notifications", f)
}
// NewListenHandler creates a HTTP handler which loads the HTTP request and
// calls the "notifications" service "listen" endpoint.
func NewListenHandler(
endpoint goa.Endpoint,
mux goahttp.Muxer,
decoder func(*http.Request) goahttp.Decoder,
encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
errhandler func(context.Context, http.ResponseWriter, error),
formatter func(err error) goahttp.Statuser,
upgrader goahttp.Upgrader,
configurer goahttp.ConnConfigureFunc,
) http.Handler {
var (
encodeError = EncodeListenError(encoder, formatter)
)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
ctx = context.WithValue(ctx, goa.MethodKey, "listen")
ctx = context.WithValue(ctx, goa.ServiceKey, "notifications")
var err error
var cancel context.CancelFunc
ctx, cancel = context.WithCancel(ctx)
v := ¬ifications.ListenEndpointInput{
Stream: &ListenServerStream{
upgrader: upgrader,
configurer: configurer,
cancel: cancel,
w: w,
r: r,
},
}
_, err = endpoint(ctx, v)
if err != nil {
if _, ok := err.(websocket.HandshakeError); ok {
return
}
if err := encodeError(ctx, w, err); err != nil {
errhandler(ctx, w, err)
}
return
}
})
}
// MountSeenHandler configures the mux to serve the "notifications" service
// "seen" endpoint.
func MountSeenHandler(mux goahttp.Muxer, h http.Handler) {
f, ok := handleNotificationsOrigin(h).(http.HandlerFunc)
if !ok {
f = func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r)
}
}
mux.Handle("POST", "/notifications/seen", f)
}
// NewSeenHandler creates a HTTP handler which loads the HTTP request and calls
// the "notifications" service "seen" endpoint.
func NewSeenHandler(
endpoint goa.Endpoint,
mux goahttp.Muxer,
decoder func(*http.Request) goahttp.Decoder,
encoder func(context.Context, http.ResponseWriter) goahttp.Encoder,
errhandler func(context.Context, http.ResponseWriter, error),
formatter func(err error) goahttp.Statuser,
) http.Handler {
var (
decodeRequest = DecodeSeenRequest(mux, decoder)
encodeResponse = EncodeSeenResponse(encoder)
encodeError = EncodeSeenError(encoder, formatter)
)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), goahttp.AcceptTypeKey, r.Header.Get("Accept"))
ctx = context.WithValue(ctx, goa.MethodKey, "seen")
ctx = context.WithValue(ctx, goa.ServiceKey, "notifications")
payload, err := decodeRequest(r)
if err != nil {
if err := encodeError(ctx, w, err); err != nil {
errhandler(ctx, w, err)
}
return
}
res, err := endpoint(ctx, payload)
if err != nil {
if err := encodeError(ctx, w, err); err != nil {
errhandler(ctx, w, err)
}
return
}
if err := encodeResponse(ctx, w, res); err != nil {
errhandler(ctx, w, err)
}
})
}
// MountCORSHandler configures the mux to serve the CORS endpoints for the
// service notifications.
func MountCORSHandler(mux goahttp.Muxer, h http.Handler) {
h = handleNotificationsOrigin(h)
f, ok := h.(http.HandlerFunc)
if !ok {
f = func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r)
}
}
mux.Handle("OPTIONS", "/notifications", f)
mux.Handle("OPTIONS", "/notifications/seen", f)
}
// NewCORSHandler creates a HTTP handler which returns a simple 200 response.
func NewCORSHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
})
}
// handleNotificationsOrigin applies the CORS response headers corresponding to
// the origin for the service notifications.
func handleNotificationsOrigin(h http.Handler) http.Handler {
spec0 := regexp.MustCompile("(.+[.])?fklocal.org:\\d+")
spec1 := regexp.MustCompile("127.0.0.1:\\d+")
spec2 := regexp.MustCompile("192.168.(\\d+).(\\d+):\\d+")
origHndlr := h.(http.HandlerFunc)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
origin := r.Header.Get("Origin")
if origin == "" {
// Not a CORS request
origHndlr(w, r)
return
}
if cors.MatchOriginRegexp(origin, spec0) {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Vary", "Origin")
w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
w.Header().Set("Access-Control-Allow-Credentials", "false")
if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {
// We are handling a preflight request
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
}
origHndlr(w, r)
return
}
if cors.MatchOriginRegexp(origin, spec1) {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Vary", "Origin")
w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
w.Header().Set("Access-Control-Allow-Credentials", "false")
if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {
// We are handling a preflight request
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
}
origHndlr(w, r)
return
}
if cors.MatchOriginRegexp(origin, spec2) {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Vary", "Origin")
w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
w.Header().Set("Access-Control-Allow-Credentials", "false")
if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {
// We are handling a preflight request
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
}
origHndlr(w, r)
return
}
if cors.MatchOrigin(origin, "https://*.fieldkit.org") {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Vary", "Origin")
w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
w.Header().Set("Access-Control-Allow-Credentials", "false")
if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {
// We are handling a preflight request
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
}
origHndlr(w, r)
return
}
if cors.MatchOrigin(origin, "https://*.fkdev.org") {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Vary", "Origin")
w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
w.Header().Set("Access-Control-Allow-Credentials", "false")
if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {
// We are handling a preflight request
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
}
origHndlr(w, r)
return
}
if cors.MatchOrigin(origin, "https://dataviz.floodnet.nyc") {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Vary", "Origin")
w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
w.Header().Set("Access-Control-Allow-Credentials", "false")
if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {
// We are handling a preflight request
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
}
origHndlr(w, r)
return
}
if cors.MatchOrigin(origin, "https://fieldkit.org") {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Vary", "Origin")
w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
w.Header().Set("Access-Control-Allow-Credentials", "false")
if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {
// We are handling a preflight request
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
}
origHndlr(w, r)
return
}
if cors.MatchOrigin(origin, "https://fkdev.org") {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Vary", "Origin")
w.Header().Set("Access-Control-Expose-Headers", "Authorization, Content-Type")
w.Header().Set("Access-Control-Allow-Credentials", "false")
if acrm := r.Header.Get("Access-Control-Request-Method"); acrm != "" {
// We are handling a preflight request
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST, DELETE, PATCH, PUT")
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
}
origHndlr(w, r)
return
}
origHndlr(w, r)
return
})
}