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


--------------------------------------------------------------------------------
import           Data.String
import qualified Network.Wai.Handler.Warp       as Warp
import qualified Network.Wai.Application.Static as Static
import qualified Network.Wai                    as Wai
import           Network.HTTP.Types.Status      (Status)

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

staticServer :: Logger               -- ^ Logger
             -> Static.StaticSettings -- ^ Static file server settings
             -> String               -- ^ Host to bind on
             -> Int                  -- ^ Port to listen on
             -> IO ()                -- ^ Blocks forever
staticServer :: Logger -> StaticSettings -> [Char] -> Int -> IO ()
staticServer Logger
logger StaticSettings
settings [Char]
host Int
port = do
    forall (m :: * -> *). MonadIO m => Logger -> [Char] -> m ()
Logger.header Logger
logger forall a b. (a -> b) -> a -> b
$ [Char]
"Listening on http://" forall a. [a] -> [a] -> [a]
++ [Char]
host forall a. [a] -> [a] -> [a]
++ [Char]
":" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> [Char]
show Int
port
    Logger -> IO ()
Logger.flush Logger
logger -- ensure this line is logged before Warp errors
    Settings -> Application -> IO ()
Warp.runSettings Settings
warpSettings forall a b. (a -> b) -> a -> b
$ StaticSettings -> Application
Static.staticApp StaticSettings
settings
  where
    warpSettings :: Settings
warpSettings = (Request -> Status -> Maybe Integer -> IO ())
-> Settings -> Settings
Warp.setLogger Request -> Status -> Maybe Integer -> IO ()
noLog
        forall a b. (a -> b) -> a -> b
$ HostPreference -> Settings -> Settings
Warp.setHost (forall a. IsString a => [Char] -> a
fromString [Char]
host)
        forall a b. (a -> b) -> a -> b
$ Int -> Settings -> Settings
Warp.setPort Int
port Settings
Warp.defaultSettings

noLog :: Wai.Request -> Status -> Maybe Integer -> IO ()
noLog :: Request -> Status -> Maybe Integer -> IO ()
noLog Request
_ Status
_ Maybe Integer
_ = forall (m :: * -> *) a. Monad m => a -> m a
return ()