added listings

This commit is contained in:
2025-12-26 20:01:08 -05:00
parent 5d0a9bb255
commit 56ebb8eba2
19 changed files with 843 additions and 130 deletions

View File

@@ -6,10 +6,11 @@ use axum::{
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::router as state_router;
use crate::company::company::company_routes;
use crate::auth::auth::{auth_middleware, login, register};
use crate::data::data::get_user;
use crate::state::data::{get_counties_by_state, get_county_by_id};
use crate::listing::data::{get_listings, get_listing_by_id, get_listings_by_county, create_listing, update_listing, delete_listing};
use axum::middleware;
use sqlx::PgPool;
use std::sync::Arc;
@@ -17,6 +18,7 @@ mod auth;
mod data;
mod state;
mod company;
mod listing;
#[tokio::main]
async fn main() {
@@ -35,7 +37,6 @@ async fn main() {
// Create app state
let state = AppState {
db_url: database_url.clone(),
db: db.clone(),
jwt_secret: env::var("JWT_SECRET").expect("JWT_SECRET must be set"),
};
@@ -43,18 +44,29 @@ async fn main() {
// Configure CORS
let cors = CorsLayer::new()
.allow_origin(tower_http::cors::AllowOrigin::exact(frontend_origin.parse::<header::HeaderValue>().unwrap()))
.allow_methods([Method::GET, Method::POST])
.allow_methods([Method::GET, Method::POST, Method::PUT, Method::DELETE])
.allow_headers(Any);
// Build router
let app = Router::new()
.route("/", axum::routing::get(|| async { "API is running" }))
.merge(auth_router())
.merge(data_router())
.merge(state_router())
.merge(company_routes().await)
.with_state(state.clone())
.layer(cors);
// Build router with separated public and protected routes
let protected_routes = Router::new()
.route("/user", axum::routing::get(get_user))
.route("/company", axum::routing::any(crate::company::company::company_handler))
.route("/listing", axum::routing::get(get_listings))
.route("/listing", axum::routing::post(create_listing))
.route("/listing/:listing_id", axum::routing::get(get_listing_by_id))
.route("/listing/:listing_id", axum::routing::put(update_listing))
.route("/listing/:listing_id", axum::routing::delete(delete_listing))
.route_layer(middleware::from_fn_with_state(state.clone(), auth_middleware));
let public_routes = Router::new()
.route("/auth/register", axum::routing::post(register))
.route("/auth/login", axum::routing::post(login))
.route("/categories", axum::routing::get(crate::company::category::get_all_categories))
.route("/state/:state_abbr", axum::routing::get(get_counties_by_state))
.route("/state/:state_abbr/:county_id", axum::routing::get(get_county_by_id))
.route("/listings/county/:county_id", axum::routing::get(get_listings_by_county));
let app = public_routes.merge(protected_routes).with_state(state).layer(cors);
// Print server status
println!("Server is running on http://0.0.0.0:9552");