From 2aacd3a5655d46a657a38d00b575eceb77261d4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9odore=20Pr=C3=A9vot?= Date: Thu, 28 Dec 2023 22:38:37 +0000 Subject: [PATCH] doc(example): add `cors` for `salvo_echo` --- examples/salvo-echo/Cargo.toml | 4 +++- examples/salvo-echo/salvo_echo.rs | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/examples/salvo-echo/Cargo.toml b/examples/salvo-echo/Cargo.toml index 3a285904..4ddc1696 100644 --- a/examples/salvo-echo/Cargo.toml +++ b/examples/salvo-echo/Cargo.toml @@ -13,7 +13,9 @@ tokio = { workspace = true, features = ["rt-multi-thread", "macros"] } tracing-subscriber.workspace = true tracing.workspace = true serde_json.workspace = true +tower.workspace = true +tower-http = { version = "0.5.0", features = ["cors"] } [[bin]] name = "salvo-echo" -path = "salvo_echo.rs" \ No newline at end of file +path = "salvo_echo.rs" diff --git a/examples/salvo-echo/salvo_echo.rs b/examples/salvo-echo/salvo_echo.rs index b34ec1f1..945c3621 100644 --- a/examples/salvo-echo/salvo_echo.rs +++ b/examples/salvo-echo/salvo_echo.rs @@ -5,6 +5,8 @@ use socketioxide::{ SocketIo, }; +use tower::ServiceBuilder; +use tower_http::cors::CorsLayer; use tracing::info; use tracing_subscriber::FmtSubscriber; @@ -41,6 +43,12 @@ async fn main() -> Result<(), Box> { let (layer, io) = SocketIo::new_layer(); + // This code is used to integrates other tower layers before or after Socket.IO such as CORS + // Beware that classic salvo request won't pass through these layers + let layer = ServiceBuilder::new() + .layer(CorsLayer::permissive()) // Enable CORS policy + .layer(layer); // Mount Socket.IO + io.ns("/", on_connect); io.ns("/custom", on_connect);