danielsz / system

Reloaded components à la carte
607 stars 77 forks source link

Impossible to attach middleware to endpoints #130

Open woodlamp-tech-admin opened 5 years ago

woodlamp-tech-admin commented 5 years ago

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?

jennykwan commented 5 years ago

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

danielsz commented 5 years ago

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.

jennykwan commented 5 years ago

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 (https://github.com/juxt/bidi/issues/160).

danielsz commented 5 years ago

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

jennykwan commented 5 years ago

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.