on b91c8be help sign in
43 lines 1.7 kB history raw
1--------------------------------------------------------------------------------
2-- | Implements a basic static file server for previewing options
3{-# LANGUAGE OverloadedStrings #-}
4module Hakyll.Preview.Server
5 ( staticServer
6 ) where
9--------------------------------------------------------------------------------
10import Control.Monad.Trans (liftIO)
11import qualified Snap.Core as Snap
12import qualified Snap.Http.Server as Snap
13import qualified Snap.Util.FileServe as Snap
16--------------------------------------------------------------------------------
17-- | Serve a given directory
18static :: FilePath -- ^ Directory to serve
19 -> (FilePath -> IO ()) -- ^ Pre-serve hook
20 -> Snap.Snap ()
21static directory preServe =
22 Snap.serveDirectoryWith directoryConfig directory
23 where
24 directoryConfig :: Snap.DirectoryConfig Snap.Snap
25 directoryConfig = Snap.fancyDirectoryConfig
26 { Snap.preServeHook = liftIO . preServe
27 }
30--------------------------------------------------------------------------------
31-- | Main method, runs a static server in the given directory
32staticServer :: FilePath -- ^ Directory to serve
33 -> (FilePath -> IO ()) -- ^ Pre-serve hook
34 -> Int -- ^ Port to listen on
35 -> IO () -- ^ Blocks forever
36staticServer directory preServe port =
37 Snap.httpServe config $ static directory preServe
38 where
39 -- Snap server config
40 config = Snap.setPort port
41 $ Snap.setAccessLog Snap.ConfigNoLog
42 $ Snap.setErrorLog Snap.ConfigNoLog
43 $ Snap.emptyConfig