-
Notifications
You must be signed in to change notification settings - Fork 66
/
handlers.go
583 lines (484 loc) · 15.4 KB
/
handlers.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
package server
import (
"encoding/json"
"net/http"
"github.com/rs/zerolog/hlog"
"github.com/gilcrest/diygoapi"
"github.com/gilcrest/diygoapi/errs"
)
// CreateMovie is a HandlerFunc used to create a Movie
func (s *Server) handleMovieCreate(w http.ResponseWriter, r *http.Request) {
logger := *hlog.FromRequest(r)
adt, err := diygoapi.AuditFromRequest(r)
if err != nil {
errs.HTTPErrorResponse(w, logger, err)
return
}
// Declare request body (rb) as an instance of service.MovieRequest
rb := new(diygoapi.CreateMovieRequest)
// Decode JSON HTTP request body into a Decoder type
// and unmarshal that into the CreateMovieRequest struct (rb)
err = json.NewDecoder(r.Body).Decode(&rb)
defer r.Body.Close()
// Call decoderErr to determine if body is nil, json is malformed
// or any other error
err = decoderErr(err)
if err != nil {
errs.HTTPErrorResponse(w, logger, err)
return
}
var response *diygoapi.MovieResponse
response, err = s.MovieServicer.Create(r.Context(), rb, adt)
if err != nil {
errs.HTTPErrorResponse(w, logger, err)
return
}
// Encode response struct to JSON for the response body
err = json.NewEncoder(w).Encode(response)
if err != nil {
errs.HTTPErrorResponse(w, logger, errs.E(errs.Internal, err))
return
}
}
// handleMovieUpdate handles PUT requests for the /movies/{id} endpoint
// and updates the given movie
func (s *Server) handleMovieUpdate(w http.ResponseWriter, r *http.Request) {
logger := *hlog.FromRequest(r)
adt, err := diygoapi.AuditFromRequest(r)
if err != nil {
errs.HTTPErrorResponse(w, logger, err)
return
}
// return the extlID from the Path
extlID := r.PathValue("extlID")
// Declare request body (rb) as an instance of service.MovieRequest
rb := new(diygoapi.UpdateMovieRequest)
// Decode JSON HTTP request body into a Decoder type
// and unmarshal that into requestData
err = json.NewDecoder(r.Body).Decode(&rb)
defer r.Body.Close()
// Call DecoderErr to determine if body is nil, json is malformed
// or any other error
err = decoderErr(err)
if err != nil {
errs.HTTPErrorResponse(w, logger, err)
return
}
// External ID is from path variable, need to set separate
// from decoding response body
rb.ExternalID = extlID
var response *diygoapi.MovieResponse
response, err = s.MovieServicer.Update(r.Context(), rb, adt)
if err != nil {
errs.HTTPErrorResponse(w, logger, err)
return
}
// Encode response struct to JSON for the response body
err = json.NewEncoder(w).Encode(response)
if err != nil {
errs.HTTPErrorResponse(w, logger, errs.E(errs.Internal, err))
return
}
}
// handleMovieDelete handles DELETE requests for the /movies/{id} endpoint
// and updates the given movie
func (s *Server) handleMovieDelete(w http.ResponseWriter, r *http.Request) {
logger := *hlog.FromRequest(r)
// return the extlID from the Path
extlID := r.PathValue("extlID")
response, err := s.MovieServicer.Delete(r.Context(), extlID)
if err != nil {
errs.HTTPErrorResponse(w, logger, err)
return
}
// Encode response struct to JSON for the response body
err = json.NewEncoder(w).Encode(response)
if err != nil {
errs.HTTPErrorResponse(w, logger, errs.E(errs.Internal, err))
return
}
}
// handleFindMovieByID handles GET requests for the /movies/{id} endpoint
// and finds a movie by its ID
func (s *Server) handleFindMovieByID(w http.ResponseWriter, r *http.Request) {
logger := *hlog.FromRequest(r)
// return the extlID from the Path
extlID := r.PathValue("extlID")
response, err := s.MovieServicer.FindMovieByExternalID(r.Context(), extlID)
if err != nil {
errs.HTTPErrorResponse(w, logger, err)
return
}
// Encode response struct to JSON for the response body
err = json.NewEncoder(w).Encode(response)
if err != nil {
errs.HTTPErrorResponse(w, logger, errs.E(errs.Internal, err))
return
}
}
// handleFindAllMovies handles GET requests for the /movies endpoint and finds
// all movies
func (s *Server) handleFindAllMovies(w http.ResponseWriter, r *http.Request) {
logger := *hlog.FromRequest(r)
response, err := s.MovieServicer.FindAllMovies(r.Context())
if err != nil {
errs.HTTPErrorResponse(w, logger, err)
return
}
// Encode response struct to JSON for the response body
err = json.NewEncoder(w).Encode(response)
if err != nil {
errs.HTTPErrorResponse(w, logger, errs.E(errs.Internal, err))
return
}
}
// handleOrgCreate is a HandlerFunc used to create an Org
func (s *Server) handleOrgCreate(w http.ResponseWriter, r *http.Request) {
lgr := *hlog.FromRequest(r)
adt, err := diygoapi.AuditFromRequest(r)
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
// Declare request body (rb) as an instance of service.MovieRequest
rb := new(diygoapi.CreateOrgRequest)
// Decode JSON HTTP request body into a Decoder type
// and unmarshal that into the MovieRequest struct in the
// AddMovieHandler
err = json.NewDecoder(r.Body).Decode(&rb)
defer r.Body.Close()
// Call decoderErr to determine if body is nil, json is malformed
// or any other error
err = decoderErr(err)
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
var response *diygoapi.OrgResponse
response, err = s.OrgServicer.Create(r.Context(), rb, adt)
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
// Encode response struct to JSON for the response body
err = json.NewEncoder(w).Encode(response)
if err != nil {
errs.HTTPErrorResponse(w, lgr, errs.E(errs.Internal, err))
return
}
}
// handleOrgUpdate is a HandlerFunc used to update an Org
func (s *Server) handleOrgUpdate(w http.ResponseWriter, r *http.Request) {
lgr := *hlog.FromRequest(r)
adt, err := diygoapi.AuditFromRequest(r)
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
// Declare request body (rb) as an instance of service.MovieRequest
rb := new(diygoapi.UpdateOrgRequest)
// Decode JSON HTTP request body into a Decoder type
// and unmarshal that into the MovieRequest struct in the
// AddMovieHandler
err = json.NewDecoder(r.Body).Decode(&rb)
defer r.Body.Close()
// Call decoderErr to determine if body is nil, json is malformed
// or any other error
err = decoderErr(err)
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
// return the extlID from the Path
rb.ExternalID = r.PathValue("extlID")
var response *diygoapi.OrgResponse
response, err = s.OrgServicer.Update(r.Context(), rb, adt)
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
// Encode response struct to JSON for the response body
err = json.NewEncoder(w).Encode(response)
if err != nil {
errs.HTTPErrorResponse(w, lgr, errs.E(errs.Internal, err))
return
}
}
// handleOrgDelete is a HandlerFunc used to delete an Org
func (s *Server) handleOrgDelete(w http.ResponseWriter, r *http.Request) {
lgr := *hlog.FromRequest(r)
// return the extlID from the Path
extlID := r.PathValue("extlID")
response, err := s.OrgServicer.Delete(r.Context(), extlID)
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
// Encode response struct to JSON for the response body
err = json.NewEncoder(w).Encode(response)
if err != nil {
errs.HTTPErrorResponse(w, lgr, errs.E(errs.Internal, err))
return
}
}
// handleOrgFindAll is a HandlerFunc used to find a list of Orgs
func (s *Server) handleOrgFindAll(w http.ResponseWriter, r *http.Request) {
lgr := *hlog.FromRequest(r)
response, err := s.OrgServicer.FindAll(r.Context())
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
// Encode response struct to JSON for the response body
err = json.NewEncoder(w).Encode(response)
if err != nil {
errs.HTTPErrorResponse(w, lgr, errs.E(errs.Internal, err))
return
}
}
// handleOrgFindByExtlID is a HandlerFunc used to find a specific Org by External ID
func (s *Server) handleOrgFindByExtlID(w http.ResponseWriter, r *http.Request) {
lgr := *hlog.FromRequest(r)
// return the extlID from the Path
extlID := r.PathValue("extlID")
response, err := s.OrgServicer.FindByExternalID(r.Context(), extlID)
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
// Encode response struct to JSON for the response body
err = json.NewEncoder(w).Encode(response)
if err != nil {
errs.HTTPErrorResponse(w, lgr, errs.E(errs.Internal, err))
return
}
}
// handleAppCreate is a HandlerFunc used to create an App
func (s *Server) handleAppCreate(w http.ResponseWriter, r *http.Request) {
lgr := *hlog.FromRequest(r)
adt, err := diygoapi.AuditFromRequest(r)
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
// Declare request body (rb)
rb := new(diygoapi.CreateAppRequest)
// Decode JSON HTTP request body into a Decoder type
// and unmarshal that into the MovieRequest struct in the
// AddMovieHandler
err = json.NewDecoder(r.Body).Decode(&rb)
defer r.Body.Close()
// Call decoderErr to determine if body is nil, json is malformed
// or any other error
err = decoderErr(err)
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
var response *diygoapi.AppResponse
response, err = s.AppServicer.Create(r.Context(), rb, adt)
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
// Encode response struct to JSON for the response body
err = json.NewEncoder(w).Encode(response)
if err != nil {
errs.HTTPErrorResponse(w, lgr, errs.E(errs.Internal, err))
return
}
}
// handleNewUser is a HandlerFunc used to register a User
func (s *Server) handleNewUser(w http.ResponseWriter, r *http.Request) {
lgr := *hlog.FromRequest(r)
params, err := s.AuthenticationServicer.NewAuthenticationParams(r, defaultRealm)
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
var response *diygoapi.UserResponse
response, err = s.AuthenticationServicer.SelfRegister(r.Context(), params)
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
// Encode response struct to JSON for the response body
err = json.NewEncoder(w).Encode(response)
if err != nil {
errs.HTTPErrorResponse(w, lgr, errs.E(errs.Internal, err))
return
}
}
// handleLoggerRead handles GET requests for the /logger endpoint
func (s *Server) handleLoggerRead(w http.ResponseWriter, r *http.Request) {
lgr := *hlog.FromRequest(r)
response := s.LoggerService.Read()
// Encode response struct to JSON for the response body
err := json.NewEncoder(w).Encode(response)
if err != nil {
errs.HTTPErrorResponse(w, lgr, errs.E(errs.Internal, err))
return
}
}
// handleLoggerUpdate handles PUT requests for the /logger endpoint
// and updates the logger globals
func (s *Server) handleLoggerUpdate(w http.ResponseWriter, r *http.Request) {
lgr := *hlog.FromRequest(r)
// Declare rb as an instance of service.LoggerRequest
rb := new(diygoapi.LoggerRequest)
// Decode JSON HTTP request body into a json.Decoder type
// and unmarshal that into rb
err := json.NewDecoder(r.Body).Decode(&rb)
defer r.Body.Close()
// Call DecoderErr to determine if body is nil, json is malformed
// or any other error
err = decoderErr(err)
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
var response *diygoapi.LoggerResponse
response, err = s.LoggerService.Update(rb)
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
// Encode response struct to JSON for the response body
err = json.NewEncoder(w).Encode(response)
if err != nil {
errs.HTTPErrorResponse(w, lgr, errs.E(errs.Internal, err))
return
}
}
// Ping handles GET requests for the /ping endpoint
func (s *Server) handlePing(w http.ResponseWriter, r *http.Request) {
// pull logger from request context
lgr := *hlog.FromRequest(r)
// pull the context from the http request
ctx := r.Context()
response := s.PingService.Ping(ctx, lgr)
// Encode response struct to JSON for the response body
err := json.NewEncoder(w).Encode(response)
if err != nil {
errs.HTTPErrorResponse(w, lgr, errs.E(errs.Internal, err))
return
}
}
// handleGenesis handles POST requests for the /genesis endpoint
func (s *Server) handleGenesis(w http.ResponseWriter, r *http.Request) {
lgr := *hlog.FromRequest(r)
// Declare rb as an instance of service.LoggerRequest
rb := new(diygoapi.GenesisRequest)
// Decode JSON HTTP request body into a json.Decoder type
// and unmarshal that into rb
err := json.NewDecoder(r.Body).Decode(&rb)
defer r.Body.Close()
// Call DecoderErr to determine if body is nil, json is malformed
// or any other error
err = decoderErr(err)
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
var response diygoapi.GenesisResponse
response, err = s.GenesisServicer.Arche(r.Context(), rb)
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
// Encode response struct to JSON for the response body
err = json.NewEncoder(w).Encode(response)
if err != nil {
errs.HTTPErrorResponse(w, lgr, errs.E(errs.Internal, err))
return
}
}
// handleGenesis handles GET requests for the /genesis endpoint
func (s *Server) handleGenesisRead(w http.ResponseWriter, r *http.Request) {
lgr := *hlog.FromRequest(r)
var (
response diygoapi.GenesisResponse
err error
)
response, err = s.GenesisServicer.ReadConfig()
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
// Encode response struct to JSON for the response body
err = json.NewEncoder(w).Encode(response)
if err != nil {
errs.HTTPErrorResponse(w, lgr, errs.E(errs.Internal, err))
return
}
}
// handlePermissionCreate handles POST requests for the /permission endpoint
func (s *Server) handlePermissionCreate(w http.ResponseWriter, r *http.Request) {
lgr := *hlog.FromRequest(r)
var (
err error
adt diygoapi.Audit
)
adt, err = diygoapi.AuditFromRequest(r)
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
// Declare rb as an instance of service.PermissionRequest
rb := new(diygoapi.CreatePermissionRequest)
// Decode JSON HTTP request body into a json.Decoder type
// and unmarshal that into rb
err = json.NewDecoder(r.Body).Decode(&rb)
defer r.Body.Close()
// Call DecoderErr to determine if body is nil, json is malformed
// or any other error
err = decoderErr(err)
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
var response *diygoapi.PermissionResponse
response, err = s.PermissionServicer.Create(r.Context(), rb, adt)
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
// Encode response struct to JSON for the response body
err = json.NewEncoder(w).Encode(response)
if err != nil {
errs.HTTPErrorResponse(w, lgr, errs.E(errs.Internal, err))
return
}
}
// handlePermissionFindAll handles GET requests for the /permission endpoint
func (s *Server) handlePermissionFindAll(w http.ResponseWriter, r *http.Request) {
lgr := *hlog.FromRequest(r)
response, err := s.PermissionServicer.FindAll(r.Context())
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
// Encode response struct to JSON for the response body
err = json.NewEncoder(w).Encode(response)
if err != nil {
errs.HTTPErrorResponse(w, lgr, errs.E(errs.Internal, err))
return
}
}
// handlePermissionDelete handles DELETE requests for the /permission endpoint
func (s *Server) handlePermissionDelete(w http.ResponseWriter, r *http.Request) {
lgr := *hlog.FromRequest(r)
// return the extlID from the Path
extlID := r.PathValue("extlID")
response, err := s.PermissionServicer.Delete(r.Context(), extlID)
if err != nil {
errs.HTTPErrorResponse(w, lgr, err)
return
}
// Encode response struct to JSON for the response body
err = json.NewEncoder(w).Encode(response)
if err != nil {
errs.HTTPErrorResponse(w, lgr, errs.E(errs.Internal, err))
return
}
}