Athenaeum/src/main.rs

66 lines
1.9 KiB
Rust
Raw Normal View History

2025-05-30 18:00:52 +02:00
mod models;
mod auth;
mod books;
mod cart;
mod profile;
mod error;
use actix_web::{web, App, HttpServer};
2025-05-23 15:25:48 +02:00
use actix_cors::Cors;
use actix_files::Files;
use dotenv::dotenv;
2025-05-30 18:00:52 +02:00
use env_logger::Builder;
2025-05-23 15:25:48 +02:00
use sqlx::postgres::PgPoolOptions;
2025-05-30 18:00:52 +02:00
use std::env;
2025-05-30 20:02:17 +02:00
use log;
2025-05-23 15:25:48 +02:00
#[actix_web::main]
async fn main() -> std::io::Result<()> {
2025-05-30 18:00:52 +02:00
Builder::from_env(env_logger::Env::default().default_filter_or("debug")).init();
2025-05-23 15:25:48 +02:00
dotenv().ok();
2025-05-30 18:00:52 +02:00
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set in .env");
2025-05-23 15:25:48 +02:00
let pool = PgPoolOptions::new()
.max_connections(5)
.connect(&database_url)
.await
.expect("Failed to create pool");
HttpServer::new(move || {
let cors = Cors::default()
.allow_any_origin()
2025-05-30 15:40:23 +02:00
.allowed_methods(vec!["GET", "POST", "DELETE"])
2025-05-23 15:25:48 +02:00
.allowed_headers(vec![
2025-05-30 18:00:52 +02:00
actix_web::http::header::CONTENT_TYPE,
actix_web::http::header::AUTHORIZATION,
actix_web::http::header::ACCEPT,
2025-05-25 16:25:22 +02:00
])
.supports_credentials();
2025-05-23 15:25:48 +02:00
App::new()
2025-05-25 16:25:22 +02:00
.app_data(web::Data::new(pool.clone()))
2025-05-23 15:25:48 +02:00
.wrap(cors)
.wrap(actix_web::middleware::Logger::default())
2025-05-30 18:00:52 +02:00
.service(books::get_books)
.service(books::get_book)
.service(auth::register)
.service(auth::login)
2025-05-30 20:02:17 +02:00
.service(auth::logout)
.service(auth::check_auth)
2025-05-30 18:00:52 +02:00
.service(cart::get_cart)
.service(cart::add_to_cart)
.service(cart::remove_from_cart)
2025-05-30 20:02:17 +02:00
.service(cart::checkout)
2025-05-30 18:00:52 +02:00
.service(profile::get_order_history)
2025-05-24 16:47:32 +02:00
.service(
2025-05-25 16:25:22 +02:00
Files::new("/images", "./static/images")
2025-05-24 16:47:32 +02:00
.show_files_listing(),
)
2025-05-23 15:25:48 +02:00
.service(Files::new("/", "./static").index_file("index.html"))
})
.bind("0.0.0.0:7999")?
.run()
.await
}
2025-05-25 16:25:22 +02:00