on master help sign in
add S.Utils.StaticFiles
d8b0e4cSönke Hahn
servant.cabal+2
@@ -46,6 +46,7 @@ library
46 , http-types
46 , http-types
47 , network-uri >= 2.6
47 , network-uri >= 2.6
48 , wai
48 , wai
49+ , wai-app-static
49 , warp
50 , warp
50 , safe
51 , safe
51 , transformers
52 , transformers
@@ -94,6 +95,7 @@ test-suite spec
94 , QuickCheck
95 , QuickCheck
95 , servant
96 , servant
96 , string-conversions
97 , string-conversions
98+ , temporary
97 , text
99 , text
98 , transformers
100 , transformers
99 , wai
101 , wai
src/Servant.hs+7
@@ -12,6 +12,10 @@ module Servant (
12 module Servant.Docs,
12 module Servant.Docs,
13 -- | Helper module
13 -- | Helper module
14 module Servant.Common.Text,
14 module Servant.Common.Text,
15+ -- | Utilities on top of the servant core
16+ module Servant.Utils.ApiQuasiQuoting,
17+ module Servant.Utils.Links,
18+ module Servant.Utils.StaticFiles,
15 -- | Useful re-exports
19 -- | Useful re-exports
16 Proxy(..),
20 Proxy(..),
17 ) where
21 ) where
@@ -23,3 +27,6 @@ import Servant.Common.BaseUrl
23import Servant.Common.Text
27import Servant.Common.Text
24import Servant.Docs
28import Servant.Docs
25import Servant.Server
29import Servant.Server
30+import Servant.Utils.ApiQuasiQuoting
31+import Servant.Utils.Links
32+import Servant.Utils.StaticFiles
src/Servant/Utils/StaticFiles.hsnew file+11
@@ -0,0 +1,11 @@
1+module Servant.Utils.StaticFiles where
2+
3+import Filesystem.Path.CurrentOS (decodeString)
4+import Network.Wai.Application.Static
5+
6+import Servant.API
7+import Servant.Server
8+
9+serveDirectory :: FilePath -> Server Raw
10+serveDirectory documentRoot =
11+ staticApp (defaultFileServerSettings (decodeString (documentRoot ++ "/")))
test/Servant/Utils/StaticFilesSpec.hsnew file+51
@@ -0,0 +1,51 @@
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE OverloadedStrings #-}
3+{-# LANGUAGE TypeOperators #-}
4+module Servant.Utils.StaticFilesSpec where
5+
6+import Test.Hspec hiding (pending)
7+import System.IO.Temp
8+import Test.Hspec.Wai
9+import Network.Wai
10+import Data.Proxy
11+import System.Directory
12+import Control.Exception
13+
14+import Servant.Utils.StaticFiles
15+import Servant.API.Sub
16+import Servant.API.Raw
17+import Servant.Server
18+
19+type Api =
20+ "static" :> Raw
21+
22+app :: Application
23+app = serve (Proxy :: Proxy Api) server
24+
25+server :: Server Api
26+server = serveDirectory "static"
27+
28+withStaticFiles :: IO () -> IO ()
29+withStaticFiles action = withSystemTempDirectory "servant-test" $ \ tmpDir ->
30+ bracket (setup tmpDir) teardown (const action)
31+ where
32+ setup tmpDir = do
33+ outer <- getCurrentDirectory
34+ setCurrentDirectory tmpDir
35+ createDirectory "static"
36+ writeFile "static/foo.txt" "bar"
37+ writeFile "static/index.html" "index"
38+ return outer
39+
40+ teardown outer = do
41+ setCurrentDirectory outer
42+
43+spec :: Spec
44+spec = do
45+ around_ withStaticFiles $ with (return app) $
46+ describe "serveDirectory" $ do
47+ it "successfully serves files" $ do
48+ get "/static/foo.txt" `shouldRespondWith` "bar"
49+
50+ it "serves the contents of index.html when requesting the root of a directory" $ do
51+ get "/static" `shouldRespondWith` "index"