@@ -37,12 +37,12 @@ func (r response) StatusCode() int {
3737}
3838
3939type information struct {
40- prefix string
41- baseURL string
40+ prefix string
41+ resolver URLResolver
4242}
4343
4444func (i information ) GetBaseURL () string {
45- return i .baseURL
45+ return i .resolver . GetBaseURL ()
4646}
4747
4848func (i information ) GetPrefix () string {
@@ -235,6 +235,18 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
235235 marshalers : marshalers ,
236236 }
237237
238+ requestInfo := func (r * http.Request ) * information {
239+ var info * information
240+ if resolver , ok := api .info .resolver .(RequestAwareURLResolver ); ok {
241+ resolver .SetRequest (* r )
242+ info = & information {prefix : api .prefix , resolver : resolver }
243+ } else {
244+ info = & api .info
245+ }
246+
247+ return info
248+ }
249+
238250 api .router .Handle ("OPTIONS" , api .prefix + name , func (w http.ResponseWriter , r * http.Request , ps httprouter.Params ) {
239251 c := api .contextPool .Get ().(APIContexter )
240252 c .Reset ()
@@ -254,21 +266,24 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
254266 })
255267
256268 api .router .GET (api .prefix + name , func (w http.ResponseWriter , r * http.Request , _ httprouter.Params ) {
269+ info := requestInfo (r )
257270 c := api .contextPool .Get ().(APIContexter )
258271 c .Reset ()
259272 api .middlewareChain (c , w , r )
260- err := res .handleIndex (c , w , r , api .info )
273+
274+ err := res .handleIndex (c , w , r , * info )
261275 api .contextPool .Put (c )
262276 if err != nil {
263277 handleError (err , w , r , marshalers )
264278 }
265279 })
266280
267281 api .router .GET (api .prefix + name + "/:id" , func (w http.ResponseWriter , r * http.Request , ps httprouter.Params ) {
282+ info := requestInfo (r )
268283 c := api .contextPool .Get ().(APIContexter )
269284 c .Reset ()
270285 api .middlewareChain (c , w , r )
271- err := res .handleRead (c , w , r , ps , api . info )
286+ err := res .handleRead (c , w , r , ps , * info )
272287 api .contextPool .Put (c )
273288 if err != nil {
274289 handleError (err , w , r , marshalers )
@@ -282,10 +297,11 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
282297 for _ , relation := range relations {
283298 api .router .GET (api .prefix + name + "/:id/relationships/" + relation .Name , func (relation jsonapi.Reference ) httprouter.Handle {
284299 return func (w http.ResponseWriter , r * http.Request , ps httprouter.Params ) {
300+ info := requestInfo (r )
285301 c := api .contextPool .Get ().(APIContexter )
286302 c .Reset ()
287303 api .middlewareChain (c , w , r )
288- err := res .handleReadRelation (c , w , r , ps , api . info , relation )
304+ err := res .handleReadRelation (c , w , r , ps , * info , relation )
289305 api .contextPool .Put (c )
290306 if err != nil {
291307 handleError (err , w , r , marshalers )
@@ -295,10 +311,11 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
295311
296312 api .router .GET (api .prefix + name + "/:id/" + relation .Name , func (relation jsonapi.Reference ) httprouter.Handle {
297313 return func (w http.ResponseWriter , r * http.Request , ps httprouter.Params ) {
314+ info := requestInfo (r )
298315 c := api .contextPool .Get ().(APIContexter )
299316 c .Reset ()
300317 api .middlewareChain (c , w , r )
301- err := res .handleLinked (c , api , w , r , ps , relation , api . info )
318+ err := res .handleLinked (c , api , w , r , ps , relation , * info )
302319 api .contextPool .Put (c )
303320 if err != nil {
304321 handleError (err , w , r , marshalers )
@@ -351,10 +368,11 @@ func (api *API) addResource(prototype jsonapi.MarshalIdentifier, source CRUD, ma
351368 }
352369
353370 api .router .POST (api .prefix + name , func (w http.ResponseWriter , r * http.Request , ps httprouter.Params ) {
371+ info := requestInfo (r )
354372 c := api .contextPool .Get ().(APIContexter )
355373 c .Reset ()
356374 api .middlewareChain (c , w , r )
357- err := res .handleCreate (c , w , r , api .prefix , api . info )
375+ err := res .handleCreate (c , w , r , api .prefix , * info )
358376 api .contextPool .Put (c )
359377 if err != nil {
360378 handleError (err , w , r , marshalers )
0 commit comments