Last active
February 10, 2016 08:03
-
-
Save sphvn/fbb41d66cbeb928dfcdd to your computer and use it in GitHub Desktop.
Example of using Haskell Snap for File / Directory Serving with Routes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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