Skip to content

Commit

Permalink
Fix RPC cors issue of preflight request
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Jan 19, 2024
1 parent 54b3749 commit 0521e15
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async-trait = "0.1"
axum = "0.6.20"
tokio-util = { version = "0.7.3", features = ["codec"] }
futures-util = { version = "0.3.21" }
tower-http = { version = "0.3.5", features = ["timeout"] }
tower-http = { version = "0.3.5", features = ["timeout", "cors"] }
async-stream = "0.3.3"
ckb-async-runtime = { path = "../util/runtime", version = "= 0.114.0-pre" }

Expand Down
4 changes: 4 additions & 0 deletions rpc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::sync::Arc;
use std::time::Duration;
use tokio::net::TcpListener;
use tokio_util::codec::{FramedRead, FramedWrite, LinesCodec, LinesCodecError};
use tower_http::cors::CorsLayer;
use tower_http::timeout::TimeoutLayer;

#[doc(hidden)]
Expand Down Expand Up @@ -87,13 +88,16 @@ impl RpcServer {
.with_channel_size(4)
.with_pipeline_size(4);

let cors = CorsLayer::permissive();

// HTTP and WS server.
let method_router =
post(handle_jsonrpc::<Option<Session>>).get(handle_jsonrpc_ws::<Option<Session>>);
let mut app = Router::new()
.route("/", method_router.clone())
.route("/*path", method_router)
.layer(Extension(Arc::clone(rpc)))
.layer(cors)
.layer(TimeoutLayer::new(Duration::from_secs(30)));

if enable_websocket {
Expand Down

0 comments on commit 0521e15

Please sign in to comment.