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:
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