Fork me on GitHub
#duct
<
2020-03-05
>
keoko06:03:47

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.

kwrooijen07:03:39

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]}}}
          ...

keoko08:03:02

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

keoko08:03:00

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]}}}

keoko08:03:34

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

keoko08:03:31

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

kwrooijen08:03:29

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

weavejester16:03:28

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

keoko07:03:51

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

keoko18:03:08

@UG9U7TPDZ thanks, it worked as you mention: they are sorted by key.

keoko18:03:23

@U0BKWMG5B 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.