Add config.toml support for OIDC and server configuration

- Add toml crate dependency for TOML file parsing
- Create config module with Config struct to deserialize config.toml
- Support OIDC URLs (auth_url, token_url, redirect_url), credentials (client_id, client_secret)
- Make server host/port, database URL, and session secret configurable
- Create config.example.toml with all configuration options documented
- Update main.rs to load config.toml with fallback to environment variables
- Maintain backward compatibility with environment variable configuration
This commit is contained in:
Lukas Wölfer
2026-04-10 23:45:12 +02:00
parent d27022b26a
commit b6f03f9efb
7 changed files with 194 additions and 27 deletions

90
src/config.rs Normal file
View File

@@ -0,0 +1,90 @@
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
pub oidc: OidcConfig,
pub server: ServerConfig,
pub database: DatabaseConfig,
pub session: SessionConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OidcConfig {
pub client_id: String,
pub client_secret: String,
pub auth_url: String,
pub token_url: String,
pub redirect_url: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerConfig {
pub host: String,
pub port: u16,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatabaseConfig {
pub url: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionConfig {
pub secret: String,
}
impl Config {
/// Load configuration from a TOML file
/// Falls back to environment variables if file doesn't exist
pub fn load(path: &str) -> Result<Self, Box<dyn std::error::Error>> {
if Path::new(path).exists() {
let content = fs::read_to_string(path)?;
let config: Config = toml::from_str(&content)?;
Ok(config)
} else {
// Fall back to environment variables
Ok(Config::from_env())
}
}
/// Load configuration from environment variables
pub fn from_env() -> Self {
let client_id =
std::env::var("OIDC_CLIENT_ID").unwrap_or_else(|_| "your_client_id".to_string());
let client_secret = std::env::var("OIDC_CLIENT_SECRET")
.unwrap_or_else(|_| "your_client_secret".to_string());
let auth_url = std::env::var("OIDC_AUTH_URL")
.unwrap_or_else(|_| "https://your-provider.com/auth".to_string());
let token_url = std::env::var("OIDC_TOKEN_URL")
.unwrap_or_else(|_| "https://your-provider.com/token".to_string());
let redirect_url = std::env::var("OIDC_REDIRECT_URL")
.unwrap_or_else(|_| "http://localhost:3000/auth/callback".to_string());
let host = std::env::var("SERVER_HOST").unwrap_or_else(|_| "127.0.0.1".to_string());
let port = std::env::var("SERVER_PORT")
.unwrap_or_else(|_| "3000".to_string())
.parse()
.unwrap_or(3000);
let database_url = std::env::var("DATABASE_URL")
.unwrap_or_else(|_| "sqlite:weight_tracker.db".to_string());
let session_secret = std::env::var("SESSION_SECRET").unwrap_or_else(|_| {
"your_secret_key_that_is_long_enough_so_the_library_does_not_complain".to_string()
});
Config {
oidc: OidcConfig {
client_id,
client_secret,
auth_url,
token_url,
redirect_url,
},
server: ServerConfig { host, port },
database: DatabaseConfig { url: database_url },
session: SessionConfig {
secret: session_secret,
},
}
}
}