Fork me on GitHub
#reitit
<
2021-09-29
>
Charlie Briggs15:09:59

I feel this is a stupid question, but is there a straightforwards way of using a middleware (request logging in this case) only on a reitit subpath? Or disabling it on a specific subpath? We don't want to log healthcheck requests/responses but do want it on our API routes - but the logging middleware is in the middle of the ring middleware stack so I'm not sure of the best way of doing this while still using {:data {:middleware}} as a second arg to ring-router We're coming from compojure so the logging middleware is currently unaware of reitit

Ben Sless15:09:00

You can stick niddlewares on routes, too

Charlie Briggs15:09:39

I just got this to work by creating a function which returns middleware with options and called that on the two subpaths (`/internal` and /api) with logging: true/false respectively. That means I had to remove the default middleware on the router though, I'd prefer it if there was a way to mutate things but I guess this works - I was wondering on the most ideomatic way

Ben Sless15:09:28

There is, you you put the logging middleware on the api subpath

Charlie Briggs15:09:04

but it needs to be called in a specific order in the middleware vector, am I missing a simple solution to set that up? My assumption is that if I just add it there, it'll be last in the chain

Ben Sless15:09:00

Right, if the order matters then you have to do something else. In either case, middle wares are applied after matching, so you can indeed go with a function which will return the middleware chain at creation time

Ben Sless15:09:05

It's not a bad option

Ben Sless15:09:24

Then call that function on each subpath