git.iko.soy/typeable/servant
on master help sign in
Deprecate S.Utils.StaticFiles in favor of S.Server.StaticFiles
commit374a7b88fb252ff7019b0c12a965770e6dce71fd authorOleg Grenrus <oleg.grenrus@iki.fi> dateThu 5 Jul 2018, 13:15 (8 years ago) parent187c3f4
5 files changed+106−96
servant-server/servant-server.cabal+5−1 servant-server/src/Servant.hs+2−2 servant-server/src/Servant/Server/StaticFiles.hs+92 servant-server/src/Servant/Utils/StaticFiles.hs+4−90 servant-server/test/Servant/Utils/StaticFilesSpec.hs → servant-server/test/Servant/Server/StaticFilesSpec.hs+3−3
servant-server/servant-server.cabal+5−1
@@ -54,6 +54,10 @@ library
5454 Servant.Server.Internal.Router
5555 Servant.Server.Internal.RoutingApplication
5656 Servant.Server.Internal.ServantErr
57+ Servant.Server.StaticFiles
58+
59+ -- deprecated
60+ exposed-modules:
5761 Servant.Utils.StaticFiles
5862
5963 -- Bundled with GHC: Lower bound to not force re-installs
@@ -133,12 +137,12 @@ test-suite spec
133137 Servant.Server.Internal.ContextSpec
134138 Servant.Server.Internal.RoutingApplicationSpec
135139 Servant.Server.RouterSpec
140+ Servant.Server.StaticFilesSpec
136141 Servant.Server.StreamingSpec
137142 Servant.Server.UsingContextSpec
138143 Servant.Server.UsingContextSpec.TestCombinators
139144 Servant.HoistSpec
140145 Servant.ServerSpec
141− Servant.Utils.StaticFilesSpec
142146
143147 -- Dependencies inherited from the library. No need to specify bounds.
144148 build-depends:
servant-server/src/Servant.hs+2−2
@@ -7,7 +7,7 @@ module Servant (
77 module Servant.Server,
88 -- | Utilities on top of the servant core
99 module Servant.Links,
10− module Servant.Utils.StaticFiles,
10+ module Servant.Server.StaticFiles,
1111 -- | Useful re-exports
1212 Proxy(..),
1313 throwError
@@ -19,4 +19,4 @@ import Data.Proxy
1919import Servant.API
2020import Servant.Links
2121import 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.
81module Servant.Utils.StaticFiles
9− ( serveDirectoryWebApp
10− , serveDirectoryWebAppLookup
11− , serveDirectoryFileServer
12− , serveDirectoryEmbedded
13− , serveDirectoryWith
14− , -- * Deprecated
15− serveDirectory
16− ) where
2+ {-# DEPRECATED "Use Servant.ServerStaticFiles." #-}
3+ ( module Servant.Server.StaticFiles )
4+ where
175
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
6+import Servant.Server.StaticFiles
servant-server/test/Servant/Utils/StaticFilesSpec.hs → servant-server/test/Servant/Server/StaticFilesSpec.hsrenamed+3−3
@@ -3,7 +3,7 @@
33{-# LANGUAGE OverloadedStrings #-}
44{-# LANGUAGE TypeOperators #-}
55{-# OPTIONS_GHC -fno-warn-orphans #-}
6−module Servant.Utils.StaticFilesSpec where
6+module Servant.Server.StaticFilesSpec where
77
88import Control.Exception
99 (bracket)
@@ -24,10 +24,10 @@ import Servant.API
2424 ((:<|>) ((:<|>)), (:>), Capture, Get, JSON, Raw)
2525import Servant.Server
2626 (Server, serve)
27+import Servant.Server.StaticFiles
28+ (serveDirectoryFileServer)
2729import Servant.ServerSpec
2830 (Person (Person))
29−import Servant.Utils.StaticFiles
30− (serveDirectoryFileServer)
3131
3232type Api =
3333 "dummy_api" :> Capture "person_name" String :> Get '[JSON] Person