Skip to content

Instantly share code, notes, and snippets.

@sphvn sphvn/Main.hs
Last active Feb 10, 2016

Embed
What would you like to do?
Example of using Haskell Snap for File / Directory Serving with Routes.
{-# LANGUAGE OverloadedStrings #-}
module Main where
import qualified Data.HashMap.Strict as H
import qualified Data.ByteString.Char8 as C
import Control.Applicative ((<|>))
import Snap.Core
import Snap.Util.FileServe
import Snap.Http.Server (quickHttpServe)
import Snap.Util.GZip (withCompression)
main :: IO ()
main = quickHttpServe $ withCompression site
site :: Snap ()
site = routes <|> handles <|> staticContent <|> notFound
where
routes = page "blog" <|> page "page2" <|> page "page3"
page x = route [(C.pack x, serveFile $ "static/" ++ x ++ ".html")]
handles = route[("hellow", writeBS "hello :)")]
staticContent = serveDirectory' "static"
notFound = serveFile "static/404.html"
serveDirectory' :: MonadSnap m => FilePath -> m ()
serveDirectory' = serveDirectoryWith config
where
config = fancyDirectoryConfig {
indexFiles = ["index.html"]
, mimeTypes = newMimeTypes `H.union` defaultMimeTypes
}
newMimeTypes = H.fromList [
(".appcache", "text/cache-manifest")
, (".ttf", "font/truetype")
, (".otf", "font/opentype")
, (".eot", "application/vnd.ms-fontobject")
, (".woff", "application/font-woff")
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.