1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
-- | Implements a basic static file server for previewing options
--
{-# LANGUAGE OverloadedStrings #-}
module Hakyll.Network.Server
( staticServer
) where
import Control.Monad.Trans (liftIO)
import Control.Applicative ((<$>))
import Codec.Binary.UTF8.String
import System.FilePath ((</>))
import System.Directory (doesFileExist)
import qualified Data.ByteString as SB
import Snap.Util.FileServe (fileServeSingle)
import Snap.Types (Snap, rqURI, getRequest, writeBS)
import Snap.Http.Server ( httpServe, setAccessLog, setErrorLog, addListen
, ConfigListen (..), emptyConfig
)
import Hakyll.Web.Util.String (replaceAll)
-- | The first file in the list that actually exists is returned
--
findFile :: [FilePath] -> IO (Maybe FilePath)
findFile [] = return Nothing
findFile (x : xs) = do
exists <- doesFileExist x
if exists then return (Just x) else findFile xs
-- | Serve a given directory
--
static :: FilePath -- ^ Directory to serve
-> (FilePath -> IO ()) -- ^ Pre-serve hook
-> Snap ()
static directory preServe = do
-- Obtain the path
uri <- rqURI <$> getRequest
let filePath = replaceAll "\\?$" (const "") -- Remove trailing ?
$ replaceAll "#[^#]*$" (const "") -- Remove #section
$ replaceAll "^/" (const "") -- Remove leading /
$ decode $ SB.unpack uri
-- Try to find the requested file
r <- liftIO $ findFile $ map (directory </>) $
[ filePath
, filePath </> "index.htm"
, filePath </> "index.html"
]
case r of
-- Not found, error
Nothing -> writeBS "Not found"
-- Found, serve
Just f -> do
liftIO $ preServe f
fileServeSingle f
-- | Main method, runs a static server in the given directory
--
staticServer :: FilePath -- ^ Directory to serve
-> (FilePath -> IO ()) -- ^ Pre-serve hook
-> Int -- ^ Port to listen on
-> IO () -- ^ Blocks forever
staticServer directory preServe port =
httpServe config $ static directory preServe
where
-- Snap server config
config = addListen (ListenHttp "0.0.0.0" port)
$ setAccessLog Nothing
$ setErrorLog Nothing
$ emptyConfig
|