From c41267811e464100123aba84b9dabc5f26a5bba2 Mon Sep 17 00:00:00 2001 From: "Julian K. Arni" Date: Fri, 15 May 2015 12:58:12 +0200 Subject: [PATCH 1/2] CPP for support for all 3.X versions of wai-app-static --- servant-server/src/Servant/Utils/StaticFiles.hs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/servant-server/src/Servant/Utils/StaticFiles.hs b/servant-server/src/Servant/Utils/StaticFiles.hs index 07c51173..0b26b3b2 100644 --- a/servant-server/src/Servant/Utils/StaticFiles.hs +++ b/servant-server/src/Servant/Utils/StaticFiles.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} -- | This module defines a sever-side handler that lets you serve static files. -- -- - 'serveDirectory' lets you serve anything that lives under a particular @@ -6,10 +7,13 @@ module Servant.Utils.StaticFiles ( serveDirectory, ) where -import Filesystem.Path.CurrentOS (decodeString) +import Data.List (isSuffixOf) import Network.Wai.Application.Static (staticApp, defaultFileServerSettings) import Servant.API.Raw (Raw) import Servant.Server (Server) +#if !MIN_VERSION_wai_app_static(3,1,0) +import Filesystem.Path.CurrentOS (decodeString) +#endif -- | Serve anything under the specified directory as a 'Raw' endpoint. -- @@ -32,5 +36,10 @@ import Servant.Server (Server) -- handler in the last position, because /servant/ will try to match the handlers -- in order. serveDirectory :: FilePath -> Server Raw -serveDirectory documentRoot = - staticApp (defaultFileServerSettings (decodeString (documentRoot ++ "/"))) +serveDirectory = +#if MIN_VERSION_wai_app_static(3,1,0) + staticApp . defaultFileServerSettings . mkSuff +#else + staticApp . defaultFileServerSettings . decodeString . mkSuff +#endif + where mkSuff x = if "/" `isSuffixOf` x then x else x ++ "/" From 7e14eeafe927c3f78b087c1fd4ad641f92efb1cd Mon Sep 17 00:00:00 2001 From: Ilya Smelkov Date: Sat, 16 May 2015 01:03:48 +0300 Subject: [PATCH 2/2] Use filepath to add trailing path separator --- servant-server/servant-server.cabal | 3 ++- servant-server/src/Servant/Utils/StaticFiles.hs | 7 +++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/servant-server/servant-server.cabal b/servant-server/servant-server.cabal index 6c325517..116c0a10 100644 --- a/servant-server/servant-server.cabal +++ b/servant-server/servant-server.cabal @@ -53,10 +53,11 @@ library , split >= 0.2 && < 0.3 , string-conversions >= 0.3 && < 0.4 , system-filepath >= 0.4 && < 0.5 + , filepath >= 1 , text >= 1.2 && < 1.3 , transformers >= 0.3 && < 0.5 , wai >= 3.0 && < 3.1 - , wai-app-static >= 3.0 && < 3.1 + , wai-app-static >= 3.0 && < 3.2 , warp >= 3.0 && < 3.1 hs-source-dirs: src default-language: Haskell2010 diff --git a/servant-server/src/Servant/Utils/StaticFiles.hs b/servant-server/src/Servant/Utils/StaticFiles.hs index 0b26b3b2..203c27f3 100644 --- a/servant-server/src/Servant/Utils/StaticFiles.hs +++ b/servant-server/src/Servant/Utils/StaticFiles.hs @@ -7,7 +7,7 @@ module Servant.Utils.StaticFiles ( serveDirectory, ) where -import Data.List (isSuffixOf) +import System.FilePath (addTrailingPathSeparator) import Network.Wai.Application.Static (staticApp, defaultFileServerSettings) import Servant.API.Raw (Raw) import Servant.Server (Server) @@ -38,8 +38,7 @@ import Filesystem.Path.CurrentOS (decodeString) serveDirectory :: FilePath -> Server Raw serveDirectory = #if MIN_VERSION_wai_app_static(3,1,0) - staticApp . defaultFileServerSettings . mkSuff + staticApp . defaultFileServerSettings . addTrailingPathSeparator #else - staticApp . defaultFileServerSettings . decodeString . mkSuff + staticApp . defaultFileServerSettings . decodeString . addTrailingPathSeparator #endif - where mkSuff x = if "/" `isSuffixOf` x then x else x ++ "/"