danielsz/system

Impossible to attach middleware to endpoints

woodlamp-tech-admin opened this issue · 5 comments

I must be insane, because I see example after example online of this being done, yet it doesn't work on my system.

I have this:

(component/system-map
  :site-middleware (new-middleware {:middleware site-middleware-config})
  :site-endpoint   (-> (new-endpoint site-routes)
                       (component/using [:site-middleware]))
  :handler         (-> (new-handler :router :bidi)
                       (component/using [:site-endpoint]))
  :http            (-> (new-web-server (:http-port config))
                       (component/using [:handler])))

The middleware isn't mounting. I can't figure out from the code how Handler does its magic, but example after example shows that this is how to do it.

What am I doing wrong?

Gah identity management on GitHub sucks. That's me above.

Hi @alyssackwan
I'm noticing that the enclosing vector in the middleware map is missing. Try this:

:site-middleware (new-middleware {:middleware [site-middleware-config}})

I hope this helps.

The site-middleware-config var is already a vector of wrap- functions.

I decided to revert from bidi to Compojure in my usage of Chestnut and that helped. I didn't test, but it doesn't seem like the wrap-mw calls to the outputs of bidi's make-handler can actually compose the same way that Compojure routes and middleware compose. I'm probably wrong, but I'll point to this thread including a philosophical statement by the maintainer of bidi (juxt/bidi#160).

Ah, I see. So it works with Compojure, but not with Bidi?

Yes. Again, I haven't tested, but I think bidi.ring/make-handler takes a bidi struct and returns a Ring handler function, and system's handler component does a double make-handler call - once for endpoint components with :middleware directly attached to them, and then again to compose all the separate endpoints into the final handler. I conjecture that the outer make-handler can't be passed a Ring handler function (from the inner make-handler calls), only a bidi struct directly, which it gets with endpoint components without :middleware. Not too hard to test, but I haven't had time.