diff options
| author | LLLL Colonq <llll@colonq> | 2023-11-16 19:06:43 -0500 |
|---|---|---|
| committer | LLLL Colonq <llll@colonq> | 2023-11-16 19:06:43 -0500 |
| commit | dcef0b65069fb38fd0f6c4382353167f603ebff1 (patch) | |
| tree | 45954ffe308c3dd056e6af4f734e6d2af89e5856 /fig-monitor-bullfrog | |
Initial commit
Diffstat (limited to 'fig-monitor-bullfrog')
| -rw-r--r-- | fig-monitor-bullfrog/fig-monitor-bullfrog.cabal | 55 | ||||
| -rw-r--r-- | fig-monitor-bullfrog/main/Main.hs | 29 | ||||
| -rw-r--r-- | fig-monitor-bullfrog/src/Fig/Monitor/Bullfrog.hs | 36 | ||||
| -rw-r--r-- | fig-monitor-bullfrog/src/Fig/Monitor/Bullfrog/Utils.hs | 29 |
4 files changed, 149 insertions, 0 deletions
diff --git a/fig-monitor-bullfrog/fig-monitor-bullfrog.cabal b/fig-monitor-bullfrog/fig-monitor-bullfrog.cabal new file mode 100644 index 0000000..2d06268 --- /dev/null +++ b/fig-monitor-bullfrog/fig-monitor-bullfrog.cabal @@ -0,0 +1,55 @@ +cabal-version: 3.4 +name: fig-monitor-bullfrog +version: 0.1.0.0 + +common defaults + ghc-options: -Wall + default-language: GHC2021 + default-extensions: NoImplicitPrelude PackageImports LambdaCase MultiWayIf OverloadedStrings OverloadedLists OverloadedRecordDot DuplicateRecordFields RecordWildCards NoFieldSelectors BlockArguments ViewPatterns TypeFamilies DataKinds GADTs + +common deps + build-depends: + base + , aeson + , base64 + , binary + , bytestring + , containers + , data-default-class + , directory + , filepath + , http-types + , http-client + , http-client-tls + , megaparsec + , mtl + , network + , safe-exceptions + , scotty + , text + , time + , tomland + , transformers + , unordered-containers + , vector + , warp + , websockets + , wuss + , fig-utils + , fig-bus + +library + import: defaults + import: deps + hs-source-dirs: src + exposed-modules: + Fig.Monitor.Bullfrog + Fig.Monitor.Bullfrog.Utils + +executable fig-monitor-bullfrog + import: defaults + import: deps + build-depends: fig-monitor-bullfrog, optparse-applicative + hs-source-dirs: + main + main-is: Main.hs
\ No newline at end of file diff --git a/fig-monitor-bullfrog/main/Main.hs b/fig-monitor-bullfrog/main/Main.hs new file mode 100644 index 0000000..966e0a1 --- /dev/null +++ b/fig-monitor-bullfrog/main/Main.hs @@ -0,0 +1,29 @@ +module Main where + +import Fig.Prelude + +import Options.Applicative + +import Fig.Monitor.Bullfrog +import Fig.Monitor.Bullfrog.Utils + +data Opts = Opts + { busHost :: Text + , busPort :: Text + , config :: FilePath + } + +parseOpts :: Parser Opts +parseOpts = Opts + <$> strOption (long "bus-host" <> metavar "HOST" <> help "Address of message bus" <> value "localhost") + <*> strOption (long "bus-port" <> metavar "PORT" <> help "Message bus port" <> showDefault <> value "32050") + <*> strOption (long "config" <> metavar "PATH" <> help "Path to config file" <> showDefault <> value "fig-monitor-bullfrog.toml") + +main :: IO () +main = do + opts <- execParser $ info (parseOpts <**> helper) + ( fullDesc + <> header "fig-monitor-bullfrog - monitor Bullfrog broadcast server" + ) + cfg <- loadConfig opts.config + bullfrogClient cfg (opts.busHost, opts.busPort) diff --git a/fig-monitor-bullfrog/src/Fig/Monitor/Bullfrog.hs b/fig-monitor-bullfrog/src/Fig/Monitor/Bullfrog.hs new file mode 100644 index 0000000..1a267f8 --- /dev/null +++ b/fig-monitor-bullfrog/src/Fig/Monitor/Bullfrog.hs @@ -0,0 +1,36 @@ +{-# Language QuasiQuotes #-} +{-# Language RecordWildCards #-} +{-# Language ApplicativeDo #-} + +module Fig.Monitor.Bullfrog + ( bullfrogClient + ) where + +import Fig.Prelude + +import qualified Data.Text as Text + +import qualified Wuss as WS +import qualified Network.WebSockets.Connection as WS + +import Fig.Utils.SExpr +import Fig.Bus.Client +import Fig.Monitor.Bullfrog.Utils + +bullfrogClient :: Config -> (Text, Text) -> IO () +bullfrogClient cfg busAddr = do + WS.runSecureClient "colonq.computer" 443 ("/bullfrog/api/channel/broadcast?token=" <> Text.unpack cfg.authToken) \conn -> do + busClient busAddr + (\cmds -> do + log "Connected to bus and broadcast server" + cmds.subscribe [sexp|(monitor bullfrog broadcast)|] + ) + (\_cmds d -> do + case d of + SExprList [ev, SExprString msg] + | ev == [sexp|(monitor bullfrog broadcast)|] -> do + log $ "Broadcasting message: " <> msg + WS.sendTextData conn msg + _ -> log $ "Invalid incoming message: " <> tshow d + ) + (pure ()) diff --git a/fig-monitor-bullfrog/src/Fig/Monitor/Bullfrog/Utils.hs b/fig-monitor-bullfrog/src/Fig/Monitor/Bullfrog/Utils.hs new file mode 100644 index 0000000..b0ae02b --- /dev/null +++ b/fig-monitor-bullfrog/src/Fig/Monitor/Bullfrog/Utils.hs @@ -0,0 +1,29 @@ +{-# Language ApplicativeDo #-} + +module Fig.Monitor.Bullfrog.Utils + ( FigMonitorBullfrogException(..) + , Config(..) + , loadConfig + ) where + +import Fig.Prelude + +import qualified Toml + +newtype FigMonitorBullfrogException = FigMonitorBullfrogException Text + deriving (Show, Eq, Ord) +instance Exception FigMonitorBullfrogException + +newtype Config = Config + { authToken :: Text + } deriving (Show, Eq, Ord) + +configCodec :: Toml.TomlCodec Config +configCodec = do + authToken <- Toml.text "auth_token" Toml..= (\a -> a.authToken) + pure $ Config{..} + +loadConfig :: FilePath -> IO Config +loadConfig path = Toml.decodeFileEither configCodec path >>= \case + Left err -> throwM . FigMonitorBullfrogException $ tshow err + Right config -> pure config |
