on master help sign in
Deprecate S.Utils.StaticFiles in favor of S.Server.StaticFiles
374a7b8Oleg Grenrus
servant-server/servant-server.cabal+5−1
@@ -54,6 +54,10 @@ library
54 Servant.Server.Internal.Router
54 Servant.Server.Internal.Router
55 Servant.Server.Internal.RoutingApplication
55 Servant.Server.Internal.RoutingApplication
56 Servant.Server.Internal.ServantErr
56 Servant.Server.Internal.ServantErr
57+ Servant.Server.StaticFiles
58+
59+ -- deprecated
60+ exposed-modules:
57 Servant.Utils.StaticFiles
61 Servant.Utils.StaticFiles
58
62
59 -- Bundled with GHC: Lower bound to not force re-installs
63 -- Bundled with GHC: Lower bound to not force re-installs
@@ -133,12 +137,12 @@ test-suite spec
133 Servant.Server.Internal.ContextSpec
137 Servant.Server.Internal.ContextSpec
134 Servant.Server.Internal.RoutingApplicationSpec
138 Servant.Server.Internal.RoutingApplicationSpec
135 Servant.Server.RouterSpec
139 Servant.Server.RouterSpec
140+ Servant.Server.StaticFilesSpec
136 Servant.Server.StreamingSpec
141 Servant.Server.StreamingSpec
137 Servant.Server.UsingContextSpec
142 Servant.Server.UsingContextSpec
138 Servant.Server.UsingContextSpec.TestCombinators
143 Servant.Server.UsingContextSpec.TestCombinators
139 Servant.HoistSpec
144 Servant.HoistSpec
140 Servant.ServerSpec
145 Servant.ServerSpec
141− Servant.Utils.StaticFilesSpec
142
146
143 -- Dependencies inherited from the library. No need to specify bounds.
147 -- Dependencies inherited from the library. No need to specify bounds.
144 build-depends:
148 build-depends:
servant-server/src/Servant.hs+2−2
@@ -7,7 +7,7 @@ module Servant (
7 module Servant.Server,
7 module Servant.Server,
8 -- | Utilities on top of the servant core
8 -- | Utilities on top of the servant core
9 module Servant.Links,
9 module Servant.Links,
10− module Servant.Utils.StaticFiles,
10+ module Servant.Server.StaticFiles,
11 -- | Useful re-exports
11 -- | Useful re-exports
12 Proxy(..),
12 Proxy(..),
13 throwError
13 throwError
@@ -19,4 +19,4 @@ import Data.Proxy
19import Servant.API
19import Servant.API
20import Servant.Links
20import Servant.Links
21import Servant.Server
21import Servant.Server
22−import Servant.Utils.StaticFiles
22+import Servant.Server.StaticFiles
servant-server/src/Servant/Server/StaticFiles.hsnew file+92
@@ -0,0 +1,92 @@
1+{-# LANGUAGE CPP #-}
2+-- | This module defines server-side handlers that lets you serve static files.
3+--
4+-- The most common needs for a web application are covered by
5+-- 'serveDirectoryWebApp`, but the other variants allow you to use
6+-- different `StaticSettings` and 'serveDirectoryWith' even allows you
7+-- to specify arbitrary 'StaticSettings' to be used for serving static files.
8+module Servant.Server.StaticFiles
9+ ( serveDirectoryWebApp
10+ , serveDirectoryWebAppLookup
11+ , serveDirectoryFileServer
12+ , serveDirectoryEmbedded
13+ , serveDirectoryWith
14+ , -- * Deprecated
15+ serveDirectory
16+ ) where
17+
18+import Data.ByteString
19+ (ByteString)
20+import Network.Wai.Application.Static
21+import Servant.API.Raw
22+ (Raw)
23+import Servant.Server
24+ (ServerT, Tagged (..))
25+import System.FilePath
26+ (addTrailingPathSeparator)
27+#if !MIN_VERSION_wai_app_static(3,1,0)
28+import Filesystem.Path.CurrentOS
29+ (decodeString)
30+#endif
31+import WaiAppStatic.Storage.Filesystem
32+ (ETagLookup)
33+
34+-- | Serve anything under the specified directory as a 'Raw' endpoint.
35+--
36+-- @
37+-- type MyApi = "static" :> Raw
38+--
39+-- server :: Server MyApi
40+-- server = serveDirectoryWebApp "\/var\/www"
41+-- @
42+--
43+-- would capture any request to @\/static\/\<something>@ and look for
44+-- @\<something>@ under @\/var\/www@.
45+--
46+-- It will do its best to guess the MIME type for that file, based on the extension,
47+-- and send an appropriate /Content-Type/ header if possible.
48+--
49+-- If your goal is to serve HTML, CSS and Javascript files that use the rest of the API
50+-- as a webapp backend, you will most likely not want the static files to be hidden
51+-- behind a /\/static\// prefix. In that case, remember to put the 'serveDirectoryWebApp'
52+-- handler in the last position, because /servant/ will try to match the handlers
53+-- in order.
54+--
55+-- Corresponds to the `defaultWebAppSettings` `StaticSettings` value.
56+serveDirectoryWebApp :: FilePath -> ServerT Raw m
57+serveDirectoryWebApp = serveDirectoryWith . defaultWebAppSettings . fixPath
58+
59+-- | Same as 'serveDirectoryWebApp', but uses `defaultFileServerSettings`.
60+serveDirectoryFileServer :: FilePath -> ServerT Raw m
61+serveDirectoryFileServer = serveDirectoryWith . defaultFileServerSettings . fixPath
62+
63+-- | Same as 'serveDirectoryWebApp', but uses 'webAppSettingsWithLookup'.
64+serveDirectoryWebAppLookup :: ETagLookup -> FilePath -> ServerT Raw m
65+serveDirectoryWebAppLookup etag =
66+ serveDirectoryWith . flip webAppSettingsWithLookup etag . fixPath
67+
68+-- | Uses 'embeddedSettings'.
69+serveDirectoryEmbedded :: [(FilePath, ByteString)] -> ServerT Raw m
70+serveDirectoryEmbedded files = serveDirectoryWith (embeddedSettings files)
71+
72+-- | Alias for 'staticApp'. Lets you serve a directory
73+-- with arbitrary 'StaticSettings'. Useful when you want
74+-- particular settings not covered by the four other
75+-- variants. This is the most flexible method.
76+serveDirectoryWith :: StaticSettings -> ServerT Raw m
77+serveDirectoryWith = Tagged . staticApp
78+
79+-- | Same as 'serveDirectoryFileServer'. It used to be the only
80+-- file serving function in servant pre-0.10 and will be kept
81+-- around for a few versions, but is deprecated.
82+serveDirectory :: FilePath -> ServerT Raw m
83+serveDirectory = serveDirectoryFileServer
84+{-# DEPRECATED serveDirectory "Use serveDirectoryFileServer instead" #-}
85+
86+fixPath :: FilePath -> FilePath
87+fixPath =
88+#if MIN_VERSION_wai_app_static(3,1,0)
89+ addTrailingPathSeparator
90+#else
91+ decodeString . addTrailingPathSeparator
92+#endif
servant-server/src/Servant/Utils/StaticFiles.hs+4−90
@@ -1,92 +1,6 @@
1−{-# LANGUAGE CPP #-}
2−-- | This module defines server-side handlers that lets you serve static files.
3−--
4−-- The most common needs for a web application are covered by
5−-- 'serveDirectoryWebApp`, but the other variants allow you to use
6−-- different `StaticSettings` and 'serveDirectoryWith' even allows you
7−-- to specify arbitrary 'StaticSettings' to be used for serving static files.
8module Servant.Utils.StaticFiles
1module Servant.Utils.StaticFiles
9− ( serveDirectoryWebApp
2+ {-# DEPRECATED "Use Servant.ServerStaticFiles." #-}
10− , serveDirectoryWebAppLookup
3+ ( module Servant.Server.StaticFiles )
11− , serveDirectoryFileServer
12− , serveDirectoryEmbedded
13− , serveDirectoryWith
14− , -- * Deprecated
15− serveDirectory
16− ) where
4+ where
17
5
18−import Data.ByteString
19− (ByteString)
20−import Network.Wai.Application.Static
21−import Servant.API.Raw
22− (Raw)
23−import Servant.Server
6+import Servant.Server.StaticFiles
24− (ServerT, Tagged (..))
25−import System.FilePath
26− (addTrailingPathSeparator)
27−#if !MIN_VERSION_wai_app_static(3,1,0)
28−import Filesystem.Path.CurrentOS
29− (decodeString)
30−#endif
31−import WaiAppStatic.Storage.Filesystem
32− (ETagLookup)
33−
34−-- | Serve anything under the specified directory as a 'Raw' endpoint.
35−--
36−-- @
37−-- type MyApi = "static" :> Raw
38−--
39−-- server :: Server MyApi
40−-- server = serveDirectoryWebApp "\/var\/www"
41−-- @
42−--
43−-- would capture any request to @\/static\/\<something>@ and look for
44−-- @\<something>@ under @\/var\/www@.
45−--
46−-- It will do its best to guess the MIME type for that file, based on the extension,
47−-- and send an appropriate /Content-Type/ header if possible.
48−--
49−-- If your goal is to serve HTML, CSS and Javascript files that use the rest of the API
50−-- as a webapp backend, you will most likely not want the static files to be hidden
51−-- behind a /\/static\// prefix. In that case, remember to put the 'serveDirectoryWebApp'
52−-- handler in the last position, because /servant/ will try to match the handlers
53−-- in order.
54−--
55−-- Corresponds to the `defaultWebAppSettings` `StaticSettings` value.
56−serveDirectoryWebApp :: FilePath -> ServerT Raw m
57−serveDirectoryWebApp = serveDirectoryWith . defaultWebAppSettings . fixPath
58−
59−-- | Same as 'serveDirectoryWebApp', but uses `defaultFileServerSettings`.
60−serveDirectoryFileServer :: FilePath -> ServerT Raw m
61−serveDirectoryFileServer = serveDirectoryWith . defaultFileServerSettings . fixPath
62−
63−-- | Same as 'serveDirectoryWebApp', but uses 'webAppSettingsWithLookup'.
64−serveDirectoryWebAppLookup :: ETagLookup -> FilePath -> ServerT Raw m
65−serveDirectoryWebAppLookup etag =
66− serveDirectoryWith . flip webAppSettingsWithLookup etag . fixPath
67−
68−-- | Uses 'embeddedSettings'.
69−serveDirectoryEmbedded :: [(FilePath, ByteString)] -> ServerT Raw m
70−serveDirectoryEmbedded files = serveDirectoryWith (embeddedSettings files)
71−
72−-- | Alias for 'staticApp'. Lets you serve a directory
73−-- with arbitrary 'StaticSettings'. Useful when you want
74−-- particular settings not covered by the four other
75−-- variants. This is the most flexible method.
76−serveDirectoryWith :: StaticSettings -> ServerT Raw m
77−serveDirectoryWith = Tagged . staticApp
78−
79−-- | Same as 'serveDirectoryFileServer'. It used to be the only
80−-- file serving function in servant pre-0.10 and will be kept
81−-- around for a few versions, but is deprecated.
82−serveDirectory :: FilePath -> ServerT Raw m
83−serveDirectory = serveDirectoryFileServer
84−{-# DEPRECATED serveDirectory "Use serveDirectoryFileServer instead" #-}
85−
86−fixPath :: FilePath -> FilePath
87−fixPath =
88−#if MIN_VERSION_wai_app_static(3,1,0)
89− addTrailingPathSeparator
90−#else
91− decodeString . addTrailingPathSeparator
92−#endif
servant-server/test/Servant/Utils/StaticFilesSpec.hs → servant-server/test/Servant/Server/StaticFilesSpec.hsrenamed+3−3
@@ -3,7 +3,7 @@
3{-# LANGUAGE OverloadedStrings #-}
3{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE TypeOperators #-}
4{-# LANGUAGE TypeOperators #-}
5{-# OPTIONS_GHC -fno-warn-orphans #-}
5{-# OPTIONS_GHC -fno-warn-orphans #-}
6−module Servant.Utils.StaticFilesSpec where
6+module Servant.Server.StaticFilesSpec where
7
7
8import Control.Exception
8import Control.Exception
9 (bracket)
9 (bracket)
@@ -24,10 +24,10 @@ import Servant.API
24 ((:<|>) ((:<|>)), (:>), Capture, Get, JSON, Raw)
24 ((:<|>) ((:<|>)), (:>), Capture, Get, JSON, Raw)
25import Servant.Server
25import Servant.Server
26 (Server, serve)
26 (Server, serve)
27+import Servant.Server.StaticFiles
28+ (serveDirectoryFileServer)
27import Servant.ServerSpec
29import Servant.ServerSpec
28 (Person (Person))
30 (Person (Person))
29−import Servant.Utils.StaticFiles
30− (serveDirectoryFileServer)
31
31
32type Api =
32type Api =
33 "dummy_api" :> Capture "person_name" String :> Get '[JSON] Person
33 "dummy_api" :> Capture "person_name" String :> Get '[JSON] Person