How to serve static content using Spock (Haskell)?
Asked Answered
F

1

8

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)

Fireplug answered 6/12, 2017 at 8:6 Comment(1)
It's been a while since I've used Spock, so this may be wrong: The path "/" in your RouteSpec doesn't have a wildcard, so I think that will only match exactly /. I suspect what you want is middleware $ staticPolicy $ addBase "static".Ephebe
F
2

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
Fireplug answered 24/2, 2018 at 20:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.