yesodweb / wai

Haskell Web Application Interface
MIT License
834 stars 263 forks source link

Middleware support in wai-app-static? #715

Open sordina opened 6 years ago

sordina commented 6 years ago

Not sure if this should be an issue, just close if not appropriate!

I'd been wanting to use wai-app-static in a Scotty app (or any wai app really) but found it hard to compose with other applications since it is an Application in its own right. This can be remedied by wrapping it to turn it into a middleware like so:

https://github.com/sordina/wai-app-static-file-embed-middleware/blob/master/src/Main.hs#L20

I can't decide if such functionality should be in wai in general, or in libraries such as wai-app-static.

Thoughts?

snoyberg commented 6 years ago

I can't speak to Scotty, but in Yesod we have the ability to defer handling to a WAI application. That would seem to me a better approach than a middleware. That said, I'm not opposed to adding functionality here if you think it would be useful in general.

sordina commented 6 years ago

I found ifRequest seems to take us almost all the way there. All that's needed is a constMiddleware that turns an application into middleware which is almost trivial.

sordina commented 6 years ago

I'll have a go at implementing and see what you think!

Vlix commented 2 years ago

Routes in scotty are literally just Middleware, where the addroute function just adds the checking for the correct path and method and then runs the handler with the request. So I think it should be easy enough to add a raw application into a scotty app. (cf. addroute, addRoute, route)