2
votes

So, I want to add two wai middlewares to my servant server. One middleware for serving static files, and one for logging.

I have my api defined (router) and this is how I start my server :

webServer :: IO ()
webServer = run 80 (middleware $ router)

And here is how I defined my middleware :

middleware :: Application -> Application
middleware = do
  logStdoutDev
  staticPolicy $ addBase "static"

Now, if I put logStdoutDev first than I can't serve static files, but if I put staticPolicy first, than I can serve static files but I loose ability to log events (basically they get ignored).

Question is really, how to properly combine wai middleware in servant.

1

1 Answers

9
votes

If I understand correctly, you want to compose two middleware's: logStdoutDev and staticPolicy $ addBase "static". But what did you do?

middleware = do
    logStdoutDev
    staticPolicy $ addBase "static"

It's equivalent with:

middleware = logStdoutDev >> (staticPolicy $ addBase "static")

So, What monad has been used? The middleware has type Application -> Application. So it's monad reader (->) Application.

The expression logStdoutDev >> (staticPolicy $ addBase "static") equivalent with \r -> (\_ -> (staticPolicy $ addBase "static") r) (logStdoutDev r). Or if simplify, we'll get: \r -> (staticPolicy $ addBase "static") r.

You should to do like this:

middleware = logStdoutDev . (staticPolicy $ addBase "static")