@@ -1,3 +1,4 @@
1-- | This module defines a sever-side handler that lets you serve static files.
2-- | This module defines a sever-side handler that lets you serve static files.
3-- - 'serveDirectory' lets you serve anything that lives under a particular
4-- - 'serveDirectory' lets you serve anything that lives under a particular
@@ -6,10 +7,13 @@ module Servant.Utils.StaticFiles (
6 serveDirectory,
7 serveDirectory,
9−import Filesystem.Path.CurrentOS (decodeString)
10+import Data.List (isSuffixOf)
10import Network.Wai.Application.Static (staticApp, defaultFileServerSettings)
11import Network.Wai.Application.Static (staticApp, defaultFileServerSettings)
11import Servant.API.Raw (Raw)
12import Servant.API.Raw (Raw)
12import Servant.Server (Server)
13import Servant.Server (Server)
14+#if !MIN_VERSION_wai_app_static(3,1,0)
15+import Filesystem.Path.CurrentOS (decodeString)
14-- | Serve anything under the specified directory as a 'Raw' endpoint.
18-- | Serve anything under the specified directory as a 'Raw' endpoint.
@@ -32,5 +36,10 @@ import Servant.Server (Server)
32-- handler in the last position, because /servant/ will try to match the handlers
36-- handler in the last position, because /servant/ will try to match the handlers
33-- in order.
37-- in order.
34serveDirectory :: FilePath -> Server Raw
38serveDirectory :: FilePath -> Server Raw
35−serveDirectory documentRoot =
39+serveDirectory =
40+#if MIN_VERSION_wai_app_static(3,1,0)
41+ staticApp . defaultFileServerSettings . mkSuff
36− staticApp (defaultFileServerSettings (decodeString (documentRoot ++ "/")))
43+ staticApp . defaultFileServerSettings . decodeString . mkSuff
45+ where mkSuff x = if "/" `isSuffixOf` x then x else x ++ "/"