|
| 1 | +mod config; |
| 2 | +mod handlers; |
| 3 | +mod models; |
| 4 | +mod routes; |
| 5 | +mod state; |
| 6 | +mod storage; |
| 7 | + |
| 8 | +use crate::{state::AppState, storage::drivers::Postgis}; |
| 9 | + |
| 10 | +use clap::Parser; |
| 11 | +use sqlx::postgres::PgPoolOptions; |
| 12 | +use std::sync::Arc; |
| 13 | +use tokio::net::TcpListener; |
| 14 | + |
| 15 | +#[tokio::main] |
| 16 | +async fn main() { |
| 17 | + dotenvy::dotenv().ok(); |
| 18 | + tracing_subscriber::fmt::init(); |
| 19 | + |
| 20 | + let args = Args::parse(); |
| 21 | + |
| 22 | + let port = std::env::var("PORT").unwrap_or_else(|_| "3000".to_string()); |
| 23 | + |
| 24 | + let config_str = |
| 25 | + std::fs::read_to_string(&args.config).expect("Failed to read configuration file"); |
| 26 | + let config: config::AppConfig = |
| 27 | + toml::from_str(&config_str).expect("Failed to parse configuration"); |
| 28 | + let config = Arc::new(config); |
| 29 | + |
| 30 | + let pool = PgPoolOptions::new() |
| 31 | + .max_connections(5) |
| 32 | + .connect( |
| 33 | + &std::env::var("DATABASE_URL").expect("`DATABASE_URL` must be set in the environment"), |
| 34 | + ) |
| 35 | + .await |
| 36 | + .expect("Failed to connect to the database"); |
| 37 | + |
| 38 | + let store = Arc::new(Postgis::new(pool, Arc::clone(&config))); |
| 39 | + |
| 40 | + let app_state = AppState { store, config }; |
| 41 | + |
| 42 | + let app = routes::create_router(app_state); |
| 43 | + |
| 44 | + let listener = TcpListener::bind(format!("127.0.0.1:{}", port)) |
| 45 | + .await |
| 46 | + .unwrap(); |
| 47 | + axum::serve(listener, app).await.unwrap(); |
| 48 | +} |
| 49 | + |
| 50 | +#[derive(Parser, Debug)] |
| 51 | +#[command(author, version, about, long_about = None)] |
| 52 | +struct Args { |
| 53 | + #[arg(short, long, default_value = "config.toml")] |
| 54 | + config: String, |
| 55 | +} |
0 commit comments