46 lines
1.5 KiB
Rust
46 lines
1.5 KiB
Rust
use std::str::FromStr;
|
|
|
|
use sqlx::SqlitePool;
|
|
use weight_tracker::{AppState, config::Config, create_app};
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
// Load configuration from config.toml or environment variables
|
|
// config-rs automatically merges file + environment + defaults
|
|
let config = Config::load("config.toml").unwrap_or_else(|e| {
|
|
eprintln!("Failed to load configuration: {}", e);
|
|
eprintln!("Using default values. Set environment variables prefixed with WEIGHT_TRACKER_ to override.");
|
|
std::process::exit(1);
|
|
});
|
|
|
|
// Set up database
|
|
let pool = SqlitePool::connect_with(
|
|
sqlx::sqlite::SqliteConnectOptions::from_str(&config.database.url)
|
|
.expect("Could not parse database URL")
|
|
.create_if_missing(true),
|
|
)
|
|
.await
|
|
.expect("Failed to connect to database");
|
|
sqlx::migrate!()
|
|
.run(&pool)
|
|
.await
|
|
.expect("Could not run database migrations");
|
|
|
|
// Set up OIDC client
|
|
let oidc_client = config.oidc.to_client();
|
|
|
|
let secret = config.session.secret.as_bytes().to_vec();
|
|
|
|
let app_state = AppState {
|
|
pool: pool.clone(),
|
|
oidc_client,
|
|
};
|
|
|
|
let app = create_app(app_state, secret, pool).await;
|
|
|
|
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());
|
|
axum::serve(listener, app).await.unwrap();
|
|
}
|