packages feed

hakyll-3.1.2.4: src/Hakyll/Web/Preview/Server.hs

-- | Implements a basic static file server for previewing options
--
{-# LANGUAGE OverloadedStrings #-}
module Hakyll.Web.Preview.Server
    ( staticServer
    ) where

import Control.Monad.Trans (liftIO)
import Control.Applicative ((<$>))
import Codec.Binary.UTF8.String
import Network.HTTP.Base (urlDecode)
import System.FilePath ((</>))
import System.Directory (doesFileExist)

import qualified Data.ByteString as SB
import Snap.Util.FileServe
import Snap.Types (Snap, rqURI, getRequest, writeBS)
import Snap.Http.Server ( httpServe, setAccessLog, setErrorLog, addListen
                        , ConfigListen (..), emptyConfig
                        )

import Hakyll.Core.Util.String (replaceAll)

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

-- | Main method, runs a static server in the given directory
--
staticServer :: FilePath             -- ^ Directory to serve
             -> (FilePath -> IO ())  -- ^ Pre-serve hook
             -> Int                  -- ^ Port to listen on
             -> IO ()                -- ^ Blocks forever
staticServer directory preServe port =
    httpServe config $ static directory preServe
  where
    -- Snap server config
    config = addListen (ListenHttp "0.0.0.0" port)
           $ setAccessLog Nothing
           $ setErrorLog Nothing
           $ emptyConfig