git.iko.soy/typeable/servant
on master help sign in
Haddocks for the StaticFiles module
commit5435dd40327d1e587ada17fab047f5a33fe8b594 authorAlp Mestanogullari <alpmestan@gmail.com> dateTue 25 Nov 2014, 15:48 (12 years ago) parent5b5f530
1 file changed+38
src/Servant/Utils/StaticFiles.hs+38
src/Servant/Utils/StaticFiles.hs+38
@@ -1,3 +1,10 @@
1+-- | This module defines sever-side handlers that let you serve static files
2+-- and your API's docs.
3+--
4+-- - 'serveDirectory' lets you serve anything that lives under a particular
5+-- directory on your filesystem.
6+-- - 'serveDocumentation' lets you serve the markdown-version of the docs for
7+-- your API.
18module Servant.Utils.StaticFiles (
29 serveDirectory,
310 serveDocumentation,
@@ -14,10 +21,41 @@ import Servant.API.Raw
1421import Servant.Docs
1522import Servant.Server
1623
24+-- | Serve anything under the specified directory as a 'Raw' endpoint.
25+--
26+-- @
27+-- type MyApi = "static" :> Raw
28+--
29+-- server :: Server MyApi
30+-- server = serveDirectory "\/var\/www"
31+-- @
32+--
33+-- would capture any request to @\/static\/\<something>@ and look for
34+-- @\<something>@ under @\/var\/www@.
35+--
36+-- It will do its best to guess the MIME type for that file, based on the extension,
37+-- and send an appropriate /Content-Type/ header if possible.
38+--
39+-- If your goal is to serve HTML, CSS and Javascript files that use the rest of the API
40+-- as a webapp backend, you will most likely not want the static files to be hidden
41+-- behind a /\/static\// prefix. In that case, remember to put the 'serveDirectory'
42+-- handler in the last position, because /servant/ will try to match the handlers
43+-- in order.
1744serveDirectory :: FilePath -> Server Raw
1845serveDirectory documentRoot =
1946 staticApp (defaultFileServerSettings (decodeString (documentRoot ++ "/")))
2047
48+-- | Serve your API's docs as markdown embedded in an html \<pre> tag.
49+--
50+-- > type MyApi = "users" :> Get [User]
51+-- > :<|> "docs :> Raw
52+-- >
53+-- > apiProxy :: Proxy MyApi
54+-- > apiProxy = Proxy
55+-- >
56+-- > server :: Server MyApi
57+-- > server = listUsers
58+-- > :<|> serveDocumentation apiProxy
2159serveDocumentation :: HasDocs api => Proxy api -> Server Raw
2260serveDocumentation proxy _request respond =
2361 respond $ responseLBS ok200 [] $ cs $ toHtml $ markdown $ docs proxy