on master help sign in
Revamp static file serving module.

Instead of only exposing 'serveDirectory', which picks a specific static file serving strategy (file server settings), we now expose 4 different variants each corresponding to an variant of StaticSettings in wai-app-static. In addition to these, we expose a more flexible 'serveDirectoryWith' function which allows the user to specify some arbitrary StaticSettings, if the 4 existing variants do not cover a user's needs.

commit8efabed5c6abd06c9d6c98ad206e66f217ffc125 authorAlp Mestanogullari <alpmestan@gmail.com> dateMon 9 Jan 2017, 00:37 (10 years ago) parentc09c0cf
3 files changed+54−18
doc/tutorial/Javascript.lhs+1−1 doc/tutorial/Server.lhs+7−3 servant-server/src/Servant/Utils/StaticFiles.hs+46−14
doc/tutorial/Javascript.lhs+1−1
@@ -136,7 +136,7 @@ server = randomPoint
136136
137137server' :: Server API'
138138server' = server
139− :<|> serveDirectory "static"
139+ :<|> serveDirectoryWebApp "static"
140140
141141app :: Application
142142app = serve api' server'
doc/tutorial/Server.lhs+7−3
@@ -787,10 +787,10 @@ directory serving WAI application, namely:
787787
788788``` haskell ignore
789789-- exported by Servant and Servant.Server
790−serveDirectory :: FilePath -> Server Raw
790+serveDirectoryWebApp :: FilePath -> Server Raw
791791```
792792
793−`serveDirectory`'s argument must be a path to a valid directory.
793+`serveDirectoryWebApp`'s argument must be a path to a valid directory.
794794
795795Here's an example API that will serve some static files:
796796
@@ -807,7 +807,7 @@ staticAPI = Proxy
807807
808808``` haskell
809809server7 :: Server StaticAPI
810−server7 = serveDirectory "static-files"
810+server7 = serveDirectoryWebApp "static-files"
811811
812812app3 :: Application
813813app3 = serve staticAPI server7
@@ -821,6 +821,10 @@ In other words: If a client requests `/static/foo.txt`, the server will look for
821821`./static-files/foo.txt`. If that file exists it'll succeed and serve the file.
822822If it doesn't exist, the handler will fail with a `404` status code.
823823
824+`serveDirectoryWebApp` uses some standard settings that fit the use case of
825+serving static files for most web apps. You can find out about the other
826+options in the documentation of the `Servant.Utils.StaticFiles` module.
827+
824828## Nested APIs
825829
826830Let's see how you can define APIs in a modular way, while avoiding repetition.
servant-server/src/Servant/Utils/StaticFiles.hs+46−14
@@ -1,20 +1,27 @@
11{-# 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.
33--
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
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+ ) where
915
10−import Network.Wai.Application.Static (defaultFileServerSettings,
11− staticApp)
16+import Data.ByteString (ByteString)
17+import Network.Wai.Application.Static
1218import Servant.API.Raw (Raw)
1319import Servant.Server (Server)
1420import System.FilePath (addTrailingPathSeparator)
1521#if !MIN_VERSION_wai_app_static(3,1,0)
1622import Filesystem.Path.CurrentOS (decodeString)
1723#endif
24+import WaiAppStatic.Storage.Filesystem (ETagLookup)
1825
1926-- | Serve anything under the specified directory as a 'Raw' endpoint.
2027--
@@ -22,7 +29,7 @@ import Filesystem.Path.CurrentOS (decodeString)
2229-- type MyApi = "static" :> Raw
2330--
2431-- server :: Server MyApi
25−-- server = serveDirectory "\/var\/www"
32+-- server = serveDirectoryWebApp "\/var\/www"
2633-- @
2734--
2835-- would capture any request to @\/static\/\<something>@ and look for
@@ -33,13 +40,38 @@ import Filesystem.Path.CurrentOS (decodeString)
3340--
3441-- If your goal is to serve HTML, CSS and Javascript files that use the rest of the API
3542-- 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'
3744-- handler in the last position, because /servant/ will try to match the handlers
3845-- in order.
39−serveDirectory :: FilePath -> Server Raw
40−serveDirectory =
46+--
47+-- Corresponds to the `defaultWebAppSettings` `StaticSettings` value.
48+serveDirectoryWebApp :: FilePath -> Server Raw
49+serveDirectoryWebApp = staticApp . defaultWebAppSettings . fixPath
50+
51+-- | Same as 'serveDirectoryWebApp', but uses `defaultFileServerSettings`.
52+serveDirectoryFileServer :: FilePath -> Server Raw
53+serveDirectoryFileServer = staticApp . defaultFileServerSettings . fixPath
54+
55+-- | Same as 'serveDirectoryWebApp', but uses 'webAppSettingsWithLookup'.
56+serveDirectoryWebAppLookup :: ETagLookup -> FilePath -> Server Raw
57+serveDirectoryWebAppLookup etag =
58+ staticApp . flip webAppSettingsWithLookup etag . fixPath
59+
60+-- | Uses 'embeddedSettings'.
61+serveDirectoryEmbedded :: [(FilePath, ByteString)] -> Server Raw
62+serveDirectoryEmbedded files = staticApp (embeddedSettings files)
63+
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
70+
71+fixPath :: FilePath -> FilePath
72+fixPath =
4173#if MIN_VERSION_wai_app_static(3,1,0)
42− staticApp . defaultFileServerSettings . addTrailingPathSeparator
74+ addTrailingPathSeparator
4375#else
44− staticApp . defaultFileServerSettings . decodeString . addTrailingPathSeparator
76+ decodeString . addTrailingPathSeparator
4577#endif