summaryrefslogtreecommitdiff
path: root/fig-web/main/Main.hs
blob: 7e3d509abc34c474ee3b048cec9dddadc1060005 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
{-# Language ApplicativeDo #-}

module Main where

import Fig.Prelude

import Options.Applicative

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

parsePublicOptions :: Parser PublicOptions
parsePublicOptions = do
  pure PublicOptions{}

parseSecureOptions :: Parser SecureOptions
parseSecureOptions = do
  simAuth <- switch (long "sim-auth" <> help "Simulate authentication instead of actually requiring authentication proxy headers")
  pure SecureOptions{..}

data Command
  = Public PublicOptions
  | Secure SecureOptions
  | TestFFI

parseCommand :: Parser Command
parseCommand = hsubparser $ mconcat
  [ command "public" $ info (Public <$> parsePublicOptions) (progDesc "Launch the public web server")
  , command "secure" $ info (Secure <$> parseSecureOptions) (progDesc "Launch the private web server (intended to be run behind authentication proxy)")
  , command "testffi" $ info (pure TestFFI) (progDesc "Test the FFI")
  ]

data Opts = Opts
  { busHost :: !Text
  , busPort :: !Text
  , port :: !(Maybe Int)
  , 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")
  port <- optional $ option auto (long "port" <> metavar "PORT" <> help "Web server port")
  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
    <> Options.Applicative.header "fig-web - web backends"
    )
  icfg <- loadConfig opts.config
  let cfg = case opts.port of
        Nothing -> icfg
        Just p -> icfg { Fig.Web.Utils.port = p }
  case opts.cmd of
    Public o -> Public.server o cfg (opts.busHost, opts.busPort)
    Secure o -> Secure.server o cfg (opts.busHost, opts.busPort)
    TestFFI -> do
      log "testing FFI"
      res <- FFI.checkAnswer "(lambda (d e) (equal? d e))" "hello computer" "hello computer"
      log $ "result: " <> tshow res
      inpa <- FFI.genInputAnswer "(lambda (x) (cons x x))" "hello computer"
      log $ "input/answer: " <> tshow inpa