This commit is contained in:
2025-06-08 13:27:50 -04:00
commit 41c4eca4f5
18 changed files with 2656 additions and 0 deletions

59
src/main.rs Executable file
View File

@@ -0,0 +1,59 @@
use axum::{
http::{header, Method},
Router,
};
use std::env;
use tower_http::cors::{CorsLayer, Any};
use crate::auth::structs::AppState;
use crate::auth::auth::router as auth_router;
use crate::data::data::router as data_router;
use crate::state::data::{get_counties_by_state, get_county_by_id};
mod auth;
mod data;
mod state;
#[tokio::main]
async fn main() {
// Load environment variables
dotenv::dotenv().ok();
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let jwt_secret = env::var("JWT_SECRET").expect("JWT_SECRET must be set");
let frontend_origin = env::var("FRONTEND_ORIGIN").unwrap_or_else(|_| "http://localhost:9551".to_string());
// Connect to PostgreSQL
let db = sqlx::PgPool::connect(&database_url)
.await
.expect("Failed to connect to database");
// Create app state
let state = AppState { db, jwt_secret };
// Configure CORS
let cors = CorsLayer::new()
.allow_origin(frontend_origin.parse::<header::HeaderValue>().unwrap())
.allow_methods([Method::GET, Method::POST])
.allow_headers(Any);
// Build router
let app = Router::new()
.route("/", axum::routing::get(|| async { "API is running" }))
.merge(auth_router())
.merge(data_router())
.route("/state/:state_abbr", axum::routing::get(get_counties_by_state))
.route("/state/:state_abbr/:county_id", axum::routing::get(get_county_by_id))
.layer(cors)
.with_state(state);
// Print server status
// use std::io::Write;
println!("Server is running on http://0.0.0.0:9552");
// std::io::stdout().flush().unwrap();
// Run server
axum::Server::bind(&"0.0.0.0:9552".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}