Skip to content
This repository was archived by the owner on Dec 15, 2025. It is now read-only.

Commit 12cc9bd

Browse files
committed
fixed CORS issue for playground openrpc
1 parent feeb34d commit 12cc9bd

File tree

3 files changed

+42
-105
lines changed

3 files changed

+42
-105
lines changed

Cargo.lock

Lines changed: 13 additions & 89 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ thiserror = "1.0"
2626
clap = "2.33"
2727
git-version = "0.3.5"
2828
command-group = "1.0.8"
29-
hyper = "1.6.0"
30-
axum = { version = "0.7.4", features = ["http1"] }
29+
hyper = "1.6"
30+
# axum = { version = "0.7.4", features = ["http1"] }
3131
bytes = "1.0"
3232
jsonrpsee = { version = "0.24.9", features = ["server", "client", "macros"] }
3333
memchr = "2.5.0"
3434
async-trait = "0.1.88"
3535
reth-ipc = { git = "https://github.com/paradigmxyz/reth", package = "reth-ipc" }
36+
tower-http = { version = "0.5", features = ["cors"] }
37+
tower = "0.4"
3638

3739
[dev-dependencies]
3840
tokio = { version = "1.14.0", features = ["full", "test-util"] }

src/app/api.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
use super::rpc::{
2+
ZinitLoggingApiServer, ZinitRpcApiServer, ZinitServiceApiServer, ZinitSystemApiServer,
3+
};
14
use crate::zinit::ZInit;
25
use anyhow::{bail, Context, Result};
36
use jsonrpsee::server::ServerHandle;
47
use reth_ipc::server::Builder;
58
use serde::{Deserialize, Serialize};
69
use serde_json::Value;
710
use std::collections::HashMap;
8-
use super::rpc::{ZinitLoggingApiServer, ZinitRpcApiServer, ZinitServiceApiServer, ZinitSystemApiServer};
911
use std::sync::Arc;
1012
use tokio::sync::Mutex;
13+
use tower_http::cors::{Any, CorsLayer};
14+
use tower_http::cors::{AllowMethods, AllowHeaders};
1115

1216
#[derive(Clone, Debug, Deserialize, Serialize)]
1317
#[serde(rename_all = "lowercase")]
@@ -52,30 +56,37 @@ impl Api {
5256
}
5357

5458
pub async fn serve(&self, endpoint: String) -> Result<ApiServer> {
55-
5659
let server = Builder::default().build(endpoint);
5760
let mut module = ZinitRpcApiServer::into_rpc(self.clone());
5861
module.merge(ZinitSystemApiServer::into_rpc(self.clone()))?;
5962
module.merge(ZinitServiceApiServer::into_rpc(self.clone()))?;
6063
module.merge(ZinitLoggingApiServer::into_rpc(self.clone()))?;
6164

62-
6365
let _handle = server.start(module).await?;
6466

6567
Ok(ApiServer { _handle })
6668
}
67-
69+
6870
/// Start an HTTP/RPC server at a specified address
6971
pub async fn start_http_server(&self, address: String) -> Result<String> {
7072
// Parse the address string
71-
let socket_addr = address.parse::<std::net::SocketAddr>()
73+
let socket_addr = address
74+
.parse::<std::net::SocketAddr>()
7275
.context("Failed to parse socket address")?;
73-
74-
// Create the JSON-RPC server
75-
let server_rpc = jsonrpsee::server::ServerBuilder::default()
76+
77+
let cors = CorsLayer::new()
78+
// Allow `POST` when accessing the resource
79+
.allow_methods(AllowMethods::any())
80+
// Allow requests from any origin
81+
.allow_origin(Any)
82+
.allow_headers(AllowHeaders::any());
83+
let middleware = tower::ServiceBuilder::new().layer(cors);
84+
85+
// Create the JSON-RPC server with CORS support
86+
let server_rpc = jsonrpsee::server::ServerBuilder::default().set_http_middleware(middleware)
7687
.build(socket_addr)
7788
.await?;
78-
89+
7990
// Create and merge all API modules
8091
let mut rpc_module = ZinitRpcApiServer::into_rpc(self.clone());
8192
rpc_module.merge(ZinitSystemApiServer::into_rpc(self.clone()))?;
@@ -84,18 +95,18 @@ impl Api {
8495

8596
// Start the server
8697
let handle = server_rpc.start(rpc_module);
87-
98+
8899
// Store the handle
89100
let mut http_handle = self.http_server_handle.lock().await;
90101
*http_handle = Some(handle);
91-
102+
92103
Ok(format!("HTTP/RPC server started at {}", address))
93104
}
94-
105+
95106
/// Stop the HTTP/RPC server if running
96107
pub async fn stop_http_server(&self) -> Result<()> {
97108
let mut http_handle = self.http_server_handle.lock().await;
98-
109+
99110
if http_handle.is_some() {
100111
// The handle is automatically dropped, which should stop the server
101112
*http_handle = None;
@@ -104,4 +115,4 @@ impl Api {
104115
bail!("No HTTP/RPC server is currently running")
105116
}
106117
}
107-
}
118+
}

0 commit comments

Comments
 (0)