@@ -1,92 +1,6 @@
2−-- | This module defines server-side handlers that lets you serve static files.
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.
8module Servant.Utils.StaticFiles
1module Servant.Utils.StaticFiles
9− ( serveDirectoryWebApp
2+ {-# DEPRECATED "Use Servant.ServerStaticFiles." #-}
10− , serveDirectoryWebAppLookup
3+ ( module Servant.Server.StaticFiles )
11− , serveDirectoryFileServer
12− , serveDirectoryEmbedded
18−import Data.ByteString
20−import Network.Wai.Application.Static
21−import Servant.API.Raw
23−import Servant.Server
6+import Servant.Server.StaticFiles
24− (ServerT, Tagged (..))
25−import System.FilePath
26− (addTrailingPathSeparator)
27−#if !MIN_VERSION_wai_app_static(3,1,0)
28−import Filesystem.Path.CurrentOS
31−import WaiAppStatic.Storage.Filesystem
34−-- | Serve anything under the specified directory as a 'Raw' endpoint.
37−-- type MyApi = "static" :> Raw
39−-- server :: Server MyApi
40−-- server = serveDirectoryWebApp "\/var\/www"
43−-- would capture any request to @\/static\/\<something>@ and look for
44−-- @\<something>@ under @\/var\/www@.
46−-- It will do its best to guess the MIME type for that file, based on the extension,
47−-- and send an appropriate /Content-Type/ header if possible.
49−-- If your goal is to serve HTML, CSS and Javascript files that use the rest of the API
50−-- as a webapp backend, you will most likely not want the static files to be hidden
51−-- behind a /\/static\// prefix. In that case, remember to put the 'serveDirectoryWebApp'
52−-- handler in the last position, because /servant/ will try to match the handlers
55−-- Corresponds to the `defaultWebAppSettings` `StaticSettings` value.
56−serveDirectoryWebApp :: FilePath -> ServerT Raw m
57−serveDirectoryWebApp = serveDirectoryWith . defaultWebAppSettings . fixPath
59−-- | Same as 'serveDirectoryWebApp', but uses `defaultFileServerSettings`.
60−serveDirectoryFileServer :: FilePath -> ServerT Raw m
61−serveDirectoryFileServer = serveDirectoryWith . defaultFileServerSettings . fixPath
63−-- | Same as 'serveDirectoryWebApp', but uses 'webAppSettingsWithLookup'.
64−serveDirectoryWebAppLookup :: ETagLookup -> FilePath -> ServerT Raw m
65−serveDirectoryWebAppLookup etag =
66− serveDirectoryWith . flip webAppSettingsWithLookup etag . fixPath
68−-- | Uses 'embeddedSettings'.
69−serveDirectoryEmbedded :: [(FilePath, ByteString)] -> ServerT Raw m
70−serveDirectoryEmbedded files = serveDirectoryWith (embeddedSettings files)
72−-- | Alias for 'staticApp'. Lets you serve a directory
73−-- with arbitrary 'StaticSettings'. Useful when you want
74−-- particular settings not covered by the four other
75−-- variants. This is the most flexible method.
76−serveDirectoryWith :: StaticSettings -> ServerT Raw m
77−serveDirectoryWith = Tagged . staticApp
79−-- | Same as 'serveDirectoryFileServer'. It used to be the only
80−-- file serving function in servant pre-0.10 and will be kept
81−-- around for a few versions, but is deprecated.
82−serveDirectory :: FilePath -> ServerT Raw m
83−serveDirectory = serveDirectoryFileServer
84−{-# DEPRECATED serveDirectory "Use serveDirectoryFileServer instead" #-}
86−fixPath :: FilePath -> FilePath
88−#if MIN_VERSION_wai_app_static(3,1,0)
89− addTrailingPathSeparator
91− decodeString . addTrailingPathSeparator