1
1
{-# LANGUAGE CPP #-}
2
- -- | This module defines a sever -side handler that lets you serve static files.
2
+ -- | This module defines server -side handlers that lets you serve static files.
3
3
--
4
- -- - 'serveDirectory' lets you serve anything that lives under a particular
5
- -- directory on your filesystem.
6
- module Servant.Utils.StaticFiles (
7
- serveDirectory ,
8
- ) where
9
-
10
- import Network.Wai.Application.Static (defaultFileServerSettings ,
11
- staticApp )
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.Utils.StaticFiles
9
+ ( serveDirectoryWebApp
10
+ , serveDirectoryWebAppLookup
11
+ , serveDirectoryFileServer
12
+ , serveDirectoryEmbedded
13
+ , serveDirectoryWith
14
+ , -- * Deprecated
15
+ serveDirectory
16
+ ) where
17
+
18
+ import Data.ByteString (ByteString )
19
+ import Network.Wai.Application.Static
12
20
import Servant.API.Raw (Raw )
13
21
import Servant.Server (Server )
14
22
import System.FilePath (addTrailingPathSeparator )
15
23
#if !MIN_VERSION_wai_app_static(3,1,0)
16
24
import Filesystem.Path.CurrentOS (decodeString )
17
25
#endif
26
+ import WaiAppStatic.Storage.Filesystem (ETagLookup )
18
27
19
28
-- | Serve anything under the specified directory as a 'Raw' endpoint.
20
29
--
21
30
-- @
22
31
-- type MyApi = "static" :> Raw
23
32
--
24
33
-- server :: Server MyApi
25
- -- server = serveDirectory "\/var\/www"
34
+ -- server = serveDirectoryWebApp "\/var\/www"
26
35
-- @
27
36
--
28
37
-- would capture any request to @\/static\/\<something>@ and look for
@@ -33,13 +42,45 @@ import Filesystem.Path.CurrentOS (decodeString)
33
42
--
34
43
-- If your goal is to serve HTML, CSS and Javascript files that use the rest of the API
35
44
-- as a webapp backend, you will most likely not want the static files to be hidden
36
- -- behind a /\/static\// prefix. In that case, remember to put the 'serveDirectory '
45
+ -- behind a /\/static\// prefix. In that case, remember to put the 'serveDirectoryWebApp '
37
46
-- handler in the last position, because /servant/ will try to match the handlers
38
47
-- in order.
48
+ --
49
+ -- Corresponds to the `defaultWebAppSettings` `StaticSettings` value.
50
+ serveDirectoryWebApp :: FilePath -> Server Raw
51
+ serveDirectoryWebApp = staticApp . defaultWebAppSettings . fixPath
52
+
53
+ -- | Same as 'serveDirectoryWebApp', but uses `defaultFileServerSettings`.
54
+ serveDirectoryFileServer :: FilePath -> Server Raw
55
+ serveDirectoryFileServer = staticApp . defaultFileServerSettings . fixPath
56
+
57
+ -- | Same as 'serveDirectoryWebApp', but uses 'webAppSettingsWithLookup'.
58
+ serveDirectoryWebAppLookup :: ETagLookup -> FilePath -> Server Raw
59
+ serveDirectoryWebAppLookup etag =
60
+ staticApp . flip webAppSettingsWithLookup etag . fixPath
61
+
62
+ -- | Uses 'embeddedSettings'.
63
+ serveDirectoryEmbedded :: [(FilePath , ByteString )] -> Server Raw
64
+ serveDirectoryEmbedded files = staticApp (embeddedSettings files)
65
+
66
+ -- | Alias for 'staticApp'. Lets you serve a directory
67
+ -- with arbitrary 'StaticSettings'. Useful when you want
68
+ -- particular settings not covered by the four other
69
+ -- variants. This is the most flexible method.
70
+ serveDirectoryWith :: StaticSettings -> Server Raw
71
+ serveDirectoryWith = staticApp
72
+
73
+ -- | Same as 'serveDirectoryFileServer'. It used to be the only
74
+ -- file serving function in servant pre-0.10 and will be kept
75
+ -- around for a few versions, but is deprecated.
39
76
serveDirectory :: FilePath -> Server Raw
40
- serveDirectory =
77
+ serveDirectory = serveDirectoryFileServer
78
+ {-# DEPRECATED serveDirectory "Use serveDirectoryFileServer instead" #-}
79
+
80
+ fixPath :: FilePath -> FilePath
81
+ fixPath =
41
82
#if MIN_VERSION_wai_app_static(3,1,0)
42
- staticApp . defaultFileServerSettings . addTrailingPathSeparator
83
+ addTrailingPathSeparator
43
84
#else
44
- staticApp . defaultFileServerSettings . decodeString . addTrailingPathSeparator
85
+ decodeString . addTrailingPathSeparator
45
86
#endif
0 commit comments