Skip to content

Instantly share code, notes, and snippets.

@sphvn
Last active February 10, 2016 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sphvn/fbb41d66cbeb928dfcdd to your computer and use it in GitHub Desktop.
Save sphvn/fbb41d66cbeb928dfcdd to your computer and use it in GitHub Desktop.
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