summaryrefslogtreecommitdiff
path: root/src/Hakyll/Preview/Server.hs
blob: 5de3d0c875f077e457915d58948523a7c36730f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
--------------------------------------------------------------------------------
-- | Implements a basic static file server for previewing options
{-# LANGUAGE OverloadedStrings #-}
module Hakyll.Preview.Server
    ( staticServer
    ) where


--------------------------------------------------------------------------------
import           Control.Monad.Trans   (liftIO)
import qualified Data.ByteString.Char8 as B
import qualified Snap.Core             as Snap
import qualified Snap.Http.Server      as Snap
import qualified Snap.Util.FileServe   as Snap


--------------------------------------------------------------------------------
import           Hakyll.Core.Logger    (Logger)
import qualified Hakyll.Core.Logger    as Logger


--------------------------------------------------------------------------------
-- | Serve a given directory
static :: FilePath             -- ^ Directory to serve
       -> (FilePath -> IO ())  -- ^ Pre-serve hook
       -> Snap.Snap ()
static directory preServe =
    Snap.serveDirectoryWith directoryConfig directory
  where
    directoryConfig :: Snap.DirectoryConfig Snap.Snap
    directoryConfig = Snap.fancyDirectoryConfig
        { Snap.preServeHook = liftIO . preServe
        }


--------------------------------------------------------------------------------
-- | Main method, runs a static server in the given directory
staticServer :: Logger               -- ^ Logger
             -> FilePath             -- ^ Directory to serve
             -> (FilePath -> IO ())  -- ^ Pre-serve hook
             -> String               -- ^ Host to bind on
             -> Int                  -- ^ Port to listen on
             -> IO ()                -- ^ Blocks forever
staticServer logger directory preServe host port = do
    Logger.header logger $ "Listening on http://" ++ host ++ ":" ++ show port
    Snap.httpServe config $ static directory preServe
  where
    -- Snap server config
    config = Snap.setBind  (B.pack host)
           $ Snap.setPort      port
           $ Snap.setAccessLog Snap.ConfigNoLog
           $ Snap.setErrorLog  Snap.ConfigNoLog
           $ Snap.setVerbose   False
           $ Snap.emptyConfig