@@ -250,19 +250,29 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source interfac
250250 baseURL = "/" + prefix + baseURL
251251 }
252252
253- api .router .Handle ("OPTIONS" , baseURL , func (w http.ResponseWriter , r * http.Request , _ map [string ]string ) {
253+ api .router .Handle ("OPTIONS" , baseURL , func (w http.ResponseWriter , r * http.Request , _ map [string ]string , context map [ string ] interface {} ) {
254254 c := api .contextPool .Get ().(APIContexter )
255255 c .Reset ()
256+
257+ for key , val := range context {
258+ c .Set (key , val )
259+ }
260+
256261 api .middlewareChain (c , w , r )
257262 w .Header ().Set ("Allow" , strings .Join (getAllowedMethods (source , true ), "," ))
258263 w .WriteHeader (http .StatusNoContent )
259264 api .contextPool .Put (c )
260265 })
261266
262- api .router .Handle ("GET" , baseURL , func (w http.ResponseWriter , r * http.Request , _ map [string ]string ) {
267+ api .router .Handle ("GET" , baseURL , func (w http.ResponseWriter , r * http.Request , _ map [string ]string , context map [ string ] interface {} ) {
263268 info := requestInfo (r , api )
264269 c := api .contextPool .Get ().(APIContexter )
265270 c .Reset ()
271+
272+ for key , val := range context {
273+ c .Set (key , val )
274+ }
275+
266276 api .middlewareChain (c , w , r )
267277
268278 err := res .handleIndex (c , w , r , * info )
@@ -273,19 +283,29 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source interfac
273283 })
274284
275285 if _ , ok := source .(ResourceGetter ); ok {
276- api .router .Handle ("OPTIONS" , baseURL + "/:id" , func (w http.ResponseWriter , r * http.Request , _ map [string ]string ) {
286+ api .router .Handle ("OPTIONS" , baseURL + "/:id" , func (w http.ResponseWriter , r * http.Request , _ map [string ]string , context map [ string ] interface {} ) {
277287 c := api .contextPool .Get ().(APIContexter )
278288 c .Reset ()
289+
290+ for key , val := range context {
291+ c .Set (key , val )
292+ }
293+
279294 api .middlewareChain (c , w , r )
280295 w .Header ().Set ("Allow" , strings .Join (getAllowedMethods (source , false ), "," ))
281296 w .WriteHeader (http .StatusNoContent )
282297 api .contextPool .Put (c )
283298 })
284299
285- api .router .Handle ("GET" , baseURL + "/:id" , func (w http.ResponseWriter , r * http.Request , params map [string ]string ) {
300+ api .router .Handle ("GET" , baseURL + "/:id" , func (w http.ResponseWriter , r * http.Request , params map [string ]string , context map [ string ] interface {} ) {
286301 info := requestInfo (r , api )
287302 c := api .contextPool .Get ().(APIContexter )
288303 c .Reset ()
304+
305+ for key , val := range context {
306+ c .Set (key , val )
307+ }
308+
289309 api .middlewareChain (c , w , r )
290310 err := res .handleRead (c , w , r , params , * info )
291311 api .contextPool .Put (c )
@@ -301,10 +321,15 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source interfac
301321 relations := casted .GetReferences ()
302322 for _ , relation := range relations {
303323 api .router .Handle ("GET" , baseURL + "/:id/relationships/" + relation .Name , func (relation jsonapi.Reference ) routing.HandlerFunc {
304- return func (w http.ResponseWriter , r * http.Request , params map [string ]string ) {
324+ return func (w http.ResponseWriter , r * http.Request , params map [string ]string , context map [ string ] interface {} ) {
305325 info := requestInfo (r , api )
306326 c := api .contextPool .Get ().(APIContexter )
307327 c .Reset ()
328+
329+ for key , val := range context {
330+ c .Set (key , val )
331+ }
332+
308333 api .middlewareChain (c , w , r )
309334 err := res .handleReadRelation (c , w , r , params , * info , relation )
310335 api .contextPool .Put (c )
@@ -315,10 +340,15 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source interfac
315340 }(relation ))
316341
317342 api .router .Handle ("GET" , baseURL + "/:id/" + relation .Name , func (relation jsonapi.Reference ) routing.HandlerFunc {
318- return func (w http.ResponseWriter , r * http.Request , params map [string ]string ) {
343+ return func (w http.ResponseWriter , r * http.Request , params map [string ]string , context map [ string ] interface {} ) {
319344 info := requestInfo (r , api )
320345 c := api .contextPool .Get ().(APIContexter )
321346 c .Reset ()
347+
348+ for key , val := range context {
349+ c .Set (key , val )
350+ }
351+
322352 api .middlewareChain (c , w , r )
323353 err := res .handleLinked (c , api , w , r , params , relation , * info )
324354 api .contextPool .Put (c )
@@ -329,9 +359,14 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source interfac
329359 }(relation ))
330360
331361 api .router .Handle ("PATCH" , baseURL + "/:id/relationships/" + relation .Name , func (relation jsonapi.Reference ) routing.HandlerFunc {
332- return func (w http.ResponseWriter , r * http.Request , params map [string ]string ) {
362+ return func (w http.ResponseWriter , r * http.Request , params map [string ]string , context map [ string ] interface {} ) {
333363 c := api .contextPool .Get ().(APIContexter )
334364 c .Reset ()
365+
366+ for key , val := range context {
367+ c .Set (key , val )
368+ }
369+
335370 api .middlewareChain (c , w , r )
336371 err := res .handleReplaceRelation (c , w , r , params , relation )
337372 api .contextPool .Put (c )
@@ -344,9 +379,14 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source interfac
344379 if _ , ok := ptrPrototype .(jsonapi.EditToManyRelations ); ok && relation .Name == jsonapi .Pluralize (relation .Name ) {
345380 // generate additional routes to manipulate to-many relationships
346381 api .router .Handle ("POST" , baseURL + "/:id/relationships/" + relation .Name , func (relation jsonapi.Reference ) routing.HandlerFunc {
347- return func (w http.ResponseWriter , r * http.Request , params map [string ]string ) {
382+ return func (w http.ResponseWriter , r * http.Request , params map [string ]string , context map [ string ] interface {} ) {
348383 c := api .contextPool .Get ().(APIContexter )
349384 c .Reset ()
385+
386+ for key , val := range context {
387+ c .Set (key , val )
388+ }
389+
350390 api .middlewareChain (c , w , r )
351391 err := res .handleAddToManyRelation (c , w , r , params , relation )
352392 api .contextPool .Put (c )
@@ -357,9 +397,14 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source interfac
357397 }(relation ))
358398
359399 api .router .Handle ("DELETE" , baseURL + "/:id/relationships/" + relation .Name , func (relation jsonapi.Reference ) routing.HandlerFunc {
360- return func (w http.ResponseWriter , r * http.Request , params map [string ]string ) {
400+ return func (w http.ResponseWriter , r * http.Request , params map [string ]string , context map [ string ] interface {} ) {
361401 c := api .contextPool .Get ().(APIContexter )
362402 c .Reset ()
403+
404+ for key , val := range context {
405+ c .Set (key , val )
406+ }
407+
363408 api .middlewareChain (c , w , r )
364409 err := res .handleDeleteToManyRelation (c , w , r , params , relation )
365410 api .contextPool .Put (c )
@@ -373,10 +418,15 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source interfac
373418 }
374419
375420 if _ , ok := source .(ResourceCreator ); ok {
376- api .router .Handle ("POST" , baseURL , func (w http.ResponseWriter , r * http.Request , params map [string ]string ) {
421+ api .router .Handle ("POST" , baseURL , func (w http.ResponseWriter , r * http.Request , params map [string ]string , context map [ string ] interface {} ) {
377422 info := requestInfo (r , api )
378423 c := api .contextPool .Get ().(APIContexter )
379424 c .Reset ()
425+
426+ for key , val := range context {
427+ c .Set (key , val )
428+ }
429+
380430 api .middlewareChain (c , w , r )
381431 err := res .handleCreate (c , w , r , info .prefix , * info )
382432 api .contextPool .Put (c )
@@ -387,9 +437,14 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source interfac
387437 }
388438
389439 if _ , ok := source .(ResourceDeleter ); ok {
390- api .router .Handle ("DELETE" , baseURL + "/:id" , func (w http.ResponseWriter , r * http.Request , params map [string ]string ) {
440+ api .router .Handle ("DELETE" , baseURL + "/:id" , func (w http.ResponseWriter , r * http.Request , params map [string ]string , context map [ string ] interface {} ) {
391441 c := api .contextPool .Get ().(APIContexter )
392442 c .Reset ()
443+
444+ for key , val := range context {
445+ c .Set (key , val )
446+ }
447+
393448 api .middlewareChain (c , w , r )
394449 err := res .handleDelete (c , w , r , params )
395450 api .contextPool .Put (c )
@@ -400,10 +455,15 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source interfac
400455 }
401456
402457 if _ , ok := source .(ResourceUpdater ); ok {
403- api .router .Handle ("PATCH" , baseURL + "/:id" , func (w http.ResponseWriter , r * http.Request , params map [string ]string ) {
458+ api .router .Handle ("PATCH" , baseURL + "/:id" , func (w http.ResponseWriter , r * http.Request , params map [string ]string , context map [ string ] interface {} ) {
404459 info := requestInfo (r , api )
405460 c := api .contextPool .Get ().(APIContexter )
406461 c .Reset ()
462+
463+ for key , val := range context {
464+ c .Set (key , val )
465+ }
466+
407467 api .middlewareChain (c , w , r )
408468 err := res .handleUpdate (c , w , r , params , * info )
409469 api .contextPool .Put (c )
0 commit comments