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.
This commit is contained in:
Alp Mestanogullari 2017-01-09 00:37:37 +01:00 committed by Oleg Grenrus
parent c09c0cfd3f
commit 8efabed5c6
3 changed files with 54 additions and 18 deletions

View file

@ -136,7 +136,7 @@ server = randomPoint
server' :: Server API' server' :: Server API'
server' = server server' = server
:<|> serveDirectory "static" :<|> serveDirectoryWebApp "static"
app :: Application app :: Application
app = serve api' server' app = serve api' server'

View file

@ -787,10 +787,10 @@ directory serving WAI application, namely:
``` haskell ignore ``` haskell ignore
-- exported by Servant and Servant.Server -- exported by Servant and Servant.Server
serveDirectory :: FilePath -> Server Raw serveDirectoryWebApp :: FilePath -> Server Raw
``` ```
`serveDirectory`'s argument must be a path to a valid directory. `serveDirectoryWebApp`'s argument must be a path to a valid directory.
Here's an example API that will serve some static files: Here's an example API that will serve some static files:
@ -807,7 +807,7 @@ staticAPI = Proxy
``` haskell ``` haskell
server7 :: Server StaticAPI server7 :: Server StaticAPI
server7 = serveDirectory "static-files" server7 = serveDirectoryWebApp "static-files"
app3 :: Application app3 :: Application
app3 = serve staticAPI server7 app3 = serve staticAPI server7
@ -821,6 +821,10 @@ In other words: If a client requests `/static/foo.txt`, the server will look for
`./static-files/foo.txt`. If that file exists it'll succeed and serve the file. `./static-files/foo.txt`. If that file exists it'll succeed and serve the file.
If it doesn't exist, the handler will fail with a `404` status code. If it doesn't exist, the handler will fail with a `404` status code.
`serveDirectoryWebApp` uses some standard settings that fit the use case of
serving static files for most web apps. You can find out about the other
options in the documentation of the `Servant.Utils.StaticFiles` module.
## Nested APIs ## Nested APIs
Let's see how you can define APIs in a modular way, while avoiding repetition. Let's see how you can define APIs in a modular way, while avoiding repetition.

View file

@ -1,20 +1,27 @@
{-# LANGUAGE CPP #-} {-# LANGUAGE CPP #-}
-- | This module defines a sever-side handler that lets you serve static files. -- | This module defines server-side handlers that lets you serve static files.
-- --
-- - 'serveDirectory' lets you serve anything that lives under a particular -- The most common needs for a web application are covered by
-- directory on your filesystem. -- 'serveDirectoryWebApp`, but the other variants allow you to use
module Servant.Utils.StaticFiles ( -- different `StaticSettings` and 'serveDirectoryWith' even allows you
serveDirectory, -- to specify arbitrary 'StaticSettings' to be used for serving static files.
module Servant.Utils.StaticFiles
( serveDirectoryWebApp
, serveDirectoryWebAppLookup
, serveDirectoryFileServer
, serveDirectoryEmbedded
, serveDirectoryWith
) where ) where
import Network.Wai.Application.Static (defaultFileServerSettings, import Data.ByteString (ByteString)
staticApp) import Network.Wai.Application.Static
import Servant.API.Raw (Raw) import Servant.API.Raw (Raw)
import Servant.Server (Server) import Servant.Server (Server)
import System.FilePath (addTrailingPathSeparator) import System.FilePath (addTrailingPathSeparator)
#if !MIN_VERSION_wai_app_static(3,1,0) #if !MIN_VERSION_wai_app_static(3,1,0)
import Filesystem.Path.CurrentOS (decodeString) import Filesystem.Path.CurrentOS (decodeString)
#endif #endif
import WaiAppStatic.Storage.Filesystem (ETagLookup)
-- | Serve anything under the specified directory as a 'Raw' endpoint. -- | Serve anything under the specified directory as a 'Raw' endpoint.
-- --
@ -22,7 +29,7 @@ import Filesystem.Path.CurrentOS (decodeString)
-- type MyApi = "static" :> Raw -- type MyApi = "static" :> Raw
-- --
-- server :: Server MyApi -- server :: Server MyApi
-- server = serveDirectory "\/var\/www" -- server = serveDirectoryWebApp "\/var\/www"
-- @ -- @
-- --
-- would capture any request to @\/static\/\<something>@ and look for -- would capture any request to @\/static\/\<something>@ and look for
@ -33,13 +40,38 @@ import Filesystem.Path.CurrentOS (decodeString)
-- --
-- If your goal is to serve HTML, CSS and Javascript files that use the rest of the API -- If your goal is to serve HTML, CSS and Javascript files that use the rest of the API
-- as a webapp backend, you will most likely not want the static files to be hidden -- as a webapp backend, you will most likely not want the static files to be hidden
-- behind a /\/static\// prefix. In that case, remember to put the 'serveDirectory' -- behind a /\/static\// prefix. In that case, remember to put the 'serveDirectoryWebApp'
-- handler in the last position, because /servant/ will try to match the handlers -- handler in the last position, because /servant/ will try to match the handlers
-- in order. -- in order.
serveDirectory :: FilePath -> Server Raw --
serveDirectory = -- Corresponds to the `defaultWebAppSettings` `StaticSettings` value.
serveDirectoryWebApp :: FilePath -> Server Raw
serveDirectoryWebApp = staticApp . defaultWebAppSettings . fixPath
-- | Same as 'serveDirectoryWebApp', but uses `defaultFileServerSettings`.
serveDirectoryFileServer :: FilePath -> Server Raw
serveDirectoryFileServer = staticApp . defaultFileServerSettings . fixPath
-- | Same as 'serveDirectoryWebApp', but uses 'webAppSettingsWithLookup'.
serveDirectoryWebAppLookup :: ETagLookup -> FilePath -> Server Raw
serveDirectoryWebAppLookup etag =
staticApp . flip webAppSettingsWithLookup etag . fixPath
-- | Uses 'embeddedSettings'.
serveDirectoryEmbedded :: [(FilePath, ByteString)] -> Server Raw
serveDirectoryEmbedded files = staticApp (embeddedSettings files)
-- | Alias for 'staticApp'. Lets you serve a directory
-- with arbitrary 'StaticSettings'. Useful when you want
-- particular settings not covered by the four other
-- variants. This is the most flexible method.
serveDirectoryWith :: StaticSettings -> Server Raw
serveDirectoryWith = staticApp
fixPath :: FilePath -> FilePath
fixPath =
#if MIN_VERSION_wai_app_static(3,1,0) #if MIN_VERSION_wai_app_static(3,1,0)
staticApp . defaultFileServerSettings . addTrailingPathSeparator addTrailingPathSeparator
#else #else
staticApp . defaultFileServerSettings . decodeString . addTrailingPathSeparator decodeString . addTrailingPathSeparator
#endif #endif