1010module Main where
1111
1212import qualified Control.Concurrent.STM as STM
13+ import Control.Exception (throwIO )
1314import Control.Lens (unto )
1415import Control.Monad.Freer (Eff , interpret , reinterpret , run , send )
1516import Control.Monad.Freer.Error (Error , runError )
@@ -18,12 +19,13 @@ import Control.Monad.Freer.Extras.Log (LogLevel (..), LogMessage
1819import Control.Monad.Freer.State (State , runState )
1920import Control.Monad.Freer.Writer (runWriter )
2021import Control.Monad.IO.Class (liftIO )
22+ import qualified Data.Aeson as A
2123import Data.Foldable (for_ , traverse_ )
2224import Data.Function ((&) )
2325import Data.Functor (void )
2426import Data.Sequence (Seq , (<|) )
2527import Data.Text.Prettyprint.Doc (Pretty (.. ))
26- import Data.Yaml ( decodeFileThrow )
28+ import qualified Data.Yaml as Y
2729import Options.Applicative (execParser )
2830import qualified Plutus.ChainIndex.Server as Server
2931
@@ -33,10 +35,9 @@ import Cardano.BM.Trace (Trace, logError)
3335
3436import Cardano.Protocol.Socket.Client (ChainSyncEvent (.. ), runChainSync )
3537import CommandLine (AppConfig (.. ), Command (.. ), applyOverrides , cmdWithHelpParser )
36- import Config (ChainIndexConfig )
37- import qualified Config as Config
38+ import qualified Config
3839import Ledger (Slot (.. ))
39- import Logging ( defaultConfig , loadConfig )
40+ import qualified Logging
4041import Plutus.ChainIndex.Compatibility (fromCardanoBlock , fromCardanoPoint , tipFromCardanoBlock )
4142import Plutus.ChainIndex.Effects (ChainIndexControlEffect (.. ), ChainIndexQueryEffect (.. ),
4243 appendBlock , rollback )
@@ -110,33 +111,42 @@ main = do
110111 -- Parse comand line arguments.
111112 cmdConfig@ AppConfig {acLogConfigPath, acConfigPath, acMinLogLevel, acCommand, acCLIConfigOverrides} <- execParser cmdWithHelpParser
112113
113- -- Initialise logging
114- logConfig <- maybe defaultConfig loadConfig acLogConfigPath
115- for_ acMinLogLevel $ \ ll -> CM. setMinSeverity logConfig ll
116- (trace :: Trace IO ChainIndexLog , _ ) <- setupTrace_ logConfig " chain-index"
114+ case acCommand of
115+ DumpDefaultConfig path ->
116+ A. encodeFile path Config. defaultConfig
117117
118- -- Reading configuration file
119- config <- case acConfigPath of
120- Nothing -> pure Config. defaultConfig
121- Just p -> decodeFileThrow @ IO @ ChainIndexConfig p
118+ DumpDefaultLoggingConfig path ->
119+ Logging. defaultConfig >>= CM. toRepresentation >>= Y. encodeFile path
122120
123- putStrLn " Command line config:"
124- print cmdConfig
121+ StartChainIndex {} -> do
122+ -- Initialise logging
123+ logConfig <- maybe Logging. defaultConfig Logging. loadConfig acLogConfigPath
124+ for_ acMinLogLevel $ \ ll -> CM. setMinSeverity logConfig ll
125+ (trace :: Trace IO ChainIndexLog , _ ) <- setupTrace_ logConfig " chain-index"
125126
126- let actualConfig = applyOverrides acCLIConfigOverrides config
127- putStrLn " Configuration:"
128- print (pretty actualConfig)
127+ -- Reading configuration file
128+ config <- applyOverrides acCLIConfigOverrides <$> case acConfigPath of
129+ Nothing -> pure Config. defaultConfig
130+ Just p -> A. eitherDecodeFileStrict p >>=
131+ either (throwIO . Config. DecodeConfigException ) pure
129132
130- appState <- STM. newTVarIO mempty
133+ putStrLn " \n Command line config:"
134+ print cmdConfig
131135
132- case acCommand of
133- StartChainIndex {} -> do
134- putStrLn $ " Connecting to the node using socket: " <> Config. cicSocketPath actualConfig
135- void $ runChainSync (Config. cicSocketPath actualConfig)
136- (Config. cicSlotConfig actualConfig)
137- (Config. cicNetworkId actualConfig)
136+ putStrLn " \n Logging config:"
137+ CM. toRepresentation logConfig >>= print
138+
139+ putStrLn " \n Chain Index config:"
140+ print (pretty config)
141+
142+ appState <- STM. newTVarIO mempty
143+
144+ putStrLn $ " Connecting to the node using socket: " <> Config. cicSocketPath config
145+ void $ runChainSync (Config. cicSocketPath config)
146+ (Config. cicSlotConfig config)
147+ (Config. cicNetworkId config)
138148 []
139149 (chainSyncHandler trace appState)
140- putStrLn $ " Starting webserver on port " <> show ( Config. cicPort actualConfig)
141- Server. serveChainIndexQueryServer (Config. cicPort actualConfig) appState
142- _ -> pure ()
150+
151+ putStrLn $ " Starting webserver on port " <> show (Config. cicPort config)
152+ Server. serveChainIndexQueryServer ( Config. cicPort config) appState
0 commit comments