|
1 | | -{-# LANGUAGE CPP #-} |
2 | | --- | This module defines server-side handlers that lets you serve static files. |
3 | | --- |
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 | 1 | module Servant.Utils.StaticFiles |
9 | | - ( serveDirectoryWebApp |
10 | | - , serveDirectoryWebAppLookup |
11 | | - , serveDirectoryFileServer |
12 | | - , serveDirectoryEmbedded |
13 | | - , serveDirectoryWith |
14 | | - , -- * Deprecated |
15 | | - serveDirectory |
16 | | - ) where |
| 2 | + {-# DEPRECATED "Use Servant.ServerStaticFiles." #-} |
| 3 | + ( module Servant.Server.StaticFiles ) |
| 4 | + where |
17 | 5 |
|
18 | | -import Data.ByteString |
19 | | - (ByteString) |
20 | | -import Network.Wai.Application.Static |
21 | | -import Servant.API.Raw |
22 | | - (Raw) |
23 | | -import Servant.Server |
24 | | - (ServerT, Tagged (..)) |
25 | | -import System.FilePath |
26 | | - (addTrailingPathSeparator) |
27 | | -#if !MIN_VERSION_wai_app_static(3,1,0) |
28 | | -import Filesystem.Path.CurrentOS |
29 | | - (decodeString) |
30 | | -#endif |
31 | | -import WaiAppStatic.Storage.Filesystem |
32 | | - (ETagLookup) |
33 | | - |
34 | | --- | Serve anything under the specified directory as a 'Raw' endpoint. |
35 | | --- |
36 | | --- @ |
37 | | --- type MyApi = "static" :> Raw |
38 | | --- |
39 | | --- server :: Server MyApi |
40 | | --- server = serveDirectoryWebApp "\/var\/www" |
41 | | --- @ |
42 | | --- |
43 | | --- would capture any request to @\/static\/\<something>@ and look for |
44 | | --- @\<something>@ under @\/var\/www@. |
45 | | --- |
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. |
48 | | --- |
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 |
53 | | --- in order. |
54 | | --- |
55 | | --- Corresponds to the `defaultWebAppSettings` `StaticSettings` value. |
56 | | -serveDirectoryWebApp :: FilePath -> ServerT Raw m |
57 | | -serveDirectoryWebApp = serveDirectoryWith . defaultWebAppSettings . fixPath |
58 | | - |
59 | | --- | Same as 'serveDirectoryWebApp', but uses `defaultFileServerSettings`. |
60 | | -serveDirectoryFileServer :: FilePath -> ServerT Raw m |
61 | | -serveDirectoryFileServer = serveDirectoryWith . defaultFileServerSettings . fixPath |
62 | | - |
63 | | --- | Same as 'serveDirectoryWebApp', but uses 'webAppSettingsWithLookup'. |
64 | | -serveDirectoryWebAppLookup :: ETagLookup -> FilePath -> ServerT Raw m |
65 | | -serveDirectoryWebAppLookup etag = |
66 | | - serveDirectoryWith . flip webAppSettingsWithLookup etag . fixPath |
67 | | - |
68 | | --- | Uses 'embeddedSettings'. |
69 | | -serveDirectoryEmbedded :: [(FilePath, ByteString)] -> ServerT Raw m |
70 | | -serveDirectoryEmbedded files = serveDirectoryWith (embeddedSettings files) |
71 | | - |
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 |
78 | | - |
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" #-} |
85 | | - |
86 | | -fixPath :: FilePath -> FilePath |
87 | | -fixPath = |
88 | | -#if MIN_VERSION_wai_app_static(3,1,0) |
89 | | - addTrailingPathSeparator |
90 | | -#else |
91 | | - decodeString . addTrailingPathSeparator |
92 | | -#endif |
| 6 | +import Servant.Server.StaticFiles |
0 commit comments