mnemonic-hash-checker: pass config through cli args or env variables

This commit is contained in:
Ryan Heywood 2023-08-05 21:48:39 -05:00
parent 0625c2672a
commit a7f798702d
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
2 changed files with 38 additions and 8 deletions

View File

@ -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"] }

View File

@ -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<SocketAddr>,
#[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))