From f92f21f47f37eaba58d1fef49f054689d95ce913 Mon Sep 17 00:00:00 2001 From: puripuri2100 Date: Thu, 21 Sep 2023 07:34:32 +0900 Subject: [PATCH] =?UTF-8?q?cors=E8=A8=AD=E5=AE=9A=E3=82=92=E3=81=97?= =?UTF-8?q?=E3=81=9F=20=E5=85=A8=E3=81=A6=E3=81=8B=E3=82=89=E3=81=AE?= =?UTF-8?q?=E3=82=A2=E3=82=AF=E3=82=BB=E3=82=B9=E3=82=92=E8=A8=B1=E5=8F=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 25 +++++++++++++++++++++++++ Cargo.toml | 1 + src/app.rs | 7 +++++++ 3 files changed, 33 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index e7bbc9f..d60be1e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -723,6 +723,12 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "http-range-header" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "add0ab9360ddbd88cfeb3bd9574a1d85cfdfa14db10b3e21d3700dbc4328758f" + [[package]] name = "httparse" version = "1.8.0" @@ -1313,6 +1319,7 @@ dependencies = [ "structopt", "thiserror", "tokio", + "tower-http", "tracing", "tracing-subscriber", "url", @@ -2182,6 +2189,24 @@ dependencies = [ "tracing", ] +[[package]] +name = "tower-http" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61c5bb1d698276a2443e5ecfabc1008bf15a36c12e6a7176e7bf089ea9131140" +dependencies = [ + "bitflags 2.3.3", + "bytes", + "futures-core", + "futures-util", + "http", + "http-body", + "http-range-header", + "pin-project-lite", + "tower-layer", + "tower-service", +] + [[package]] name = "tower-layer" version = "0.3.2" diff --git a/Cargo.toml b/Cargo.toml index 3ea48fb..1ba4eb5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ sqlx = { version = "0.7.0", features = [ "runtime-tokio-rustls", "json", "chrono structopt = "0.3.26" thiserror = "1.0.41" tokio = { version = "1.29.1", features = ["full"] } +tower-http = { version = "0.4.4", features = ["cors"] } tracing = "0.1.37" tracing-subscriber = "0.3.17" url = "2.4.0" diff --git a/src/app.rs b/src/app.rs index 5349b5f..9de5073 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,6 +1,7 @@ use anyhow::Result; use axum::{ extract::Query, + http::Method, routing::{delete, get, post}, Router, }; @@ -8,6 +9,7 @@ use chrono::Utc; use std::collections::HashMap; use std::net::SocketAddr; use std::sync::Arc; +use tower_http::cors::{Any, CorsLayer}; use uuid::Uuid; /// コンテナの管理を行うエンドポイントの定義 @@ -138,6 +140,11 @@ pub async fn app(bind: SocketAddr) -> Result<()> { let conn = Arc::clone(&conn); move |body| container::insert_container(body, conn) }), + ) + .layer( + CorsLayer::new() + .allow_methods([Method::GET, Method::POST]) + .allow_origin(Any), ); // サーバーの実行