[cabin] Parse command-line options
This commit is contained in:
parent
63a36d8a71
commit
78235ef7cf
3 changed files with 47 additions and 9 deletions
|
|
@ -7,23 +7,55 @@ import Control.Concurrent.STM
|
||||||
import Lens.Micro
|
import Lens.Micro
|
||||||
import Lens.Micro.TH
|
import Lens.Micro.TH
|
||||||
import qualified Network.WebSockets as WS
|
import qualified Network.WebSockets as WS
|
||||||
|
import Options.Applicative
|
||||||
|
|
||||||
import Forest.Node
|
import Forest.Node
|
||||||
import Forest.Server.Branch.SharedEdit
|
import Forest.Server.Branch.SharedEdit
|
||||||
import Forest.Server.Schema
|
import Forest.Server.Schema
|
||||||
import Forest.Server.TreeApp
|
import Forest.Server.TreeApp
|
||||||
|
|
||||||
{- Websocket server stuff -}
|
{- Command line options -}
|
||||||
|
|
||||||
pingDelay :: Int
|
data ServerOptions = ServerOptions
|
||||||
pingDelay = 10
|
{ serverPingDelay :: Int
|
||||||
|
, serverHost :: String
|
||||||
|
, serverPort :: Int
|
||||||
|
}
|
||||||
|
|
||||||
pongDelay :: Int
|
parser :: Parser ServerOptions
|
||||||
pongDelay = 3 * pingDelay
|
parser = ServerOptions
|
||||||
|
<$> option auto
|
||||||
|
( long "ping-delay"
|
||||||
|
<> help "How many seconds to wait between each ping sent to the client"
|
||||||
|
<> value 10
|
||||||
|
<> showDefault
|
||||||
|
<> metavar "SECONDS"
|
||||||
|
)
|
||||||
|
<*> strOption
|
||||||
|
( short 'h'
|
||||||
|
<> long "host"
|
||||||
|
<> help "The server's host"
|
||||||
|
<> value (WS.serverHost WS.defaultServerOptions)
|
||||||
|
<> showDefault
|
||||||
|
<> metavar "HOST"
|
||||||
|
)
|
||||||
|
<*> option auto
|
||||||
|
( short 'p'
|
||||||
|
<> long "port"
|
||||||
|
<> help "The port to listen for websocket connections on"
|
||||||
|
<> value (WS.serverPort WS.defaultServerOptions)
|
||||||
|
<> showDefault
|
||||||
|
<> metavar "PORT"
|
||||||
|
)
|
||||||
|
|
||||||
options :: WS.ServerOptions
|
serverOptionsParserInfo :: ParserInfo ServerOptions
|
||||||
options = WS.defaultServerOptions
|
serverOptionsParserInfo = info (helper <*> parser) fullDesc
|
||||||
{ WS.serverRequirePong = Just pongDelay
|
|
||||||
|
wsOptions :: ServerOptions -> WS.ServerOptions
|
||||||
|
wsOptions o = WS.defaultServerOptions
|
||||||
|
{ WS.serverHost = serverHost o
|
||||||
|
, WS.serverPort = serverPort o
|
||||||
|
, WS.serverRequirePong = Just $ serverPingDelay o * 2
|
||||||
}
|
}
|
||||||
|
|
||||||
{- The actual app -}
|
{- The actual app -}
|
||||||
|
|
@ -65,6 +97,8 @@ constructor broadcastChan seg cont = do
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
|
opts <- execParser serverOptionsParserInfo
|
||||||
|
|
||||||
putStrLn "Preparing server"
|
putStrLn "Preparing server"
|
||||||
broadcastChan <- atomically newBroadcastTChan
|
broadcastChan <- atomically newBroadcastTChan
|
||||||
let onEditChange = atomically $ writeTChan broadcastChan UpdateSharedEdit
|
let onEditChange = atomically $ writeTChan broadcastChan UpdateSharedEdit
|
||||||
|
|
@ -76,4 +110,5 @@ main = do
|
||||||
}
|
}
|
||||||
|
|
||||||
putStrLn "Starting server"
|
putStrLn "Starting server"
|
||||||
WS.runServerWithOptions options $ runTreeApp pingDelay app
|
WS.runServerWithOptions (wsOptions opts) $
|
||||||
|
runTreeApp (serverPingDelay opts) app
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@ library
|
||||||
, forest-server
|
, forest-server
|
||||||
, microlens
|
, microlens
|
||||||
, microlens-th
|
, microlens-th
|
||||||
|
, optparse-applicative
|
||||||
, stm
|
, stm
|
||||||
, websockets
|
, websockets
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
|
|
@ -53,6 +54,7 @@ executable forest-cabin
|
||||||
, forest-server
|
, forest-server
|
||||||
, microlens
|
, microlens
|
||||||
, microlens-th
|
, microlens-th
|
||||||
|
, optparse-applicative
|
||||||
, stm
|
, stm
|
||||||
, websockets
|
, websockets
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ dependencies:
|
||||||
- forest-server
|
- forest-server
|
||||||
- microlens
|
- microlens
|
||||||
- microlens-th
|
- microlens-th
|
||||||
|
- optparse-applicative
|
||||||
- stm
|
- stm
|
||||||
- websockets
|
- websockets
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue