From 89bb2170f39b5316fe30f9b58ef2d1e82ea0210c Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Fri, 12 Jun 2026 12:17:49 +0300 Subject: [PATCH] Disable cors --- .gitignore | 1 + Cargo.lock | 17 +++++++++++++++++ Cargo.toml | 3 ++- src/main.rs | 9 ++++++++- 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index ea8c4bf..40d9aca 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/.idea \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 9488a82..028939e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -324,6 +324,7 @@ dependencies = [ "axum", "rand", "tokio", + "tower-http", ] [[package]] @@ -682,6 +683,22 @@ dependencies = [ "tracing", ] +[[package]] +name = "tower-http" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cfcf7e2740e6fc6d4d688b4ef00650406bb94adf4731e43c096c3a19fe40840" +dependencies = [ + "bitflags", + "bytes", + "http", + "http-body", + "pin-project-lite", + "tower-layer", + "tower-service", + "tracing", +] + [[package]] name = "tower-layer" version = "0.3.3" diff --git a/Cargo.toml b/Cargo.toml index 1750cd8..672f443 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ edition = "2024" [dependencies] axum = { version = "0.8.9", features = ["multipart", ] } tokio = { version = "1.52.3", features = ["full"] } -rand = "0.10.1" \ No newline at end of file +rand = "0.10.1" +tower-http = {version = "0.6.6", features = ["cors", "trace"]} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 03633b3..72a89c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ use axum::extract::{DefaultBodyLimit, Path}; use axum::http::StatusCode; +use axum::http::Method; + use axum::response::IntoResponse; use axum::routing::post; use axum::{extract::Multipart, routing::get, Router}; @@ -8,17 +10,22 @@ use rand::rng; use std::os::unix::prelude::MetadataExt; use std::path::PathBuf; use tokio::fs::File; +use tower_http::cors::{Any, CorsLayer}; #[tokio::main] async fn main() { ensure_work_dir(); + let cors = CorsLayer::new() + .allow_origin(Any) + .allow_methods(vec![Method::GET, Method::POST]).allow_headers(Any); let app = Router::new() .route("/generate", get(generate_id)) .route("/upload/{id}", post(upload_file)) .route("/download/{id}", get(download_file)) .route("/{id}/version", get(version)) - .layer(DefaultBodyLimit::max(50 * 1024 * 1024)); + .layer(DefaultBodyLimit::max(50 * 1024 * 1024)) + .layer(cors); let listener = tokio::net::TcpListener::bind("0.0.0.0:8089").await.unwrap();