anti-cors update

This commit is contained in:
arrelin
2025-12-09 17:40:37 +03:00
parent b5e4e48420
commit 7e1e89424a
3 changed files with 36 additions and 1 deletions

17
Cargo.lock generated
View File

@@ -653,6 +653,7 @@ dependencies = [
"thiserror 2.0.17",
"time",
"tokio",
"tower-http",
"tower-sessions",
"tower-sessions-sqlx-store",
"utoipa",
@@ -2792,6 +2793,22 @@ dependencies = [
"tower-service",
]
[[package]]
name = "tower-http"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e9cd434a998747dd2c4276bc96ee2e0c7a2eadf3cae88e52be55a05fa9053f5"
dependencies = [
"bitflags",
"bytes",
"http",
"http-body",
"http-body-util",
"pin-project-lite",
"tower-layer",
"tower-service",
]
[[package]]
name = "tower-layer"
version = "0.3.3"

View File

@@ -9,6 +9,7 @@ sea-orm = { version = "1.0", features = ["sqlx-postgres", "runtime-tokio-rustls"
sea-orm-migration = { version = "1.0", default-features = false, features = ["sqlx-postgres", "runtime-tokio-rustls"] }
dotenvy = "0.15.7"
axum = { version = "0.7", features = ["json"] }
tower-http = { version = "0.5", features = ["cors"] }
chrono = { version = "0.4.42", features = ["serde"] }
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0"

View File

@@ -11,6 +11,8 @@ use tower_sessions::{Expiry, SessionManagerLayer};
use tower_sessions_sqlx_store::PostgresStore;
use axum_login::AuthManagerLayerBuilder;
use time::Duration;
use tower_http::cors::CorsLayer;
use axum::http::{Method, HeaderValue};
pub mod models;
pub mod services;
@@ -133,7 +135,22 @@ pub async fn create_app(db: DatabaseConnection) -> Result<Router, DbErr> {
let swagger_ui = SwaggerUi::new("/swagger-ui")
.url("/api-docs/openapi.json", ApiDoc::openapi());
let app = api_routes.merge(swagger_ui);
let cors = CorsLayer::new()
.allow_origin([
"http://localhost:3000".parse::<HeaderValue>().unwrap(),
"http://localhost:8080".parse::<HeaderValue>().unwrap(),
])
.allow_methods([Method::GET, Method::POST, Method::PUT, Method::DELETE, Method::OPTIONS])
.allow_headers([
axum::http::header::CONTENT_TYPE,
axum::http::header::AUTHORIZATION,
axum::http::header::ACCEPT,
])
.allow_credentials(true);
let app = api_routes
.layer(cors)
.merge(swagger_ui);
Ok(app)
}