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:
90
src/config.rs
Normal file
90
src/config.rs
Normal 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,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
pub mod handlers;
|
||||
pub mod models;
|
||||
pub mod config;
|
||||
|
||||
use axum::{
|
||||
Router,
|
||||
|
||||
43
src/main.rs
43
src/main.rs
@@ -1,32 +1,29 @@
|
||||
use oauth2::{AuthUrl, ClientId, ClientSecret, RedirectUrl, TokenUrl, basic::BasicClient};
|
||||
use sqlx::SqlitePool;
|
||||
use std::env;
|
||||
use weight_tracker::{AppState, create_app};
|
||||
use weight_tracker::{AppState, create_app, config::Config};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Load configuration from config.toml or environment variables
|
||||
let config = Config::load("config.toml").unwrap_or_else(|_| {
|
||||
println!("config.toml not found, using environment variables");
|
||||
Config::from_env()
|
||||
});
|
||||
|
||||
// Set up database
|
||||
let database_url = "sqlite:weight_tracker.db";
|
||||
let pool = SqlitePool::connect(database_url)
|
||||
let pool = SqlitePool::connect(&config.database.url)
|
||||
.await
|
||||
.expect("Failed to connect to database");
|
||||
|
||||
// Set up OIDC client
|
||||
let client_id =
|
||||
ClientId::new(env::var("OIDC_CLIENT_ID").unwrap_or_else(|_| "your_client_id".to_string()));
|
||||
let client_secret = ClientSecret::new(
|
||||
env::var("OIDC_CLIENT_SECRET").unwrap_or_else(|_| "your_client_secret".to_string()),
|
||||
);
|
||||
let auth_url = AuthUrl::new(
|
||||
env::var("OIDC_AUTH_URL").unwrap_or_else(|_| "https://your-provider.com/auth".to_string()),
|
||||
)
|
||||
.unwrap();
|
||||
let token_url = TokenUrl::new(
|
||||
env::var("OIDC_TOKEN_URL")
|
||||
.unwrap_or_else(|_| "https://your-provider.com/token".to_string()),
|
||||
)
|
||||
.unwrap();
|
||||
let redirect_url = RedirectUrl::new("http://localhost:3000/auth/callback".to_string()).unwrap();
|
||||
let client_id = ClientId::new(config.oidc.client_id);
|
||||
let client_secret = ClientSecret::new(config.oidc.client_secret);
|
||||
let auth_url = AuthUrl::new(config.oidc.auth_url)
|
||||
.unwrap();
|
||||
let token_url = TokenUrl::new(config.oidc.token_url)
|
||||
.unwrap();
|
||||
let redirect_url = RedirectUrl::new(config.oidc.redirect_url)
|
||||
.unwrap();
|
||||
|
||||
let oidc_client = BasicClient::new(client_id)
|
||||
.set_client_secret(client_secret)
|
||||
@@ -35,10 +32,7 @@ async fn main() {
|
||||
// Set the URL the user will be redirected to after the authorization process.
|
||||
.set_redirect_uri(redirect_url);
|
||||
|
||||
let secret = env::var("SESSION_SECRET")
|
||||
.unwrap_or_else(|_| "your_secret_key_that_is_long_enough_so_the_library_does_not_complain".to_string())
|
||||
.as_bytes()
|
||||
.to_vec();
|
||||
let secret = config.session.secret.as_bytes().to_vec();
|
||||
|
||||
let app_state = AppState {
|
||||
pool: pool.clone(),
|
||||
@@ -47,7 +41,8 @@ async fn main() {
|
||||
|
||||
let app = create_app(app_state, secret, pool).await;
|
||||
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
|
||||
let addr = format!("{}:{}", config.server.host, config.server.port);
|
||||
let listener = tokio::net::TcpListener::bind(&addr)
|
||||
.await
|
||||
.unwrap();
|
||||
println!("listening on {}", listener.local_addr().unwrap());
|
||||
|
||||
Reference in New Issue
Block a user