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