on master help sign in
Merge pull request #234 from chrisdotcode/master

Add flag to bind on selected host, instead of default "0.0.0.0"

commit81a970c2930ad90308fb690c2bb71c7ef8ca6167 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateTue 25 Mar 2014, 11:11 (13 years ago) parent677a7f0c922ab5
5 files changed+36−21
.gitignore+2 src/Hakyll/Commands.hs+9−11 src/Hakyll/Core/Configuration.hs+6 src/Hakyll/Main.hs+14−8 src/Hakyll/Preview/Server.hs+5−2
.gitignore+2
@@ -8,6 +8,8 @@
88TAGS
99dist
1010tags
11+cabal.sandbox.config
12+.cabal-sandbox/
1113
1214# Ignore test builds.
1315tests/Main
src/Hakyll/Commands.hs+9−11
@@ -15,9 +15,7 @@ module Hakyll.Commands
1515
1616--------------------------------------------------------------------------------
1717import System.Exit (exitWith, ExitCode)
18−import System.IO.Error (catchIOError)
1918import Control.Applicative
20−import Control.Monad (void)
2119import Control.Concurrent
2220
2321--------------------------------------------------------------------------------
@@ -69,7 +67,7 @@ preview :: Configuration -> Verbosity -> Rules a -> Int -> IO ()
6967#ifdef PREVIEW_SERVER
7068preview conf verbosity rules port = do
7169 deprecatedMessage
72− watch conf verbosity port True rules
70+ watch conf verbosity "0.0.0.0" port True rules
7371 where
7472 deprecatedMessage = mapM_ putStrLn [ "The preview command has been deprecated."
7573 , "Use the watch command for recompilation and serving."
@@ -82,9 +80,9 @@ preview _ _ _ _ = previewServerDisabled
8280--------------------------------------------------------------------------------
8381-- | Watch and recompile for changes
8482
85−watch :: Configuration -> Verbosity -> Int -> Bool -> Rules a -> IO ()
83+watch :: Configuration -> Verbosity -> String -> Int -> Bool -> Rules a -> IO ()
8684#ifdef WATCH_SERVER
87−watch conf verbosity port runServer rules = do
85+watch conf verbosity host port runServer rules = do
8886#ifndef mingw32_HOST_OS
8987 _ <- forkIO $ watchUpdates conf update
9088#else
@@ -100,9 +98,9 @@ watch conf verbosity port runServer rules = do
10098 (_, ruleSet) <- run conf verbosity rules
10199 return $ rulesPattern ruleSet
102100 loop = threadDelay 100000 >> loop
103− server' = if runServer then server conf port else loop
101+ server' = if runServer then server conf host port else loop
104102#else
105−watch _ _ _ _ _ = watchServerDisabled
103+watch _ _ _ _ = watchServerDisabled
106104#endif
107105
108106--------------------------------------------------------------------------------
@@ -113,15 +111,15 @@ rebuild conf verbosity rules =
113111
114112--------------------------------------------------------------------------------
115113-- | Start a server
116−server :: Configuration -> Int -> IO ()
114+server :: Configuration -> String -> Int -> IO ()
117115#ifdef PREVIEW_SERVER
118−server conf port = do
116+server conf host port = do
119117 let destination = destinationDirectory conf
120− staticServer destination preServeHook port
118+ staticServer destination preServeHook host port
121119 where
122120 preServeHook _ = return ()
123121#else
124−server _ _ = previewServerDisabled
122+server _ _ _ = previewServerDisabled
125123#endif
126124
127125
src/Hakyll/Core/Configuration.hs+6
@@ -69,6 +69,11 @@ data Configuration = Configuration
6969 , -- | Use an in-memory cache for items. This is faster but uses more
7070 -- memory.
7171 inMemoryCache :: Bool
72+ , -- | Override default host for preview server. Default is "127.0.0.1",
73+ -- which binds only on the loopback address.
74+ -- One can also override the host as a command line argument:
75+ -- ./site preview -h "0.0.0.0"
76+ previewHost :: String
7277 , -- | Override default port for preview server. Default is 8000.
7378 -- One can also override the port as a command line argument:
7479 -- ./site preview -p 1234
@@ -91,6 +96,7 @@ defaultConfiguration = Configuration
9196 , deployCommand = "echo 'No deploy command specified' && exit 1"
9297 , deploySite = system . deployCommand
9398 , inMemoryCache = True
99+ , previewHost = "127.0.0.1"
94100 , previewPort = 8000
95101 }
96102 where
src/Hakyll/Main.hs+14−8
@@ -48,8 +48,8 @@ hakyllWith conf rules = do
4848 Help _ -> showHelp
4949 Preview _ p -> Commands.preview conf verbosity' rules p
5050 Rebuild _ -> Commands.rebuild conf verbosity' rules >>= exitWith
51− Server _ _ -> Commands.server conf (port args')
52− Watch _ p s -> Commands.watch conf verbosity' p (not s) rules
51+ Server _ _ _ -> Commands.server conf (host args') (port args')
52+ Watch _ _ p s -> Commands.watch conf verbosity' (host args') p (not s) rules
5353
5454
5555--------------------------------------------------------------------------------
@@ -67,8 +67,8 @@ data HakyllArgs
6767 | Help {verbose :: Bool}
6868 | Preview {verbose :: Bool, port :: Int}
6969 | Rebuild {verbose :: Bool}
70− | Server {verbose :: Bool, port :: Int}
71− | Watch {verbose :: Bool, port :: Int, no_server :: Bool }
70+ | Server {verbose :: Bool, host :: String, port :: Int}
71+ | Watch {verbose :: Bool, host :: String, port :: Int, no_server :: Bool }
7272 deriving (Data, Typeable, Show)
7373
7474
@@ -84,13 +84,14 @@ hakyllArgs conf = modes
8484 , (Preview (verboseFlag def) (portFlag defaultPort)) &=
8585 help "[Deprecated] Please use the watch command"
8686 , (Rebuild $ verboseFlag def) &= help "Clean and build again"
87− , (Server (verboseFlag def) (portFlag defaultPort)) &=
87+ , (Server (verboseFlag def) (hostFlag defaultHost) (portFlag defaultPort)) &=
8888 help "Start a preview server"
89− , (Watch (verboseFlag def) (portFlag defaultPort) (noServerFlag False) &=
89+ , (Watch (verboseFlag def) (hostFlag defaultHost) (portFlag defaultPort) (noServerFlag False) &=
9090 help "Autocompile on changes and start a preview server. You can watch and recompile without running a server with --no-server.")
9191 ] &= help "Hakyll static site compiler" &= program progName
92− where defaultPort = Config.previewPort conf
93−
92+ where
93+ defaultHost = Config.previewHost conf
94+ defaultPort = Config.previewPort conf
9495
9596--------------------------------------------------------------------------------
9697verboseFlag :: Data a => a -> a
@@ -103,6 +104,11 @@ noServerFlag :: Data a => a -> a
103104noServerFlag x = x &= help "Disable the built-in web server"
104105{-# INLINE noServerFlag #-}
105106
107+--------------------------------------------------------------------------------
108+hostFlag :: Data a => a -> a
109+hostFlag x = x &= help "Host to bind on"
110+{-# INLINE hostFlag #-}
111+
106112--------------------------------------------------------------------------------
107113portFlag :: Data a => a -> a
108114portFlag x = x &= help "Port to listen on"
src/Hakyll/Preview/Server.hs+5−2
@@ -8,6 +8,7 @@ module Hakyll.Preview.Server
88
99--------------------------------------------------------------------------------
1010import Control.Monad.Trans (liftIO)
11+import qualified Data.ByteString.Char8 as B
1112import qualified Snap.Core as Snap
1213import qualified Snap.Http.Server as Snap
1314import qualified Snap.Util.FileServe as Snap
@@ -31,13 +32,15 @@ static directory preServe =
3132-- | Main method, runs a static server in the given directory
3233staticServer :: FilePath -- ^ Directory to serve
3334 -> (FilePath -> IO ()) -- ^ Pre-serve hook
35+ -> String -- ^ Host to bind on
3436 -> Int -- ^ Port to listen on
3537 -> IO () -- ^ Blocks forever
36−staticServer directory preServe port =
38+staticServer directory preServe host port =
3739 Snap.httpServe config $ static directory preServe
3840 where
3941 -- Snap server config
40− config = Snap.setPort port
42+ config = Snap.setBind (B.pack host)
43+ $ Snap.setPort port
4144 $ Snap.setAccessLog Snap.ConfigNoLog
4245 $ Snap.setErrorLog Snap.ConfigNoLog
4346 $ Snap.emptyConfig