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