-
Notifications
You must be signed in to change notification settings - Fork 1
/
controllers.go
364 lines (333 loc) · 13.1 KB
/
controllers.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//************************************************************************//
// API "mara": Application Controllers
//
// Generated with goagen v1.0.0, command line:
// $ goagen
// --design=github.com/codeclysm/mara-api/design
// --out=$(GOPATH)/src/github.com/codeclysm/mara-api
// --version=v1.0.0
//
// The content of this file is auto-generated, DO NOT MODIFY
//************************************************************************//
package app
import (
"github.com/goadesign/goa"
"github.com/goadesign/goa/cors"
"golang.org/x/net/context"
"net/http"
)
// initService sets up the service encoders, decoders and mux.
func initService(service *goa.Service) {
// Setup encoders and decoders
service.Encoder.Register(goa.NewJSONEncoder, "application/json")
service.Encoder.Register(goa.NewGobEncoder, "application/gob", "application/x-gob")
service.Encoder.Register(goa.NewXMLEncoder, "application/xml")
service.Decoder.Register(goa.NewJSONDecoder, "application/json")
service.Decoder.Register(goa.NewGobDecoder, "application/gob", "application/x-gob")
service.Decoder.Register(goa.NewXMLDecoder, "application/xml")
// Setup default encoder and decoder
service.Encoder.Register(goa.NewJSONEncoder, "*/*")
service.Decoder.Register(goa.NewJSONDecoder, "*/*")
}
// AuthController is the controller interface for the Auth actions.
type AuthController interface {
goa.Muxer
Login(*LoginAuthContext) error
Reset(*ResetAuthContext) error
}
// MountAuthController "mounts" a Auth resource controller on the given service.
func MountAuthController(service *goa.Service, ctrl AuthController) {
initService(service)
var h goa.Handler
service.Mux.Handle("OPTIONS", "/auth/login", ctrl.MuxHandler("preflight", handleAuthOrigin(cors.HandlePreflight()), nil))
service.Mux.Handle("OPTIONS", "/auth/reset", ctrl.MuxHandler("preflight", handleAuthOrigin(cors.HandlePreflight()), nil))
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
// Check if there was an error loading the request
if err := goa.ContextError(ctx); err != nil {
return err
}
// Build the context
rctx, err := NewLoginAuthContext(ctx, service)
if err != nil {
return err
}
// Build the payload
if rawPayload := goa.ContextRequest(ctx).Payload; rawPayload != nil {
rctx.Payload = rawPayload.(*Login)
} else {
return goa.MissingPayloadError()
}
return ctrl.Login(rctx)
}
h = handleAuthOrigin(h)
service.Mux.Handle("POST", "/auth/login", ctrl.MuxHandler("Login", h, unmarshalLoginAuthPayload))
service.LogInfo("mount", "ctrl", "Auth", "action", "Login", "route", "POST /auth/login")
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
// Check if there was an error loading the request
if err := goa.ContextError(ctx); err != nil {
return err
}
// Build the context
rctx, err := NewResetAuthContext(ctx, service)
if err != nil {
return err
}
// Build the payload
if rawPayload := goa.ContextRequest(ctx).Payload; rawPayload != nil {
rctx.Payload = rawPayload.(*Reset)
} else {
return goa.MissingPayloadError()
}
return ctrl.Reset(rctx)
}
h = handleAuthOrigin(h)
service.Mux.Handle("POST", "/auth/reset", ctrl.MuxHandler("Reset", h, unmarshalResetAuthPayload))
service.LogInfo("mount", "ctrl", "Auth", "action", "Reset", "route", "POST /auth/reset")
}
// handleAuthOrigin applies the CORS response headers corresponding to the origin.
func handleAuthOrigin(h goa.Handler) goa.Handler {
return func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
origin := req.Header.Get("Origin")
if origin == "" {
// Not a CORS request
return h(ctx, rw, req)
}
if cors.MatchOrigin(origin, "*") {
ctx = goa.WithLogContext(ctx, "origin", origin)
rw.Header().Set("Access-Control-Allow-Origin", origin)
rw.Header().Set("Access-Control-Allow-Credentials", "true")
if acrm := req.Header.Get("Access-Control-Request-Method"); acrm != "" {
// We are handling a preflight request
rw.Header().Set("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE")
rw.Header().Set("Access-Control-Allow-Headers", "Authorization, Origin, X-Requested-With, Content-Type, Accept")
}
return h(ctx, rw, req)
}
return h(ctx, rw, req)
}
}
// unmarshalLoginAuthPayload unmarshals the request body into the context request data Payload field.
func unmarshalLoginAuthPayload(ctx context.Context, service *goa.Service, req *http.Request) error {
payload := &login{}
if err := service.DecodeRequest(req, payload); err != nil {
return err
}
if err := payload.Validate(); err != nil {
// Initialize payload with private data structure so it can be logged
goa.ContextRequest(ctx).Payload = payload
return err
}
goa.ContextRequest(ctx).Payload = payload.Publicize()
return nil
}
// unmarshalResetAuthPayload unmarshals the request body into the context request data Payload field.
func unmarshalResetAuthPayload(ctx context.Context, service *goa.Service, req *http.Request) error {
payload := &reset{}
if err := service.DecodeRequest(req, payload); err != nil {
return err
}
if err := payload.Validate(); err != nil {
// Initialize payload with private data structure so it can be logged
goa.ContextRequest(ctx).Payload = payload
return err
}
goa.ContextRequest(ctx).Payload = payload.Publicize()
return nil
}
// CalendarController is the controller interface for the Calendar actions.
type CalendarController interface {
goa.Muxer
Create(*CreateCalendarContext) error
Delete(*DeleteCalendarContext) error
Edit(*EditCalendarContext) error
List(*ListCalendarContext) error
Show(*ShowCalendarContext) error
}
// MountCalendarController "mounts" a Calendar resource controller on the given service.
func MountCalendarController(service *goa.Service, ctrl CalendarController) {
initService(service)
var h goa.Handler
service.Mux.Handle("OPTIONS", "/appointments", ctrl.MuxHandler("preflight", handleCalendarOrigin(cors.HandlePreflight()), nil))
service.Mux.Handle("OPTIONS", "/appointments/:id", ctrl.MuxHandler("preflight", handleCalendarOrigin(cors.HandlePreflight()), nil))
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
// Check if there was an error loading the request
if err := goa.ContextError(ctx); err != nil {
return err
}
// Build the context
rctx, err := NewCreateCalendarContext(ctx, service)
if err != nil {
return err
}
// Build the payload
if rawPayload := goa.ContextRequest(ctx).Payload; rawPayload != nil {
rctx.Payload = rawPayload.(*Appointment)
} else {
return goa.MissingPayloadError()
}
return ctrl.Create(rctx)
}
h = handleCalendarOrigin(h)
h = handleSecurity("jwt", h)
service.Mux.Handle("PUT", "/appointments", ctrl.MuxHandler("Create", h, unmarshalCreateCalendarPayload))
service.LogInfo("mount", "ctrl", "Calendar", "action", "Create", "route", "PUT /appointments", "security", "jwt")
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
// Check if there was an error loading the request
if err := goa.ContextError(ctx); err != nil {
return err
}
// Build the context
rctx, err := NewDeleteCalendarContext(ctx, service)
if err != nil {
return err
}
return ctrl.Delete(rctx)
}
h = handleCalendarOrigin(h)
h = handleSecurity("jwt", h)
service.Mux.Handle("DELETE", "/appointments/:id", ctrl.MuxHandler("Delete", h, nil))
service.LogInfo("mount", "ctrl", "Calendar", "action", "Delete", "route", "DELETE /appointments/:id", "security", "jwt")
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
// Check if there was an error loading the request
if err := goa.ContextError(ctx); err != nil {
return err
}
// Build the context
rctx, err := NewEditCalendarContext(ctx, service)
if err != nil {
return err
}
// Build the payload
if rawPayload := goa.ContextRequest(ctx).Payload; rawPayload != nil {
rctx.Payload = rawPayload.(*Appointment)
} else {
return goa.MissingPayloadError()
}
return ctrl.Edit(rctx)
}
h = handleCalendarOrigin(h)
h = handleSecurity("jwt", h)
service.Mux.Handle("POST", "/appointments/:id", ctrl.MuxHandler("Edit", h, unmarshalEditCalendarPayload))
service.LogInfo("mount", "ctrl", "Calendar", "action", "Edit", "route", "POST /appointments/:id", "security", "jwt")
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
// Check if there was an error loading the request
if err := goa.ContextError(ctx); err != nil {
return err
}
// Build the context
rctx, err := NewListCalendarContext(ctx, service)
if err != nil {
return err
}
return ctrl.List(rctx)
}
h = handleCalendarOrigin(h)
h = handleSecurity("jwt", h)
service.Mux.Handle("GET", "/appointments", ctrl.MuxHandler("List", h, nil))
service.LogInfo("mount", "ctrl", "Calendar", "action", "List", "route", "GET /appointments", "security", "jwt")
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
// Check if there was an error loading the request
if err := goa.ContextError(ctx); err != nil {
return err
}
// Build the context
rctx, err := NewShowCalendarContext(ctx, service)
if err != nil {
return err
}
return ctrl.Show(rctx)
}
h = handleCalendarOrigin(h)
h = handleSecurity("jwt", h)
service.Mux.Handle("GET", "/appointments/:id", ctrl.MuxHandler("Show", h, nil))
service.LogInfo("mount", "ctrl", "Calendar", "action", "Show", "route", "GET /appointments/:id", "security", "jwt")
}
// handleCalendarOrigin applies the CORS response headers corresponding to the origin.
func handleCalendarOrigin(h goa.Handler) goa.Handler {
return func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
origin := req.Header.Get("Origin")
if origin == "" {
// Not a CORS request
return h(ctx, rw, req)
}
if cors.MatchOrigin(origin, "*") {
ctx = goa.WithLogContext(ctx, "origin", origin)
rw.Header().Set("Access-Control-Allow-Origin", origin)
rw.Header().Set("Access-Control-Allow-Credentials", "true")
if acrm := req.Header.Get("Access-Control-Request-Method"); acrm != "" {
// We are handling a preflight request
rw.Header().Set("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE")
rw.Header().Set("Access-Control-Allow-Headers", "Authorization, Origin, X-Requested-With, Content-Type, Accept")
}
return h(ctx, rw, req)
}
return h(ctx, rw, req)
}
}
// unmarshalCreateCalendarPayload unmarshals the request body into the context request data Payload field.
func unmarshalCreateCalendarPayload(ctx context.Context, service *goa.Service, req *http.Request) error {
payload := &appointment{}
if err := service.DecodeRequest(req, payload); err != nil {
return err
}
payload.Finalize()
if err := payload.Validate(); err != nil {
// Initialize payload with private data structure so it can be logged
goa.ContextRequest(ctx).Payload = payload
return err
}
goa.ContextRequest(ctx).Payload = payload.Publicize()
return nil
}
// unmarshalEditCalendarPayload unmarshals the request body into the context request data Payload field.
func unmarshalEditCalendarPayload(ctx context.Context, service *goa.Service, req *http.Request) error {
payload := &appointment{}
if err := service.DecodeRequest(req, payload); err != nil {
return err
}
payload.Finalize()
if err := payload.Validate(); err != nil {
// Initialize payload with private data structure so it can be logged
goa.ContextRequest(ctx).Payload = payload
return err
}
goa.ContextRequest(ctx).Payload = payload.Publicize()
return nil
}
// PublicController is the controller interface for the Public actions.
type PublicController interface {
goa.Muxer
goa.FileServer
}
// MountPublicController "mounts" a Public resource controller on the given service.
func MountPublicController(service *goa.Service, ctrl PublicController) {
initService(service)
var h goa.Handler
service.Mux.Handle("OPTIONS", "/builder/v1/swagger.json", ctrl.MuxHandler("preflight", handlePublicOrigin(cors.HandlePreflight()), nil))
h = ctrl.FileHandler("/builder/v1/swagger.json", "swagger/swagger.json")
h = handlePublicOrigin(h)
service.Mux.Handle("GET", "/builder/v1/swagger.json", ctrl.MuxHandler("serve", h, nil))
service.LogInfo("mount", "ctrl", "Public", "files", "swagger/swagger.json", "route", "GET /builder/v1/swagger.json")
}
// handlePublicOrigin applies the CORS response headers corresponding to the origin.
func handlePublicOrigin(h goa.Handler) goa.Handler {
return func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
origin := req.Header.Get("Origin")
if origin == "" {
// Not a CORS request
return h(ctx, rw, req)
}
if cors.MatchOrigin(origin, "*") {
ctx = goa.WithLogContext(ctx, "origin", origin)
rw.Header().Set("Access-Control-Allow-Origin", origin)
rw.Header().Set("Access-Control-Allow-Credentials", "true")
if acrm := req.Header.Get("Access-Control-Request-Method"); acrm != "" {
// We are handling a preflight request
rw.Header().Set("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE")
rw.Header().Set("Access-Control-Allow-Headers", "Authorization, Origin, X-Requested-With, Content-Type, Accept")
}
return h(ctx, rw, req)
}
return h(ctx, rw, req)
}
}