Skip to content

Commit

Permalink
Expose settings for the preview server
Browse files Browse the repository at this point in the history
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 <league@contrapunctus.net>
Co-authored-by: Brian McKenna <brian@brianmckenna.org>
  • Loading branch information
3 people committed Jan 16, 2023
1 parent 92e4399 commit a7e7e52
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/Hakyll/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/Hakyll/Core/Configuration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

--------------------------------------------------------------------------------
Expand All @@ -108,6 +112,7 @@ defaultConfiguration = Configuration
, inMemoryCache = True
, previewHost = "127.0.0.1"
, previewPort = 8000
, previewSettings = Static.defaultFileServerSettings
}
where
ignoreFile' path
Expand Down
7 changes: 3 additions & 4 deletions lib/Hakyll/Preview/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a7e7e52

Please sign in to comment.