on master help sign in
CPP for support for all 3.X versions of wai-app-static
c412678Julian K. Arni
servant-server/src/Servant/Utils/StaticFiles.hs+12−3
@@ -1,3 +1,4 @@
1+{-# LANGUAGE CPP #-}
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.
2--
3--
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,
7 ) where
8 ) where
8
9
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)
16+#endif
13
17
14-- | Serve anything under the specified directory as a 'Raw' endpoint.
18-- | Serve anything under the specified directory as a 'Raw' endpoint.
15--
19--
@@ -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
42+#else
36− staticApp (defaultFileServerSettings (decodeString (documentRoot ++ "/")))
43+ staticApp . defaultFileServerSettings . decodeString . mkSuff
44+#endif
45+ where mkSuff x = if "/" `isSuffixOf` x then x else x ++ "/"