Skip to content

Commit

Permalink
Merge pull request #221 from Totodore/doc-salvo-cors
Browse files Browse the repository at this point in the history
doc(example): add `cors` for `salvo_echo`
  • Loading branch information
Totodore authored Dec 28, 2023
2 parents 39bc2f9 + 2aacd3a commit 462f95b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
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

0 comments on commit 462f95b

Please sign in to comment.