Search code examples
haskellserverhaskell-stackstatic-contenthaskell-spock

How to serve static content using Spock (Haskell)?


I am running a Spock server and wanting to serve some static content. I am using respondMiddleware function from the "Web.Spock.Action" package.

The signature is:

    respondMiddleware :: Monad m => Middleware -> ActionCtxT ctx m a

I am getting this "Middleware" using the staticPolicy function from "Network.Wai.Middleware.Static" package

    staticPolicy :: Policy -> Middleware

And the "Policy" using addBase from "Network.Wai.Middleware.Static" package

    addBase :: String -> Policy

Here is my codeblock:

    get ("/") $
      respondMiddleware $ staticPolicy $ addBase ("static")

The static has an index.html and the related JS and CSS files and it is placed in the root directory of the stack project. The error I get is Failed to load resource: the server responded with a status of 400 (Bad Request)


Solution

  • The right way to do this is using the html function from the Web.Spock.Action package.

    I used a jinja templating using ginger to parameterize my html file and then served it using the html function.

    import qualified Web.Spock.Action as SA
    
    serveHtml :: JinjaTemplate -> SA.ActionT (LoggingT IO) ()
    serveHtml tmplt = SA.html tmplt