2015-05-15 12:58:12 +02:00
|
|
|
{-# LANGUAGE CPP #-}
|
2017-01-09 00:37:37 +01:00
|
|
|
-- | This module defines server-side handlers that lets you serve static files.
|
2014-12-10 16:10:57 +01:00
|
|
|
--
|
2017-01-09 00:37:37 +01:00
|
|
|
-- The most common needs for a web application are covered by
|
|
|
|
-- 'serveDirectoryWebApp`, but the other variants allow you to use
|
|
|
|
-- different `StaticSettings` and 'serveDirectoryWith' even allows you
|
|
|
|
-- to specify arbitrary 'StaticSettings' to be used for serving static files.
|
|
|
|
module Servant.Utils.StaticFiles
|
|
|
|
( serveDirectoryWebApp
|
|
|
|
, serveDirectoryWebAppLookup
|
|
|
|
, serveDirectoryFileServer
|
|
|
|
, serveDirectoryEmbedded
|
|
|
|
, serveDirectoryWith
|
2017-01-09 11:02:02 +01:00
|
|
|
, -- * Deprecated
|
|
|
|
serveDirectory
|
2017-01-09 00:37:37 +01:00
|
|
|
) where
|
2014-12-10 16:10:57 +01:00
|
|
|
|
2017-01-09 00:37:37 +01:00
|
|
|
import Data.ByteString (ByteString)
|
|
|
|
import Network.Wai.Application.Static
|
2015-08-17 23:56:29 +02:00
|
|
|
import Servant.API.Raw (Raw)
|
2015-12-02 21:48:12 +01:00
|
|
|
import Servant.Server (Server, Tagged (..))
|
2015-08-17 23:56:29 +02:00
|
|
|
import System.FilePath (addTrailingPathSeparator)
|
2015-05-15 12:58:12 +02:00
|
|
|
#if !MIN_VERSION_wai_app_static(3,1,0)
|
2015-08-17 23:56:29 +02:00
|
|
|
import Filesystem.Path.CurrentOS (decodeString)
|
2015-05-15 12:58:12 +02:00
|
|
|
#endif
|
2017-01-09 00:37:37 +01:00
|
|
|
import WaiAppStatic.Storage.Filesystem (ETagLookup)
|
2014-12-10 16:10:57 +01:00
|
|
|
|
|
|
|
-- | Serve anything under the specified directory as a 'Raw' endpoint.
|
|
|
|
--
|
|
|
|
-- @
|
|
|
|
-- type MyApi = "static" :> Raw
|
|
|
|
--
|
|
|
|
-- server :: Server MyApi
|
2017-01-09 00:37:37 +01:00
|
|
|
-- server = serveDirectoryWebApp "\/var\/www"
|
2014-12-10 16:10:57 +01:00
|
|
|
-- @
|
|
|
|
--
|
|
|
|
-- would capture any request to @\/static\/\<something>@ and look for
|
|
|
|
-- @\<something>@ under @\/var\/www@.
|
|
|
|
--
|
|
|
|
-- It will do its best to guess the MIME type for that file, based on the extension,
|
|
|
|
-- and send an appropriate /Content-Type/ header if possible.
|
|
|
|
--
|
|
|
|
-- 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
|
2017-01-09 00:37:37 +01:00
|
|
|
-- behind a /\/static\// prefix. In that case, remember to put the 'serveDirectoryWebApp'
|
2014-12-10 16:10:57 +01:00
|
|
|
-- handler in the last position, because /servant/ will try to match the handlers
|
|
|
|
-- in order.
|
2017-01-09 00:37:37 +01:00
|
|
|
--
|
|
|
|
-- Corresponds to the `defaultWebAppSettings` `StaticSettings` value.
|
|
|
|
serveDirectoryWebApp :: FilePath -> Server Raw
|
2015-12-02 21:48:12 +01:00
|
|
|
serveDirectoryWebApp = serveDirectoryWith . defaultWebAppSettings . fixPath
|
2017-01-09 00:37:37 +01:00
|
|
|
|
|
|
|
-- | Same as 'serveDirectoryWebApp', but uses `defaultFileServerSettings`.
|
|
|
|
serveDirectoryFileServer :: FilePath -> Server Raw
|
2015-12-02 21:48:12 +01:00
|
|
|
serveDirectoryFileServer = serveDirectoryWith . defaultFileServerSettings . fixPath
|
2017-01-09 00:37:37 +01:00
|
|
|
|
|
|
|
-- | Same as 'serveDirectoryWebApp', but uses 'webAppSettingsWithLookup'.
|
|
|
|
serveDirectoryWebAppLookup :: ETagLookup -> FilePath -> Server Raw
|
|
|
|
serveDirectoryWebAppLookup etag =
|
2015-12-02 21:48:12 +01:00
|
|
|
serveDirectoryWith . flip webAppSettingsWithLookup etag . fixPath
|
2017-01-09 00:37:37 +01:00
|
|
|
|
|
|
|
-- | Uses 'embeddedSettings'.
|
|
|
|
serveDirectoryEmbedded :: [(FilePath, ByteString)] -> Server Raw
|
2015-12-02 21:48:12 +01:00
|
|
|
serveDirectoryEmbedded files = serveDirectoryWith (embeddedSettings files)
|
2017-01-09 00:37:37 +01:00
|
|
|
|
|
|
|
-- | 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
|
2015-12-02 21:48:12 +01:00
|
|
|
serveDirectoryWith = Tagged . staticApp
|
2017-01-09 00:37:37 +01:00
|
|
|
|
2017-01-09 11:02:02 +01:00
|
|
|
-- | Same as 'serveDirectoryFileServer'. It used to be the only
|
|
|
|
-- file serving function in servant pre-0.10 and will be kept
|
|
|
|
-- around for a few versions, but is deprecated.
|
|
|
|
serveDirectory :: FilePath -> Server Raw
|
|
|
|
serveDirectory = serveDirectoryFileServer
|
|
|
|
{-# DEPRECATED serveDirectory "Use serveDirectoryFileServer instead" #-}
|
|
|
|
|
2017-01-09 00:37:37 +01:00
|
|
|
fixPath :: FilePath -> FilePath
|
|
|
|
fixPath =
|
2015-05-15 12:58:12 +02:00
|
|
|
#if MIN_VERSION_wai_app_static(3,1,0)
|
2017-01-09 00:37:37 +01:00
|
|
|
addTrailingPathSeparator
|
2015-05-15 12:58:12 +02:00
|
|
|
#else
|
2017-01-09 00:37:37 +01:00
|
|
|
decodeString . addTrailingPathSeparator
|
2015-05-15 12:58:12 +02:00
|
|
|
#endif
|