Skip to content

Commit f7baf4d

Browse files
kk-hainqj-mueller
authored andcommitted
Further distinguish chain index & logging configs. Support print default configs to file.
1 parent 31e2276 commit f7baf4d

File tree

3 files changed

+62
-39
lines changed

3 files changed

+62
-39
lines changed

plutus-chain-index/app/CommandLine.hs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ applyOverrides CLIConfigOverrides{ccSocketPath, ccPort, ccNetworkId} =
3636

3737
-- | Configuration
3838
data Command =
39-
DumpDefaultConfig -- ^ Write default logging configuration
40-
{ outputFile :: !FilePath
41-
}
42-
| StartChainIndex -- ^ Start the chain index and connect it to a cardano node
39+
DumpDefaultConfig { dumpConfigPath :: !FilePath }
40+
-- ^ Write default chain index configuration
41+
| DumpDefaultLoggingConfig { dumpLoggingConfigPath :: !FilePath }
42+
-- ^ Write default logging configuration
43+
| StartChainIndex
44+
-- ^ Start the chain index and connect it to a cardano node
4345
deriving (Show)
4446

4547
data AppConfig = AppConfig
@@ -86,7 +88,7 @@ configParser =
8688
<> metavar "CONFIG"
8789
<> value Nothing
8890
<> short 'c'
89-
<> help "Path to the configuration file." )
91+
<> help "Path to the chain index configuration file." )
9092

9193
debuggingOutputParser :: Parser (Maybe Severity)
9294
debuggingOutputParser =
@@ -108,22 +110,28 @@ commandParser =
108110
subparser $
109111
mconcat
110112
[ dumpDefaultConfigParser
113+
, dumpDefaultLoggingConfigParser
111114
, startChainIndexParser
112115
]
113116

114117
dumpDefaultConfigParser :: Mod CommandFields Command
115118
dumpDefaultConfigParser =
116-
command "default-loggging-config" $
117-
flip info (fullDesc <> progDesc "Write the default logging configuration YAML to a file") $ do
118-
outputFile' <-
119+
command "default-config" $
120+
flip info (fullDesc <> progDesc "Write the default chain index JSON configuration to a file") $
121+
DumpDefaultConfig <$>
119122
argument str
120-
( metavar "OUTPUT_FILE"
121-
<> help "Output file to write logging config TAML to." )
122-
pure $ DumpDefaultConfig { outputFile = outputFile' }
123+
(metavar "OUTPUT_FILE" <> help "Output JSON file to write chain index config to.")
124+
125+
dumpDefaultLoggingConfigParser :: Mod CommandFields Command
126+
dumpDefaultLoggingConfigParser =
127+
command "default-logging-config" $
128+
flip info (fullDesc <> progDesc "Write the default logging YAML configuration to a file") $
129+
DumpDefaultLoggingConfig <$>
130+
argument str
131+
(metavar "OUTPUT_FILE" <> help "Output YAML file to write logging config to.")
123132

124133
startChainIndexParser :: Mod CommandFields Command
125134
startChainIndexParser =
126135
command "start-index" $
127136
flip info (fullDesc <> progDesc "Start the chain index and connect it to a cardano node") $ do
128137
pure StartChainIndex
129-

plutus-chain-index/app/Config.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
module Config(
1111
ChainIndexConfig(..),
12+
DecodeConfigException(..),
1213
defaultConfig,
1314
-- * Lenses
1415
socketPath,
@@ -18,6 +19,7 @@ module Config(
1819
) where
1920

2021
import Cardano.Api (NetworkId (..))
22+
import Control.Exception (Exception)
2123
import Control.Lens (makeLensesFor)
2224
import Data.Aeson (FromJSON, ToJSON)
2325
import Data.Text.Prettyprint.Doc (Pretty (..), viaShow, vsep, (<+>))
@@ -68,3 +70,6 @@ makeLensesFor [
6870
("cicNetworkId", "networkId"),
6971
("cicSlotConfig", "slotConfig")
7072
] 'ChainIndexConfig
73+
74+
newtype DecodeConfigException = DecodeConfigException String
75+
deriving (Show, Exception)

plutus-chain-index/app/Main.hs

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
module Main where
1111

1212
import qualified Control.Concurrent.STM as STM
13+
import Control.Exception (throwIO)
1314
import Control.Lens (unto)
1415
import Control.Monad.Freer (Eff, interpret, reinterpret, run, send)
1516
import Control.Monad.Freer.Error (Error, runError)
@@ -18,12 +19,13 @@ import Control.Monad.Freer.Extras.Log (LogLevel (..), LogMessage
1819
import Control.Monad.Freer.State (State, runState)
1920
import Control.Monad.Freer.Writer (runWriter)
2021
import Control.Monad.IO.Class (liftIO)
22+
import qualified Data.Aeson as A
2123
import Data.Foldable (for_, traverse_)
2224
import Data.Function ((&))
2325
import Data.Functor (void)
2426
import Data.Sequence (Seq, (<|))
2527
import Data.Text.Prettyprint.Doc (Pretty (..))
26-
import Data.Yaml (decodeFileThrow)
28+
import qualified Data.Yaml as Y
2729
import Options.Applicative (execParser)
2830
import qualified Plutus.ChainIndex.Server as Server
2931

@@ -33,10 +35,9 @@ import Cardano.BM.Trace (Trace, logError)
3335

3436
import Cardano.Protocol.Socket.Client (ChainSyncEvent (..), runChainSync)
3537
import CommandLine (AppConfig (..), Command (..), applyOverrides, cmdWithHelpParser)
36-
import Config (ChainIndexConfig)
37-
import qualified Config as Config
38+
import qualified Config
3839
import Ledger (Slot (..))
39-
import Logging (defaultConfig, loadConfig)
40+
import qualified Logging
4041
import Plutus.ChainIndex.Compatibility (fromCardanoBlock, fromCardanoPoint, tipFromCardanoBlock)
4142
import 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 "\nCommand 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 "\nLogging config:"
137+
CM.toRepresentation logConfig >>= print
138+
139+
putStrLn "\nChain 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

Comments
 (0)