Issues vscode rreading errors
This commit is contained in:
32
src/main.rs
32
src/main.rs
@@ -1,38 +1,48 @@
|
||||
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};
|
||||
use crate::state::data::router as state_router;
|
||||
use crate::company::company::company_routes;
|
||||
use sqlx::PgPool;
|
||||
use std::sync::Arc;
|
||||
|
||||
mod auth;
|
||||
mod data;
|
||||
mod state;
|
||||
mod company;
|
||||
|
||||
#[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 _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)
|
||||
let db_pool = PgPool::connect(&database_url)
|
||||
.await
|
||||
.expect("Failed to connect to database");
|
||||
|
||||
let db = Arc::new(db_pool);
|
||||
|
||||
// Create app state
|
||||
let state = AppState { db, jwt_secret };
|
||||
let state = AppState {
|
||||
db_url: database_url.clone(),
|
||||
db: db.clone(),
|
||||
jwt_secret: env::var("JWT_SECRET").expect("JWT_SECRET must be set"),
|
||||
};
|
||||
|
||||
// Configure CORS
|
||||
let cors = CorsLayer::new()
|
||||
.allow_origin(frontend_origin.parse::<header::HeaderValue>().unwrap())
|
||||
.allow_origin(tower_http::cors::AllowOrigin::exact(frontend_origin.parse::<header::HeaderValue>().unwrap()))
|
||||
.allow_methods([Method::GET, Method::POST])
|
||||
.allow_headers(Any);
|
||||
|
||||
@@ -41,15 +51,13 @@ async fn main() {
|
||||
.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);
|
||||
.merge(state_router())
|
||||
.merge(company_routes().await)
|
||||
.with_state(state.clone())
|
||||
.layer(cors);
|
||||
|
||||
// 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())
|
||||
|
||||
Reference in New Issue
Block a user