aboutsummaryrefslogtreecommitdiffstats
path: root/web/template
diff options
context:
space:
mode:
authormsi2025-11-14 10:31:30 -0300
committermsi2025-11-14 10:31:30 -0300
commit1cb08214cf3a82ad3acb8fec023e7fb2a404f6bd (patch)
tree6f3b5a558fdbca46946923c8413830ed64b0a4a3 /web/template
parent7583df586ed40b9202e7585fa5fd795ab2e3315b (diff)
downloadtemplates-1cb08214cf3a82ad3acb8fec023e7fb2a404f6bd.tar.gz
Add config files for project
Diffstat (limited to 'web/template')
-rw-r--r--web/template/.gitignore1
-rw-r--r--web/template/Cargo.toml6
-rw-r--r--web/template/config/default.toml17
-rw-r--r--web/template/config/development.toml4
-rw-r--r--web/template/src/main.rs3
-rw-r--r--web/template/src/settings.rs80
6 files changed, 108 insertions, 3 deletions
diff --git a/web/template/.gitignore b/web/template/.gitignore
index 2f7896d..ce8c5ca 100644
--- a/web/template/.gitignore
+++ b/web/template/.gitignore
@@ -1 +1,2 @@
target/
+config/production.toml
diff --git a/web/template/Cargo.toml b/web/template/Cargo.toml
index bfb4153..207eafb 100644
--- a/web/template/Cargo.toml
+++ b/web/template/Cargo.toml
@@ -9,8 +9,9 @@ edition = "2024"
[dependencies]
anyhow = "=1.0.100"
axum = { version = "=0.8.6", features = ["macros"] }
-axum-messages = "0.8.0"
-axum_csrf = { version = "0.11.0", features = ["layer"] }
+axum-messages = "=0.8.0"
+axum_csrf = { version = "=0.11.0", features = ["layer"] }
+config = { version = "=0.15.19", default-features = false, features = ["toml"] }
metrics = { version = "=0.24.2", default-features = false }
metrics-exporter-prometheus = { version = "=0.17.2", default-features = false }
minijinja = "=2.12.0"
@@ -21,4 +22,3 @@ tower-http = { version = "=0.6.6", features = ["timeout", "trace", "fs", "reques
tower-sessions = "=0.14.0"
tracing = "=0.1.41"
tracing-subscriber = { version = "=0.3.20", features = ["env-filter"] }
-
diff --git a/web/template/config/default.toml b/web/template/config/default.toml
new file mode 100644
index 0000000..dbb2f30
--- /dev/null
+++ b/web/template/config/default.toml
@@ -0,0 +1,17 @@
+[database]
+url = "postgres://postgres@localhost"
+
+[sparkpost]
+key = "sparkpost-dev-key"
+token = "sparkpost-dev-token"
+url = "https://api.sparkpost.com"
+version = 1
+
+[twitter]
+consumer_token = "twitter-dev-consumer-key"
+consumer_secret = "twitter-dev-consumer-secret"
+
+[braintree]
+merchant_id = "braintree-merchant-id"
+public_key = "braintree-dev-public-key"
+private_key = "braintree-dev-private-key"
diff --git a/web/template/config/development.toml b/web/template/config/development.toml
new file mode 100644
index 0000000..f07dcc2
--- /dev/null
+++ b/web/template/config/development.toml
@@ -0,0 +1,4 @@
+debug = true
+
+[database]
+echo = true
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<Self, ConfigError> {
+ 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::<String>("database.url"));
+
+ // You can deserialize (and thus freeze) the entire configuration as
+ s.try_deserialize()
+ }
+}