From 4f907b3ffeae013740ae3c6eb8057cc1f9911c6d Mon Sep 17 00:00:00 2001 From: Dzejkop Date: Fri, 26 Jan 2024 15:43:06 +0100 Subject: [PATCH] Sample config --- Cargo.lock | 35 ++++++++++++++++++----------------- Cargo.toml | 3 ++- bin/mpc_node.rs | 26 ++++++++++++++++++++++---- src/config.rs | 28 ++++++++++++++++++++++++++++ src/lib.rs | 1 + 5 files changed, 71 insertions(+), 22 deletions(-) create mode 100644 src/config.rs diff --git a/Cargo.lock b/Cargo.lock index c877b19..9ea52fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1253,6 +1253,24 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "mpc" +version = "0.1.0" +dependencies = [ + "clap", + "config", + "criterion", + "dotenv", + "eyre", + "metrics", + "serde", + "sqlx", + "telemetry-batteries", + "tokio", + "tracing", + "tracing-subscriber", +] + [[package]] name = "native-tls" version = "0.2.11" @@ -2060,23 +2078,6 @@ dependencies = [ "serde", ] -[[package]] -name = "service-rs" -version = "0.1.0" -dependencies = [ - "clap", - "config", - "criterion", - "dotenv", - "eyre", - "metrics", - "sqlx", - "telemetry-batteries", - "tokio", - "tracing", - "tracing-subscriber", -] - [[package]] name = "sha1" version = "0.10.6" diff --git a/Cargo.toml b/Cargo.toml index 39bd32f..5bfe584 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "service-rs" +name = "mpc" version = "0.1.0" edition = "2021" @@ -20,6 +20,7 @@ tokio = { version = "1.35.1", features = ["macros"] } tracing = "0.1.40" tracing-subscriber = "0.3.18" metrics = "0.21.1" +serde = { version = "1.0.195", features = ["derive"] } [[bin]] name = "mpc-node" diff --git a/bin/mpc_node.rs b/bin/mpc_node.rs index e3bf283..49cdf45 100644 --- a/bin/mpc_node.rs +++ b/bin/mpc_node.rs @@ -1,4 +1,7 @@ +use std::path::PathBuf; + use clap::Parser; +use mpc::config::Config; use telemetry_batteries::metrics::batteries::StatsdBattery; use telemetry_batteries::tracing::batteries::DatadogBattery; use tracing_subscriber::layer::SubscriberExt; @@ -17,6 +20,9 @@ pub const METRICS_PREFIX: &str = "mpc-node"; pub struct Args { #[clap(short, long, env)] local: bool, + + #[clap(short, long, env)] + config: Option, } #[tokio::main] @@ -44,10 +50,22 @@ async fn main() -> eyre::Result<()> { Some(METRICS_PREFIX), )?; + let mut settings = config::Config::builder(); + + if let Some(path) = args.config { + settings = settings.add_source(config::File::from(path).required(true)); + } + + let settings = settings + .add_source(config::Environment::with_prefix("MPC").separator("__")) + .build()?; + + let config = settings.try_deserialize::()?; + let mut n = 0; loop { - foo(n).await; + foo(&config, n).await; n += 1; @@ -55,9 +73,9 @@ async fn main() -> eyre::Result<()> { } } -#[tracing::instrument] -async fn foo(n: usize) { - tracing::info!(n, "Foo"); +#[tracing::instrument(skip(config))] +async fn foo(config: &Config, n: usize) { + tracing::info!(n, test = config.test.test, "Foo"); metrics::gauge!("foo", n as f64); } diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..7f68c27 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,28 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Config { + #[serde(default)] + pub test: TestConfig, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct TestConfig { + #[serde(default = "default::test")] + pub test: String, +} + +impl Default for TestConfig { + fn default() -> Self { + Self { + test: default::test(), + } + } + +} + +pub mod default { + pub fn test() -> String { + "default".to_string() + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index e69de29..ef68c36 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod config;