2025-03-11 10:29:22 -07:00
|
|
|
use anyhow::{Context as _, Result};
|
2025-02-26 13:10:48 -08:00
|
|
|
use axum::extract::FromRef;
|
2025-03-12 23:16:22 -07:00
|
|
|
use config::{Config, Environment};
|
2025-02-26 13:10:50 -08:00
|
|
|
use dotenvy::dotenv;
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
2025-02-26 13:10:48 -08:00
|
|
|
use crate::app_state::AppState;
|
|
|
|
|
2025-02-26 13:10:50 -08:00
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
|
|
pub struct Settings {
|
|
|
|
#[serde(default)]
|
|
|
|
pub base_path: String,
|
|
|
|
|
|
|
|
pub database_url: String,
|
|
|
|
|
2025-03-14 13:04:57 -07:00
|
|
|
/// When set to 1, embedded Diesel migrations will be run on startup.
|
2025-03-11 10:33:37 -07:00
|
|
|
pub run_database_migrations: Option<u8>,
|
|
|
|
|
2025-02-26 13:10:50 -08:00
|
|
|
#[serde(default = "default_host")]
|
|
|
|
pub host: String,
|
|
|
|
|
|
|
|
#[serde(default = "default_port")]
|
|
|
|
pub port: u16,
|
|
|
|
|
2025-02-26 13:10:45 -08:00
|
|
|
pub auth: AuthSettings,
|
|
|
|
|
|
|
|
pub email: EmailSettings,
|
2025-02-26 13:10:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn default_port() -> u16 {
|
|
|
|
3000
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_host() -> String {
|
|
|
|
"127.0.0.1".to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
2025-02-26 13:10:45 -08:00
|
|
|
pub struct AuthSettings {
|
2025-02-26 13:10:50 -08:00
|
|
|
pub client_id: String,
|
|
|
|
pub client_secret: String,
|
|
|
|
pub redirect_url: String,
|
|
|
|
pub auth_url: String,
|
|
|
|
pub token_url: String,
|
|
|
|
pub userinfo_url: String,
|
2025-02-26 13:10:45 -08:00
|
|
|
pub logout_url: Option<String>,
|
2025-02-26 13:10:50 -08:00
|
|
|
|
|
|
|
#[serde(default = "default_cookie_name")]
|
|
|
|
pub cookie_name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_cookie_name() -> String {
|
2025-02-26 13:10:46 -08:00
|
|
|
"SHOUT_DOT_DEV_SESSION".to_string()
|
2025-02-26 13:10:50 -08:00
|
|
|
}
|
|
|
|
|
2025-02-26 13:10:43 -08:00
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
|
|
pub struct SmtpSettings {
|
|
|
|
pub server: String,
|
|
|
|
pub username: String,
|
|
|
|
pub password: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
|
|
pub struct PostmarkSettings {
|
|
|
|
pub server_token: String,
|
|
|
|
}
|
|
|
|
|
2025-02-26 13:10:45 -08:00
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
|
|
pub struct EmailSettings {
|
2025-02-26 13:10:43 -08:00
|
|
|
pub verification_from: lettre::message::Mailbox,
|
|
|
|
pub message_from: lettre::message::Mailbox,
|
|
|
|
pub smtp: Option<SmtpSettings>,
|
|
|
|
pub postmark: Option<PostmarkSettings>,
|
2025-02-26 13:10:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SlackSettings {
|
|
|
|
pub client_id: String,
|
|
|
|
pub client_secret: String,
|
|
|
|
pub redirect_url: String,
|
|
|
|
pub auth_url: String,
|
|
|
|
pub token_url: String,
|
|
|
|
}
|
|
|
|
|
2025-02-26 13:10:50 -08:00
|
|
|
impl Settings {
|
2025-03-11 10:29:22 -07:00
|
|
|
pub fn load() -> Result<Self> {
|
|
|
|
match dotenv() {
|
|
|
|
Err(err) => {
|
|
|
|
if err.not_found() {
|
|
|
|
tracing::info!("no .env file found");
|
|
|
|
} else {
|
|
|
|
return Err(err).context("dotenvy error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(pathbuf) => {
|
|
|
|
tracing::info!(
|
|
|
|
"using env file {}",
|
|
|
|
pathbuf
|
|
|
|
.to_str()
|
|
|
|
.ok_or(anyhow::anyhow!("pathbuf is not valid unicode"))?
|
|
|
|
);
|
|
|
|
}
|
2025-02-26 13:10:50 -08:00
|
|
|
}
|
|
|
|
let s = Config::builder()
|
2025-03-11 22:21:23 -07:00
|
|
|
.add_source(Environment::default().separator("__"))
|
2025-03-11 10:29:22 -07:00
|
|
|
.build()
|
|
|
|
.context("config error")?;
|
2025-03-14 13:04:57 -07:00
|
|
|
s.try_deserialize().context("deserialize error")
|
2025-02-26 13:10:50 -08:00
|
|
|
}
|
|
|
|
}
|
2025-02-26 13:10:48 -08:00
|
|
|
|
|
|
|
impl FromRef<AppState> for Settings {
|
|
|
|
fn from_ref(state: &AppState) -> Self {
|
|
|
|
state.settings.clone()
|
|
|
|
}
|
|
|
|
}
|