content: {-# LANGUAGE NamedFieldPuns #-}
  {-# LANGUAGE OverloadedStrings #-}
  
  module Cardano.TxSubmit
    ( runTxSubmitWebapi
    , opts
    , TxSubmitCommand(..)
    ) where
  
  import qualified Cardano.BM.Setup as Logging
  import           Cardano.BM.Trace (Trace, logInfo)
  import qualified Cardano.BM.Trace as Logging
  import           Cardano.TxSubmit.CLI.Parsers (opts)
  import           Cardano.TxSubmit.CLI.Types (ConfigFile (unConfigFile), TxSubmitCommand (..),
                     TxSubmitNodeParams (..))
  import           Cardano.TxSubmit.Config (GenTxSubmitNodeConfig (..), ToggleLogging (..),
                     TxSubmitNodeConfig, readTxSubmitNodeConfig)
  import           Cardano.TxSubmit.Metrics (registerMetricsServer)
  import           Cardano.TxSubmit.Web (runTxSubmitServer)
  
  import qualified Control.Concurrent.Async as Async
  import           Control.Monad.IO.Class (MonadIO (liftIO))
  import           Data.Text (Text)
  
  runTxSubmitWebapi :: TxSubmitNodeParams -> IO ()
  runTxSubmitWebapi tsnp = do
      tsnc <- readTxSubmitNodeConfig (unConfigFile tspConfigFile)
      trce <- mkTracer tsnc
      (metrics, runMetricsServer) <- registerMetricsServer trce tspMetricsPort
      Async.withAsync
        (runTxSubmitServer trce metrics tspWebserverConfig tspProtocol tspNetworkId tspSocketPath)
        $ \txSubmitServer ->
          Async.withAsync runMetricsServer $ \_ ->
            Async.wait txSubmitServer
      logInfo trce "runTxSubmitWebapi: Stopping TxSubmit API"
    where
      TxSubmitNodeParams
        { tspProtocol
        , tspNetworkId
        , tspSocketPath
        , tspWebserverConfig
        , tspMetricsPort
        , tspConfigFile
        } = tsnp
  
  mkTracer :: TxSubmitNodeConfig -> IO (Trace IO Text)
  mkTracer enc = case tscToggleLogging enc of
    LoggingOn -> liftIO $ Logging.setupTrace (Right $ tscLoggingConfig enc) "cardano-tx-submit"
    LoggingOff -> pure Logging.nullTracer
  