From a7f798702d724e615b26ee70efc6468097683903 Mon Sep 17 00:00:00 2001 From: RyanSquared Date: Sat, 5 Aug 2023 21:48:39 -0500 Subject: [PATCH] mnemonic-hash-checker: pass config through cli args or env variables --- mnemonic-hash-checker/Cargo.toml | 2 +- mnemonic-hash-checker/src/main.rs | 44 ++++++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/mnemonic-hash-checker/Cargo.toml b/mnemonic-hash-checker/Cargo.toml index 7d3bf3c..b94b01f 100644 --- a/mnemonic-hash-checker/Cargo.toml +++ b/mnemonic-hash-checker/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" [dependencies] axum = { version = "0.6.20", features = ["headers", "macros", "tracing"] } -clap = { version = "4.3.19", features = ["derive"] } +clap = { version = "4.3.19", features = ["derive", "env"] } color-eyre = "0.6.2" deadpool-postgres = "0.10.5" serde = { version = "1.0.181", features = ["serde_derive"] } diff --git a/mnemonic-hash-checker/src/main.rs b/mnemonic-hash-checker/src/main.rs index dbff0ed..6afd33a 100644 --- a/mnemonic-hash-checker/src/main.rs +++ b/mnemonic-hash-checker/src/main.rs @@ -1,10 +1,16 @@ use std::net::SocketAddr; use std::sync::Arc; -use axum::{extract::{Path, State}, routing::get, Json, Router}; +use axum::{ + extract::{Path, State}, + routing::get, + Json, Router, +}; use color_eyre::eyre::Result; +use clap::Parser; + use deadpool_postgres::{Config, ManagerConfig, Pool, RecyclingMethod, Runtime}; use tokio_postgres::NoTls; @@ -17,6 +23,27 @@ use tracing_subscriber::prelude::*; use tracing::{debug, info}; +#[derive(Parser)] +struct CliConfig { + #[clap(long, env)] + bind_address: Option, + + #[clap(long, env, default_value = "postgres")] + postgres_dbname: String, + + #[clap(long, env, default_value = "localhost")] + postgres_host: String, + + #[clap(long, env)] + postgres_password: String, + + #[clap(long, env, default_value = "5432")] + postgres_port: u16, + + #[clap(long, env, default_value = "postgres")] + postgres_user: String, +} + fn setup_registry() { let envfilter = EnvFilter::builder() .with_default_directive(LevelFilter::DEBUG.into()) @@ -61,17 +88,20 @@ async fn main() -> Result<()> { setup_registry(); color_eyre::install()?; - let addr: SocketAddr = "0.0.0.0:8000".parse()?; + let cli_config = CliConfig::parse(); let mut config = Config::new(); - config.dbname = Some("postgres".to_string()); - config.host = Some("localhost".to_string()); - config.password = Some("sandwich".to_string()); - config.port = Some(5432); - config.user = Some("postgres".to_string()); + config.dbname = Some(cli_config.postgres_dbname); + config.host = Some(cli_config.postgres_host); + config.password = Some(cli_config.postgres_password); + config.port = Some(cli_config.postgres_port); + config.user = Some(cli_config.postgres_user); config.manager = Some(ManagerConfig { recycling_method: RecyclingMethod::Fast, }); let pool = config.create_pool(Some(Runtime::Tokio1), NoTls)?; + let addr = cli_config.bind_address.unwrap_or_else(|| { + (std::net::Ipv4Addr::new(127, 0, 0, 1), 8000).into() + }); let app = Router::new() .route("/check/:hash", get(check_hash_slug))