git.iko.soy/typeable/servant
on master help sign in
add S.Utils.StaticFiles
commitd8b0e4c7e5e85ea3aba08378e23ed5cf073070b2 authorSönke Hahn <soenke.hahn@zalora.com> dateWed 12 Nov 2014, 13:21 (12 years ago) parente862fc7
4 files changed+71
servant.cabal+2 src/Servant.hs+7 src/Servant/Utils/StaticFiles.hs+11 test/Servant/Utils/StaticFilesSpec.hs+51
servant.cabal+2
@@ -46,6 +46,7 @@ library
4646 , http-types
4747 , network-uri >= 2.6
4848 , wai
49+ , wai-app-static
4950 , warp
5051 , safe
5152 , transformers
@@ -94,6 +95,7 @@ test-suite spec
9495 , QuickCheck
9596 , servant
9697 , string-conversions
98+ , temporary
9799 , text
98100 , transformers
99101 , wai
src/Servant.hs+7
@@ -12,6 +12,10 @@ module Servant (
1212 module Servant.Docs,
1313 -- | Helper module
1414 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,
1519 -- | Useful re-exports
1620 Proxy(..),
1721 ) where
@@ -23,3 +27,6 @@ import Servant.Common.BaseUrl
2327import Servant.Common.Text
2428import Servant.Docs
2529import 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"