1
1
--------------------------------------------------------------------------------
2
- -- | Exports a datastructure for the top-level hakyll configuration
2
+ -- | Exports a data structure for the top-level Hakyll configuration.
3
+ {-# LANGUAGE OverloadedStrings #-}
3
4
module Hakyll.Core.Configuration
4
- ( Configuration (.. )
5
+ ( -- * Configuration
6
+ defaultConfiguration
7
+ , Configuration (.. )
8
+ -- * Utilities
5
9
, shouldIgnoreFile
6
10
, shouldWatchIgnore
7
- , defaultConfiguration
11
+ -- * Middleware for the preview server
12
+ , middlewareRefresh
8
13
) where
9
14
10
15
11
16
--------------------------------------------------------------------------------
12
17
import Data.Default (Default (.. ))
13
18
import Data.List (isPrefixOf , isSuffixOf )
19
+ import Data.String (fromString )
20
+ import Network.Wai (Middleware , mapResponseHeaders )
14
21
import qualified Network.Wai.Application.Static as Static
15
22
import System.Directory (canonicalizePath )
16
23
import System.Exit (ExitCode )
@@ -21,6 +28,14 @@ import System.Process (system)
21
28
22
29
23
30
--------------------------------------------------------------------------------
31
+ -- | Specifies the configuration for a Hakyll application.
32
+ --
33
+ -- Prefer to update record fields from 'defaultConfiguration'
34
+ -- instead of constructing a 'Configuration' value directly.
35
+ -- For example,
36
+ --
37
+ -- >>> let config = defaultConfiguration { destinationDirectory = "..." }
38
+ --
24
39
data Configuration = Configuration
25
40
{ -- | Directory in which the output written
26
41
destinationDirectory :: FilePath
@@ -96,14 +111,17 @@ data Configuration = Configuration
96
111
, -- | Override other settings used by the preview server. Default is
97
112
-- 'Static.defaultFileServerSettings'.
98
113
previewSettings :: FilePath -> Static. StaticSettings
114
+ , -- | WAI middleware which can sit between the preview server
115
+ -- and the file serving. Default is to do nothing.
116
+ previewMiddleware :: Middleware
99
117
}
100
118
101
119
--------------------------------------------------------------------------------
102
120
instance Default Configuration where
103
121
def = defaultConfiguration
104
122
105
123
--------------------------------------------------------------------------------
106
- -- | Default configuration for a hakyll application
124
+ -- | Default configuration for a Hakyll application.
107
125
defaultConfiguration :: Configuration
108
126
defaultConfiguration = Configuration
109
127
{ destinationDirectory = " _site"
@@ -119,6 +137,7 @@ defaultConfiguration = Configuration
119
137
, previewHost = " 127.0.0.1"
120
138
, previewPort = 8000
121
139
, previewSettings = Static. defaultFileServerSettings
140
+ , previewMiddleware = id
122
141
}
123
142
where
124
143
ignoreFile' path
@@ -161,3 +180,23 @@ shouldWatchIgnore conf = do
161
180
return (\ path ->
162
181
let path' = makeRelative fullProviderDir path
163
182
in (|| watchIgnore conf path') <$> shouldIgnoreFile conf path)
183
+
184
+
185
+ --------------------------------------------------------------------------------
186
+ -- | WAI middleware which tells clients that they should refresh loaded content
187
+ -- periodically. Can be used to avoid having to manually reload content.
188
+ --
189
+ -- For example, the following can be used to have content reloaded
190
+ -- every 10 seconds during preview:
191
+ --
192
+ -- >>> let config = defaultConfiguration { previewMiddleware = middlewareRefresh 10 }
193
+ middlewareRefresh
194
+ -- | Seconds between refreshes.
195
+ :: Int
196
+ -- | Middleware which adds the @Refresh@ header to HTTP responses.
197
+ -> Middleware
198
+ middlewareRefresh seconds app req respond = app req respond'
199
+ where
200
+ respond' = respond . autoRefresh
201
+ autoRefresh = mapResponseHeaders addRefresh
202
+ addRefresh rs = (" Refresh" , fromString $ show seconds) : rs
0 commit comments