summaryrefslogtreecommitdiff
path: root/fig-web/main/Main.hs
blob: 3567529f7504667b5c5368408855bbe1095df943 (plain)
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
{-# Language ApplicativeDo #-}

module Main where

import Fig.Prelude

import Options.Applicative

import Fig.Web.Utils
import qualified Fig.Web as Public
import qualified Fig.Web.Secure as Secure

data Command
  = Public
  | Secure

parseCommand :: Parser Command
parseCommand = subparser $ mconcat
  [ command "public" $ info (pure Public) (progDesc "Launch the public web server")
  , command "secure" $ info (pure Secure) (progDesc "Launch the private web server (intended to be run behind authentication proxy)")
  ]

data Opts = Opts
  { busHost :: !Text
  , busPort :: !Text
  , config :: !FilePath
  , cmd :: !Command
  }

parseOpts :: Parser Opts
parseOpts = do
  busHost <- strOption (long "bus-host" <> metavar "HOST" <> help "Address of message bus" <> value "localhost")
  busPort <- strOption (long "bus-port" <> metavar "PORT" <> help "Message bus port" <> showDefault <> value "32050")
  config <- strOption (long "config" <> metavar "PATH" <> help "Path to config file" <> showDefault <> value "fig-web.toml")
  cmd <- parseCommand
  pure Opts{..}

main :: IO ()
main = do
  opts <- execParser $ info (parseOpts <**> helper)
    ( fullDesc
    <> header "fig-web - public-facing web applications"
    )
  cfg <- loadConfig opts.config
  case opts.cmd of
    Public -> Public.server cfg (opts.busHost, opts.busPort)
    Secure -> Secure.server cfg (opts.busHost, opts.busPort)