Skip to content

Commit

Permalink
Add cors
Browse files Browse the repository at this point in the history
  • Loading branch information
boyuan-chen committed Jun 18, 2024
1 parent 01c4b69 commit 71da304
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions crates/rpc/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use jsonrpsee::{
server::{middleware::ProxyGetRequestLayer, ServerBuilder},
RpcModule,
};
use tower_http::cors::{Any, CorsLayer};
use rundler_builder::BuilderServer;
use rundler_pool::PoolServer;
use rundler_provider::EntryPoint;
Expand Down Expand Up @@ -110,11 +111,23 @@ where
let health_checker = HealthChecker::new(servers);
module.merge(health_checker.into_rpc())?;

// Add a CORS middleware for handling HTTP requests.
// This middleware does affect the response, including appropriate
// headers to satisfy CORS. Because any origins are allowed, the
// "Access-Control-Allow-Origin: *" header is appended to the response.
let cors = CorsLayer::new()
// Allow `POST` when accessing the resource
.allow_methods([Method::POST])
// Allow requests from any origin
.allow_origin(Any)
.allow_headers([hyper::header::CONTENT_TYPE]);

// Set up health check endpoint via GET /health registers the jsonrpc handler
let service_builder = tower::ServiceBuilder::new()
// Proxy `GET /health` requests to internal `system_health` method.
.layer(ProxyGetRequestLayer::new("/health", "system_health")?)
.timeout(self.args.rpc_timeout);
.timeout(self.args.rpc_timeout)
.layer(cors);

let server = ServerBuilder::default()
.set_logger(RpcMetricsLogger)
Expand Down Expand Up @@ -194,4 +207,4 @@ where

Ok(())
}
}
}

0 comments on commit 71da304

Please sign in to comment.