git.iko.soy/typeable/servant
on f76c729 help sign in
36 lines 1.4 kB history raw
1-- | This module defines a sever-side handler that lets you serve static files.
2--
3-- - 'serveDirectory' lets you serve anything that lives under a particular
4-- directory on your filesystem.
5module Servant.Utils.StaticFiles (
6 serveDirectory,
7 ) where
9import Filesystem.Path.CurrentOS (decodeString)
10import Network.Wai.Application.Static (staticApp, defaultFileServerSettings)
11import Servant.API.Raw (Raw)
12import Servant.Server (Server)
14-- | Serve anything under the specified directory as a 'Raw' endpoint.
15--
16-- @
17-- type MyApi = "static" :> Raw
18--
19-- server :: Server MyApi
20-- server = serveDirectory "\/var\/www"
21-- @
22--
23-- would capture any request to @\/static\/\<something>@ and look for
24-- @\<something>@ under @\/var\/www@.
25--
26-- It will do its best to guess the MIME type for that file, based on the extension,
27-- and send an appropriate /Content-Type/ header if possible.
28--
29-- If your goal is to serve HTML, CSS and Javascript files that use the rest of the API
30-- as a webapp backend, you will most likely not want the static files to be hidden
31-- behind a /\/static\// prefix. In that case, remember to put the 'serveDirectory'
32-- handler in the last position, because /servant/ will try to match the handlers
33-- in order.
34serveDirectory :: FilePath -> Server Raw
35serveDirectory documentRoot =
36 staticApp (defaultFileServerSettings (decodeString (documentRoot ++ "/")))