@@ -6,56 +6,27 @@ module Hakyll.Web.Preview.Server
8import Control.Monad.Trans (liftIO)
8import Control.Monad.Trans (liftIO)
9−import Control.Applicative ((<$>))
10−import Codec.Binary.UTF8.String
11−import Network.HTTP.Base (urlDecode)
12−import System.FilePath ((</>))
13−import System.Directory (doesFileExist)
15−import qualified Data.ByteString as SB
10+import Snap.Types (Snap)
16−import Snap.Util.FileServe (serveFile)
11+import Snap.Util.FileServe ( DirectoryConfig (..), fancyDirectoryConfig
17−import Snap.Types (Snap, rqURI, getRequest, writeBS)
12+ , serveDirectoryWith
18−import Snap.Http.Server ( httpServe, setAccessLog, setErrorLog, addListen
14+import Snap.Http.Server ( httpServe, setAccessLog, setErrorLog
19− , ConfigListen (..), emptyConfig
15+ , setPort, emptyConfig
22−import Hakyll.Core.Util.String (replaceAll)
24−-- | The first file in the list that actually exists is returned
26−findFile :: [FilePath] -> IO (Maybe FilePath)
27−findFile [] = return Nothing
28−findFile (x : xs) = do
29− exists <- doesFileExist x
30− if exists then return (Just x) else findFile xs
32-- | Serve a given directory
18-- | Serve a given directory
34static :: FilePath -- ^ Directory to serve
20static :: FilePath -- ^ Directory to serve
35 -> (FilePath -> IO ()) -- ^ Pre-serve hook
21 -> (FilePath -> IO ()) -- ^ Pre-serve hook
36 -> Snap ()
22 -> Snap ()
37−static directory preServe = do
23+static directory preServe =
38− -- Obtain the path
24+ serveDirectoryWith directoryConfig directory
39− uri <- rqURI <$> getRequest
25+ where
40− let filePath = replaceAll "\\?.*$" (const "") -- Remove trailing ?
26+ directoryConfig :: DirectoryConfig Snap
41− $ replaceAll "#[^#]*$" (const "") -- Remove #section
27+ directoryConfig = fancyDirectoryConfig
42− $ replaceAll "^/" (const "") -- Remove leading /
43− $ urlDecode $ decode $ SB.unpack uri
45− -- Try to find the requested file
46− r <- liftIO $ findFile $ map (directory </>) $
48− , filePath </> "index.htm"
49− , filePath </> "index.html"
54− Nothing -> writeBS "Not found"
57− liftIO $ preServe f
28+ { preServeHook = liftIO . preServe
60-- | Main method, runs a static server in the given directory
31-- | Main method, runs a static server in the given directory
@@ -67,7 +38,7 @@ staticServer directory preServe port =
67 httpServe config $ static directory preServe
38 httpServe config $ static directory preServe
69 -- Snap server config
40 -- Snap server config
70− config = addListen (ListenHttp "0.0.0.0" port)
41+ config = setPort port
71 $ setAccessLog Nothing
42 $ setAccessLog Nothing
72 $ setErrorLog Nothing
43 $ setErrorLog Nothing
73 $ emptyConfig
44 $ emptyConfig