duct

2020-03-05T06:31:47.016900Z

is it possible to have a web app with some routes with “:duct.module.web/site” middleware and some other routes with “:duct.module.web/api” middleware? I’ve tried to set an app with +site +api but the documentation of module.web says you should use one or the other. I’ve also tried to set a middleware through a ataraxy metadata in a route (see https://github.com/duct-framework/duct/issues/91), but I am not sure how to pass a list of middlewares to an specific route.

2020-03-05T08:26:02.017700Z

Yes, I just wanted to copy the list of middlewares set in module duct.module.web/api in one of my routes.

2020-03-05T08:36:00.017900Z

I was trying to do something like this

:duct.router/ataraxy 
{:middleware {:custom-api [(ig/ref :duct.middleware.web/not-found)
                            (ig/ref :duct.middleware.web/format)
                            (ig/ref :duct.middleware.web/defaults)]}
 :routes {"/v1" ^{:custom-api true}
          {[:get "/users/" user-id]}}}

2020-03-05T08:37:34.018100Z

My doubt with the map

^{:cors true
 :auth true}
is that if it respects the order of the keys as it is important when defining the list of middlewares

2020-03-05T08:41:31.018300Z

thanks a lot @kevin.van.rooijen I’ll follow your suggestion and check if the order of the middlewares is respected.

kwrooijen 2020-03-05T08:55:29.018500Z

I think it is sorted by key name, but I'm not sure

2020-03-05T16:49:28.018700Z

+site +api should work now, as that was fixed in the template a while ago. The module.web documentation may be out of date.

2020-03-06T07:53:51.018900Z

@weavejester how do I tell to a route use “site” middlewares and another use “api” middlewares?

2020-03-14T18:16:08.025900Z

@kevin.van.rooijen thanks, it worked as you mention: they are sorted by key.

2020-03-14T18:20:23.026100Z

@weavejester When I use +site +api , the api requests fail with an Invalid anti-forgery token . I’ve just disabled anti-forgery and it works. But still I am not sure what’s the best way to disable it only for api routes.

kwrooijen 2020-03-05T07:46:39.017100Z

What do you mean by a list of middleware? Just more than one? If you want more than one, here's an example:

:duct.router/ataraxy 
{:middleware {:cors #ig/ref :app.middleware/cors
              :auth #ig/ref :app.middleware/auth}
 :routes {"/v1" ^{:cors true
                  :auth true}
          {[:get "/users/" user-id]}}}
          ...