muhuk
muhuk

Reputation: 16085

How to ensure a middleware is not wrapped twice?

How do you ensure a middleware is not applied twice?

My application is using bidi and I have my routes and handlers separate. So my final ring handler has some middlewares (such as wrap-params and wrap-session) and some of my handlers have their own custom middlewares. I can manually watch over my main handler's constructor and my handlers file, but I would rather not rely on manual vigilance.

Is there a design that helps manage middlewares better? Or some library?

My structure looks like this:

1. routes                                 2. handlers
      \                                      /
       \                                    /
    3. (bidi.ring/make-handler routes handlers)

It's possible to wrap something in 2 and then again in 3.

3 is actually a component and make-handler is called in its start. handler & routes come from 3's component's dependencies. I have considered adding another protocol to 1 & 2 with a wrap-middlewares method, but the order of middlewares is significant. For example (buddy-auth's) wrap-authentication depends on wrap-session & wrap-params.

Upvotes: 2

Views: 233

Answers (1)

Arthur Ulfeldt
Arthur Ulfeldt

Reputation: 91554

for middleware you control it's fairly straightforward, you can just have each middleware add a key with it's name to the request and bail (or noop if you're feeling lenient) if that key is in any request it recieves. for middleware you don't control i suppose were stuck with staring at the code and thinking really hard, or as you describe it "manual vigilance".

Upvotes: 1

Related Questions