Added default headers (cors)

This commit is contained in:
2025-03-17 15:00:02 +01:00
parent c33f4e6243
commit 1280c745c7

View File

@@ -1,25 +1,25 @@
mod users;
mod cases; mod cases;
mod items; mod items;
mod types; mod types;
mod users;
mod utils; mod utils;
use users::*;
use cases::*; use cases::*;
use items::*; use items::*;
use users::*;
use utils::*; use utils::*;
use actix_web::web::Data; use actix_web::web::Data;
use actix_web::{App, HttpServer}; use actix_web::{App, HttpServer, middleware::DefaultHeaders};
use sqlx::sqlite::SqlitePool; use sqlx::sqlite::SqlitePool;
#[derive(Clone)] #[derive(Clone)]
struct AppState { struct AppState {
database: SqlitePool, database: SqlitePool,
token_expiration: u64, token_expiration: u64,
allow_origins: Vec<&'static str>,
} }
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
let pool = SqlitePool::connect("sqlite:database.db") let pool = SqlitePool::connect("sqlite:database.db")
@@ -28,9 +28,14 @@ async fn main() -> std::io::Result<()> {
let app_state = Data::new(AppState { let app_state = Data::new(AppState {
database: pool, database: pool,
token_expiration: 86400, token_expiration: 86400,
allow_origins: vec!["http://localhost:5173"],
}); });
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
.wrap(DefaultHeaders::new().add((
"Access-Control-Allow-Origin",
app_state.allow_origins.join(","),
)))
.service(login) .service(login)
.service(register) .service(register)
.service(logout) .service(logout)
@@ -43,7 +48,7 @@ async fn main() -> std::io::Result<()> {
.service(options) .service(options)
.app_data(app_state.clone()) .app_data(app_state.clone())
}) })
.bind(("127.0.0.1", 8000))? .bind(("127.0.0.1", 8000))?
.run() .run()
.await .await
} }