From a7e7e52302fd38130ac5ceb677d81bff82af45d6 Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Mon, 16 Jan 2023 19:55:31 +0100 Subject: [PATCH] Expose settings for the preview server This enables customization of the settings used by the preview server, for example to implement URL rewriting. My particular use case is to mimic the `tryFiles` setting in nginx to add an extension when looking for a matching file. Then the URL can be `/projects/web` and it will match a file called `projects/web.html` -- a nice clean URL that doesn't require lots of subdirectories and every file being called `index.html`. (This change doesn't implement that feature directly as part of Hakyll, it just exposes the settings to enable building features like it in your `site.hs`.) Co-authored-by: Christopher League Co-authored-by: Brian McKenna --- lib/Hakyll/Commands.hs | 4 ++-- lib/Hakyll/Core/Configuration.hs | 5 +++++ lib/Hakyll/Preview/Server.hs | 7 +++---- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/Hakyll/Commands.hs b/lib/Hakyll/Commands.hs index 0da749a7d..4fe879ef8 100644 --- a/lib/Hakyll/Commands.hs +++ b/lib/Hakyll/Commands.hs @@ -124,8 +124,8 @@ rebuild conf logger rules = server :: Configuration -> Logger -> String -> Int -> IO () #ifdef PREVIEW_SERVER server conf logger host port = do - let destination = destinationDirectory conf - staticServer logger destination host port + let settings = previewSettings conf $ destinationDirectory conf + staticServer logger settings host port #else server _ _ _ _ = previewServerDisabled #endif diff --git a/lib/Hakyll/Core/Configuration.hs b/lib/Hakyll/Core/Configuration.hs index 45b626e97..9d7112329 100644 --- a/lib/Hakyll/Core/Configuration.hs +++ b/lib/Hakyll/Core/Configuration.hs @@ -11,6 +11,7 @@ module Hakyll.Core.Configuration -------------------------------------------------------------------------------- import Data.Default (Default (..)) import Data.List (isPrefixOf, isSuffixOf) +import qualified Network.Wai.Application.Static as Static import System.Directory (canonicalizePath) import System.Exit (ExitCode) import System.FilePath (isAbsolute, normalise, takeFileName, makeRelative) @@ -87,6 +88,9 @@ data Configuration = Configuration -- One can also override the port as a command line argument: -- ./site preview -p 1234 previewPort :: Int + -- | Override other settings used by the preview server. Default is + -- 'Static.defaultFileServerSettings'. + , previewSettings :: FilePath -> Static.StaticSettings } -------------------------------------------------------------------------------- @@ -108,6 +112,7 @@ defaultConfiguration = Configuration , inMemoryCache = True , previewHost = "127.0.0.1" , previewPort = 8000 + , previewSettings = Static.defaultFileServerSettings } where ignoreFile' path diff --git a/lib/Hakyll/Preview/Server.hs b/lib/Hakyll/Preview/Server.hs index 828a1d25a..dcd6f594d 100644 --- a/lib/Hakyll/Preview/Server.hs +++ b/lib/Hakyll/Preview/Server.hs @@ -18,15 +18,14 @@ import Hakyll.Core.Logger (Logger) import qualified Hakyll.Core.Logger as Logger staticServer :: Logger -- ^ Logger - -> FilePath -- ^ Directory to serve + -> Static.StaticSettings -- ^ Static file server settings -> String -- ^ Host to bind on -> Int -- ^ Port to listen on -> IO () -- ^ Blocks forever -staticServer logger directory host port = do +staticServer logger settings host port = do Logger.header logger $ "Listening on http://" ++ host ++ ":" ++ show port Logger.flush logger -- ensure this line is logged before Warp errors - Warp.runSettings warpSettings $ - Static.staticApp (Static.defaultFileServerSettings directory) + Warp.runSettings warpSettings $ Static.staticApp settings where warpSettings = Warp.setLogger noLog $ Warp.setHost (fromString host)