From 1cb08214cf3a82ad3acb8fec023e7fb2a404f6bd Mon Sep 17 00:00:00 2001 From: msi Date: Fri, 14 Nov 2025 10:31:30 -0300 Subject: Add config files for project --- web/template/src/main.rs | 3 ++ web/template/src/settings.rs | 80 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 web/template/src/settings.rs (limited to 'web/template/src') diff --git a/web/template/src/main.rs b/web/template/src/main.rs index 315a365..f9c6340 100644 --- a/web/template/src/main.rs +++ b/web/template/src/main.rs @@ -24,11 +24,14 @@ mod helpers; mod metric; mod router; mod state; +mod settings; #[tokio::main] async fn main() -> anyhow::Result<()> { helpers::init_tracing(); + let _settings = settings::Settings::new(); + let (_main_server, _metrics_server) = tokio::join!(start_main_server(), metric::start_metrics_server()); Ok(()) diff --git a/web/template/src/settings.rs b/web/template/src/settings.rs new file mode 100644 index 0000000..897cbf4 --- /dev/null +++ b/web/template/src/settings.rs @@ -0,0 +1,80 @@ +use std::env; + +use tracing::{debug, info}; + +use config::{Config, ConfigError, Environment, File}; +use serde::Deserialize; + +#[derive(Debug, Deserialize)] +#[allow(unused)] +struct Database { + url: String, +} + +#[derive(Debug, Deserialize)] +#[allow(unused)] +struct Sparkpost { + key: String, + token: String, + url: String, + version: u8, +} + +#[derive(Debug, Deserialize)] +#[allow(unused)] +struct Twitter { + consumer_token: String, + consumer_secret: String, +} + +#[derive(Debug, Deserialize)] +#[allow(unused)] +struct Braintree { + merchant_id: String, + public_key: String, + private_key: String, +} + +#[derive(Debug, Deserialize)] +#[allow(unused)] +pub(crate) struct Settings { + debug: bool, + database: Database, + sparkpost: Sparkpost, + twitter: Twitter, + braintree: Braintree, +} + +impl Settings { + pub(crate) fn new() -> Result { + info!("loading settings"); + let run_mode = env::var("RUN_MODE").unwrap_or_else(|_| "development".into()); + + let s = Config::builder() + // Start off by merging in the "default" configuration file + .add_source(File::with_name("config/default")) + // Add in the current environment file + // Default to 'development' env + // Note that this file is _optional_ + .add_source( + File::with_name(&format!("config/{run_mode}")) + .required(false), + ) + // Add in a local configuration file + // This file shouldn't be checked in to git + .add_source(File::with_name("config/local").required(false)) + // Add in settings from the environment (with a prefix of APP) + // Eg.. `APP_DEBUG=1 ./target/app` would set the `debug` key + .add_source(Environment::with_prefix("app")) + // You may also programmatically change settings + .set_override("database.url", "postgres://")? + .build()?; + + // Now that we're done, let's access our configuration + debug!("debug: {:?}", s.get_bool("debug")); + debug!("database: {:?}", s.get::("database.url")); + + // You can deserialize (and thus freeze) the entire configuration as + s.try_deserialize() + } +} -- cgit v1.2.3