@@ -12,8 +12,8 @@ import (
12
12
"strings"
13
13
14
14
"github.com/golang/gddo/httputil"
15
- "github.com/julienschmidt/httprouter"
16
15
"github.com/manyminds/api2go/jsonapi"
16
+ "github.com/manyminds/api2go/routing"
17
17
)
18
18
19
19
const defaultContentTypHeader = "application/vnd.api+json"
@@ -235,19 +235,25 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
235
235
marshalers : marshalers ,
236
236
}
237
237
238
- requestInfo := func (r * http.Request ) * information {
238
+ requestInfo := func (r * http.Request , api * API ) * information {
239
239
var info * information
240
240
if resolver , ok := api .info .resolver .(RequestAwareURLResolver ); ok {
241
241
resolver .SetRequest (* r )
242
- info = & information {prefix : api .prefix , resolver : resolver }
242
+ info = & information {prefix : api .info . prefix , resolver : resolver }
243
243
} else {
244
244
info = & api .info
245
245
}
246
246
247
247
return info
248
248
}
249
249
250
- api .router .Handle ("OPTIONS" , api .prefix + name , func (w http.ResponseWriter , r * http.Request , ps httprouter.Params ) {
250
+ prefix := strings .Trim (api .info .prefix , "/" )
251
+ baseURL := "/" + name
252
+ if prefix != "" {
253
+ baseURL = "/" + prefix + baseURL
254
+ }
255
+
256
+ api .router .Handle ("OPTIONS" , baseURL , func (w http.ResponseWriter , r * http.Request , _ map [string ]string ) {
251
257
c := api .contextPool .Get ().(APIContexter )
252
258
c .Reset ()
253
259
api .middlewareChain (c , w , r )
@@ -256,7 +262,7 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
256
262
api .contextPool .Put (c )
257
263
})
258
264
259
- api .router .Handle ("OPTIONS" , api . prefix + name + "/:id" , func (w http.ResponseWriter , r * http.Request , ps httprouter. Params ) {
265
+ api .router .Handle ("OPTIONS" , baseURL + "/:id" , func (w http.ResponseWriter , r * http.Request , _ map [ string ] string ) {
260
266
c := api .contextPool .Get ().(APIContexter )
261
267
c .Reset ()
262
268
api .middlewareChain (c , w , r )
@@ -265,8 +271,8 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
265
271
api .contextPool .Put (c )
266
272
})
267
273
268
- api .router .GET ( api . prefix + name , func (w http.ResponseWriter , r * http.Request , _ httprouter. Params ) {
269
- info := requestInfo (r )
274
+ api .router .Handle ( "GET" , baseURL , func (w http.ResponseWriter , r * http.Request , _ map [ string ] string ) {
275
+ info := requestInfo (r , api )
270
276
c := api .contextPool .Get ().(APIContexter )
271
277
c .Reset ()
272
278
api .middlewareChain (c , w , r )
@@ -278,12 +284,12 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
278
284
}
279
285
})
280
286
281
- api .router .GET ( api . prefix + name + "/:id" , func (w http.ResponseWriter , r * http.Request , ps httprouter. Params ) {
282
- info := requestInfo (r )
287
+ api .router .Handle ( "GET" , baseURL + "/:id" , func (w http.ResponseWriter , r * http.Request , params map [ string ] string ) {
288
+ info := requestInfo (r , api )
283
289
c := api .contextPool .Get ().(APIContexter )
284
290
c .Reset ()
285
291
api .middlewareChain (c , w , r )
286
- err := res .handleRead (c , w , r , ps , * info )
292
+ err := res .handleRead (c , w , r , params , * info )
287
293
api .contextPool .Put (c )
288
294
if err != nil {
289
295
handleError (err , w , r , marshalers )
@@ -295,40 +301,40 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
295
301
if ok {
296
302
relations := casted .GetReferences ()
297
303
for _ , relation := range relations {
298
- api .router .GET ( api . prefix + name + "/:id/relationships/" + relation .Name , func (relation jsonapi.Reference ) httprouter. Handle {
299
- return func (w http.ResponseWriter , r * http.Request , ps httprouter. Params ) {
300
- info := requestInfo (r )
304
+ api .router .Handle ( "GET" , baseURL + "/:id/relationships/" + relation .Name , func (relation jsonapi.Reference ) routing. HandlerFunc {
305
+ return func (w http.ResponseWriter , r * http.Request , params map [ string ] string ) {
306
+ info := requestInfo (r , api )
301
307
c := api .contextPool .Get ().(APIContexter )
302
308
c .Reset ()
303
309
api .middlewareChain (c , w , r )
304
- err := res .handleReadRelation (c , w , r , ps , * info , relation )
310
+ err := res .handleReadRelation (c , w , r , params , * info , relation )
305
311
api .contextPool .Put (c )
306
312
if err != nil {
307
313
handleError (err , w , r , marshalers )
308
314
}
309
315
}
310
316
}(relation ))
311
317
312
- api .router .GET ( api . prefix + name + "/:id/" + relation .Name , func (relation jsonapi.Reference ) httprouter. Handle {
313
- return func (w http.ResponseWriter , r * http.Request , ps httprouter. Params ) {
314
- info := requestInfo (r )
318
+ api .router .Handle ( "GET" , baseURL + "/:id/" + relation .Name , func (relation jsonapi.Reference ) routing. HandlerFunc {
319
+ return func (w http.ResponseWriter , r * http.Request , params map [ string ] string ) {
320
+ info := requestInfo (r , api )
315
321
c := api .contextPool .Get ().(APIContexter )
316
322
c .Reset ()
317
323
api .middlewareChain (c , w , r )
318
- err := res .handleLinked (c , api , w , r , ps , relation , * info )
324
+ err := res .handleLinked (c , api , w , r , params , relation , * info )
319
325
api .contextPool .Put (c )
320
326
if err != nil {
321
327
handleError (err , w , r , marshalers )
322
328
}
323
329
}
324
330
}(relation ))
325
331
326
- api .router .PATCH ( api . prefix + name + "/:id/relationships/" + relation .Name , func (relation jsonapi.Reference ) httprouter. Handle {
327
- return func (w http.ResponseWriter , r * http.Request , ps httprouter. Params ) {
332
+ api .router .Handle ( "PATCH" , baseURL + "/:id/relationships/" + relation .Name , func (relation jsonapi.Reference ) routing. HandlerFunc {
333
+ return func (w http.ResponseWriter , r * http.Request , params map [ string ] string ) {
328
334
c := api .contextPool .Get ().(APIContexter )
329
335
c .Reset ()
330
336
api .middlewareChain (c , w , r )
331
- err := res .handleReplaceRelation (c , w , r , ps , relation )
337
+ err := res .handleReplaceRelation (c , w , r , params , relation )
332
338
api .contextPool .Put (c )
333
339
if err != nil {
334
340
handleError (err , w , r , marshalers )
@@ -338,25 +344,25 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
338
344
339
345
if _ , ok := ptrPrototype .(jsonapi.EditToManyRelations ); ok && relation .Name == jsonapi .Pluralize (relation .Name ) {
340
346
// generate additional routes to manipulate to-many relationships
341
- api .router .POST ( api . prefix + name + "/:id/relationships/" + relation .Name , func (relation jsonapi.Reference ) httprouter. Handle {
342
- return func (w http.ResponseWriter , r * http.Request , ps httprouter. Params ) {
347
+ api .router .Handle ( "POST" , baseURL + "/:id/relationships/" + relation .Name , func (relation jsonapi.Reference ) routing. HandlerFunc {
348
+ return func (w http.ResponseWriter , r * http.Request , params map [ string ] string ) {
343
349
c := api .contextPool .Get ().(APIContexter )
344
350
c .Reset ()
345
351
api .middlewareChain (c , w , r )
346
- err := res .handleAddToManyRelation (c , w , r , ps , relation )
352
+ err := res .handleAddToManyRelation (c , w , r , params , relation )
347
353
api .contextPool .Put (c )
348
354
if err != nil {
349
355
handleError (err , w , r , marshalers )
350
356
}
351
357
}
352
358
}(relation ))
353
359
354
- api .router .DELETE ( api . prefix + name + "/:id/relationships/" + relation .Name , func (relation jsonapi.Reference ) httprouter. Handle {
355
- return func (w http.ResponseWriter , r * http.Request , ps httprouter. Params ) {
360
+ api .router .Handle ( "DELETE" , baseURL + "/:id/relationships/" + relation .Name , func (relation jsonapi.Reference ) routing. HandlerFunc {
361
+ return func (w http.ResponseWriter , r * http.Request , params map [ string ] string ) {
356
362
c := api .contextPool .Get ().(APIContexter )
357
363
c .Reset ()
358
364
api .middlewareChain (c , w , r )
359
- err := res .handleDeleteToManyRelation (c , w , r , ps , relation )
365
+ err := res .handleDeleteToManyRelation (c , w , r , params , relation )
360
366
api .contextPool .Put (c )
361
367
if err != nil {
362
368
handleError (err , w , r , marshalers )
@@ -367,34 +373,34 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
367
373
}
368
374
}
369
375
370
- api .router .POST ( api . prefix + name , func (w http.ResponseWriter , r * http.Request , ps httprouter. Params ) {
371
- info := requestInfo (r )
376
+ api .router .Handle ( "POST" , baseURL , func (w http.ResponseWriter , r * http.Request , params map [ string ] string ) {
377
+ info := requestInfo (r , api )
372
378
c := api .contextPool .Get ().(APIContexter )
373
379
c .Reset ()
374
380
api .middlewareChain (c , w , r )
375
- err := res .handleCreate (c , w , r , api .prefix , * info )
381
+ err := res .handleCreate (c , w , r , info .prefix , * info )
376
382
api .contextPool .Put (c )
377
383
if err != nil {
378
384
handleError (err , w , r , marshalers )
379
385
}
380
386
})
381
387
382
- api .router .DELETE ( api . prefix + name + "/:id" , func (w http.ResponseWriter , r * http.Request , ps httprouter. Params ) {
388
+ api .router .Handle ( "DELETE" , baseURL + "/:id" , func (w http.ResponseWriter , r * http.Request , params map [ string ] string ) {
383
389
c := api .contextPool .Get ().(APIContexter )
384
390
c .Reset ()
385
391
api .middlewareChain (c , w , r )
386
- err := res .handleDelete (c , w , r , ps )
392
+ err := res .handleDelete (c , w , r , params )
387
393
api .contextPool .Put (c )
388
394
if err != nil {
389
395
handleError (err , w , r , marshalers )
390
396
}
391
397
})
392
398
393
- api .router .PATCH ( api . prefix + name + "/:id" , func (w http.ResponseWriter , r * http.Request , ps httprouter. Params ) {
399
+ api .router .Handle ( "PATCH" , baseURL + "/:id" , func (w http.ResponseWriter , r * http.Request , params map [ string ] string ) {
394
400
c := api .contextPool .Get ().(APIContexter )
395
401
c .Reset ()
396
402
api .middlewareChain (c , w , r )
397
- err := res .handleUpdate (c , w , r , ps )
403
+ err := res .handleUpdate (c , w , r , params )
398
404
api .contextPool .Put (c )
399
405
if err != nil {
400
406
handleError (err , w , r , marshalers )
@@ -451,8 +457,8 @@ func (res *resource) handleIndex(c APIContexter, w http.ResponseWriter, r *http.
451
457
return respondWith (response , info , http .StatusOK , w , r , res .marshalers )
452
458
}
453
459
454
- func (res * resource ) handleRead (c APIContexter , w http.ResponseWriter , r * http.Request , ps httprouter. Params , info information ) error {
455
- id := ps . ByName ( "id" )
460
+ func (res * resource ) handleRead (c APIContexter , w http.ResponseWriter , r * http.Request , params map [ string ] string , info information ) error {
461
+ id := params [ "id" ]
456
462
457
463
response , err := res .source .FindOne (id , buildRequest (c , r ))
458
464
@@ -463,8 +469,8 @@ func (res *resource) handleRead(c APIContexter, w http.ResponseWriter, r *http.R
463
469
return respondWith (response , info , http .StatusOK , w , r , res .marshalers )
464
470
}
465
471
466
- func (res * resource ) handleReadRelation (c APIContexter , w http.ResponseWriter , r * http.Request , ps httprouter. Params , info information , relation jsonapi.Reference ) error {
467
- id := ps . ByName ( "id" )
472
+ func (res * resource ) handleReadRelation (c APIContexter , w http.ResponseWriter , r * http.Request , params map [ string ] string , info information , relation jsonapi.Reference ) error {
473
+ id := params [ "id" ]
468
474
469
475
obj , err := res .source .FindOne (id , buildRequest (c , r ))
470
476
if err != nil {
@@ -519,8 +525,8 @@ func (res *resource) handleReadRelation(c APIContexter, w http.ResponseWriter, r
519
525
}
520
526
521
527
// try to find the referenced resource and call the findAll Method with referencing resource id as param
522
- func (res * resource ) handleLinked (c APIContexter , api * API , w http.ResponseWriter , r * http.Request , ps httprouter. Params , linked jsonapi.Reference , info information ) error {
523
- id := ps . ByName ( "id" )
528
+ func (res * resource ) handleLinked (c APIContexter , api * API , w http.ResponseWriter , r * http.Request , params map [ string ] string , linked jsonapi.Reference , info information ) error {
529
+ id := params [ "id" ]
524
530
for _ , resource := range api .resources {
525
531
if resource .name == linked .Type {
526
532
request := buildRequest (c , r )
@@ -607,7 +613,8 @@ func (res *resource) handleCreate(c APIContexter, w http.ResponseWriter, r *http
607
613
if ! ok {
608
614
return fmt .Errorf ("Expected one newly created object by resource %s" , res .name )
609
615
}
610
- w .Header ().Set ("Location" , prefix + res .name + "/" + result .GetID ())
616
+
617
+ w .Header ().Set ("Location" , "/" + prefix + "/" + res .name + "/" + result .GetID ())
611
618
612
619
// handle 200 status codes
613
620
switch response .StatusCode () {
@@ -624,8 +631,9 @@ func (res *resource) handleCreate(c APIContexter, w http.ResponseWriter, r *http
624
631
}
625
632
}
626
633
627
- func (res * resource ) handleUpdate (c APIContexter , w http.ResponseWriter , r * http.Request , ps httprouter.Params ) error {
628
- obj , err := res .source .FindOne (ps .ByName ("id" ), buildRequest (c , r ))
634
+ func (res * resource ) handleUpdate (c APIContexter , w http.ResponseWriter , r * http.Request , params map [string ]string ) error {
635
+ id := params ["id" ]
636
+ obj , err := res .source .FindOne (id , buildRequest (c , r ))
629
637
if err != nil {
630
638
return err
631
639
}
@@ -698,7 +706,7 @@ func (res *resource) handleUpdate(c APIContexter, w http.ResponseWriter, r *http
698
706
case http .StatusOK :
699
707
updated := response .Result ()
700
708
if updated == nil {
701
- internalResponse , err := res .source .FindOne (ps . ByName ( "id" ) , buildRequest (c , r ))
709
+ internalResponse , err := res .source .FindOne (id , buildRequest (c , r ))
702
710
if err != nil {
703
711
return err
704
712
}
@@ -722,13 +730,15 @@ func (res *resource) handleUpdate(c APIContexter, w http.ResponseWriter, r *http
722
730
}
723
731
}
724
732
725
- func (res * resource ) handleReplaceRelation (c APIContexter , w http.ResponseWriter , r * http.Request , ps httprouter. Params , relation jsonapi.Reference ) error {
733
+ func (res * resource ) handleReplaceRelation (c APIContexter , w http.ResponseWriter , r * http.Request , params map [ string ] string , relation jsonapi.Reference ) error {
726
734
var (
727
735
err error
728
736
editObj interface {}
729
737
)
730
738
731
- response , err := res .source .FindOne (ps .ByName ("id" ), buildRequest (c , r ))
739
+ id := params ["id" ]
740
+
741
+ response , err := res .source .FindOne (id , buildRequest (c , r ))
732
742
if err != nil {
733
743
return err
734
744
}
@@ -765,13 +775,15 @@ func (res *resource) handleReplaceRelation(c APIContexter, w http.ResponseWriter
765
775
return err
766
776
}
767
777
768
- func (res * resource ) handleAddToManyRelation (c APIContexter , w http.ResponseWriter , r * http.Request , ps httprouter. Params , relation jsonapi.Reference ) error {
778
+ func (res * resource ) handleAddToManyRelation (c APIContexter , w http.ResponseWriter , r * http.Request , params map [ string ] string , relation jsonapi.Reference ) error {
769
779
var (
770
780
err error
771
781
editObj interface {}
772
782
)
773
783
774
- response , err := res .source .FindOne (ps .ByName ("id" ), buildRequest (c , r ))
784
+ id := params ["id" ]
785
+
786
+ response , err := res .source .FindOne (id , buildRequest (c , r ))
775
787
if err != nil {
776
788
return err
777
789
}
@@ -830,12 +842,15 @@ func (res *resource) handleAddToManyRelation(c APIContexter, w http.ResponseWrit
830
842
return err
831
843
}
832
844
833
- func (res * resource ) handleDeleteToManyRelation (c APIContexter , w http.ResponseWriter , r * http.Request , ps httprouter. Params , relation jsonapi.Reference ) error {
845
+ func (res * resource ) handleDeleteToManyRelation (c APIContexter , w http.ResponseWriter , r * http.Request , params map [ string ] string , relation jsonapi.Reference ) error {
834
846
var (
835
847
err error
836
848
editObj interface {}
837
849
)
838
- response , err := res .source .FindOne (ps .ByName ("id" ), buildRequest (c , r ))
850
+
851
+ id := params ["id" ]
852
+
853
+ response , err := res .source .FindOne (id , buildRequest (c , r ))
839
854
if err != nil {
840
855
return err
841
856
}
@@ -902,8 +917,9 @@ func getPointerToStruct(oldObj interface{}) interface{} {
902
917
return ptr .Interface ()
903
918
}
904
919
905
- func (res * resource ) handleDelete (c APIContexter , w http.ResponseWriter , r * http.Request , ps httprouter.Params ) error {
906
- response , err := res .source .Delete (ps .ByName ("id" ), buildRequest (c , r ))
920
+ func (res * resource ) handleDelete (c APIContexter , w http.ResponseWriter , r * http.Request , params map [string ]string ) error {
921
+ id := params ["id" ]
922
+ response , err := res .source .Delete (id , buildRequest (c , r ))
907
923
if err != nil {
908
924
return err
909
925
}
0 commit comments