user7905648
user7905648

Reputation: 117

How to server a folder of images/css/js in Wai/Warp?

I have this simple Wai/Warp application

app :: Wai.Application
app req respond = respond $ case Wai.rawPathInfo req of
    "/" -> Wai.responseFile status200 [("Content-Type", "text/html")] "views/index.html" Nothing
    _ -> notFound


notFound :: Wai.Response
notFound = Wai.responseLBS status404 [("Content-Type", "text/plain")] "404 - Not Found"

main :: IO ()
main = do
    port <- getEnv "PORT"
    let setting = Warp.setPort (read port :: Int) Warp.defaultSettings
    putStrLn $ "start server on the port " ++ port
    Warp.runSettings setting app

How can I add a folder "images" to the route so that any image I'll be able to refer to any image from the index.html as "images/something.jpg"? I know how to add an exact route, but here I need to add a whole folder.

Upvotes: 1

Views: 363

Answers (1)

liminalisht
liminalisht

Reputation: 1253

Network.Wai.Middleware.Static has a staticPolicy fn that takes a Policy and returns Middleware you can add to your Wai app. A Policy will define how the URI supplied by the user will be mapped to a filepath. Check out addBase for constructing a Policy; this may suit your needs just fine.

Upvotes: 1

Related Questions