Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc(example): add cors for salvo_echo #221

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/salvo-echo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
path = "salvo_echo.rs"
8 changes: 8 additions & 0 deletions examples/salvo-echo/salvo_echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use socketioxide::{
SocketIo,
};

use tower::ServiceBuilder;
use tower_http::cors::CorsLayer;
use tracing::info;
use tracing_subscriber::FmtSubscriber;

Expand Down Expand Up @@ -41,6 +43,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

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);

Expand Down
Loading