feat: add --http.corsdomain#1305
Conversation
|
I have created a draft version as it's a first pass. I just want to know if I am doing it right. I think we need to add support for |
--http.corsdomain
mattsse
left a comment
There was a problem hiding this comment.
great!
left some suggestions
crates/rpc/rpc-builder/src/lib.rs
Outdated
| } | ||
|
|
||
| // Confiugures the http server with cors domain | ||
| pub fn with_http_cors_domain(mut self, config: ServerBuilder, domains: Vec<&str>) -> Self { |
There was a problem hiding this comment.
I would rather have this as a separate field (cors_domans: Option<...>)and then this is created in build
There was a problem hiding this comment.
Could you please elaborate on this? I am a bit confused
bin/reth/src/node/mod.rs
Outdated
| if let Some(domains) = self.rpc.http_corsdomain.as_ref() { | ||
| let domains_vec = domains.split(",").collect::<Vec<&str>>(); | ||
| let _rpc_server = reth_rpc_builder::launch( | ||
| ShareableDatabase::new(db.clone()), | ||
| reth_transaction_pool::test_utils::testing_pool(), | ||
| network.clone(), | ||
| TransportRpcModuleConfig::default() | ||
| .with_http(vec![RethRpcModule::Admin, RethRpcModule::Eth]), | ||
| RpcServerConfig::default().with_http_cors_domain(Default::default(),domains_vec), | ||
| ) | ||
| .await?; |
There was a problem hiding this comment.
we should leave this entirely up to the rpc builder.
It should handle Option<domain>
|
I have made some changes as per the comments but I am not exactly sure how to test them |
mattsse
left a comment
There was a problem hiding this comment.
thanks!
left some suggestions
bin/reth/src/node/mod.rs
Outdated
| .with_http(vec![RethRpcModule::Admin, RethRpcModule::Eth]), | ||
| RpcServerConfig::default().with_http(Default::default()), | ||
| RpcServerConfig::default() | ||
| .with_http(Default::default(), self.rpc.http_corsdomain.as_ref()), |
There was a problem hiding this comment.
the corsdomain should be configured in a separate function.
with_http should only take the server config imo.
crates/rpc/rpc-builder/src/lib.rs
Outdated
| /// Configs for JSON-RPC that allow Corsdomains | ||
| http_cors_domain_server_config: Option<ServerBuilder<Stack<CorsLayer, Identity>>>, |
There was a problem hiding this comment.
separate for cors domain only
crates/rpc/rpc-builder/src/lib.rs
Outdated
| if let Some(domains) = domains { | ||
| match domains.as_str() { | ||
| "*" => { | ||
| let cors = CorsLayer::new() | ||
| .allow_methods([Method::GET, Method::POST]) | ||
| .allow_origin(Any) | ||
| .allow_headers(Any); | ||
|
|
||
| let middleware = tower::ServiceBuilder::new().layer(cors); | ||
| self.http_cors_domain_server_config = | ||
| Some(config.set_middleware(middleware).http_only()); | ||
| } |
There was a problem hiding this comment.
all of this needs to be set when the server is started
There was a problem hiding this comment.
yeah @Tirthnp what you want here is to just set the config variable, and then in the build/start function you actually consume them
crates/rpc/rpc-builder/src/lib.rs
Outdated
| if let Some(domains) = domains { | ||
| match domains.as_str() { | ||
| "*" => { | ||
| let cors = CorsLayer::new() | ||
| .allow_methods([Method::GET, Method::POST]) | ||
| .allow_origin(Any) | ||
| .allow_headers(Any); | ||
|
|
||
| let middleware = tower::ServiceBuilder::new().layer(cors); | ||
| self.http_cors_domain_server_config = | ||
| Some(config.set_middleware(middleware).http_only()); | ||
| } |
There was a problem hiding this comment.
yeah @Tirthnp what you want here is to just set the config variable, and then in the build/start function you actually consume them
crates/rpc/rpc-builder/src/lib.rs
Outdated
| /// Configures the http server | ||
| pub fn with_http(mut self, config: ServerBuilder) -> Self { | ||
| self.http_server_config = Some(config.http_only()); | ||
| pub fn with_http(mut self, config: ServerBuilder, domains: Option<&String>) -> Self { |
There was a problem hiding this comment.
The domains method should be separate, so one should be able to do with_http(config).with_cors(Some("*"))
mattsse
left a comment
There was a problem hiding this comment.
great progress.
mostly style nits, otherwise this is pretty close.
crates/rpc/rpc-builder/src/lib.rs
Outdated
| match domains.as_str() { | ||
| "*" => { | ||
| cors = Some(CorsLayer::new() | ||
| .allow_methods([Method::GET, Method::POST]) | ||
| .allow_origin(Any) | ||
| .allow_headers(Any)); | ||
| } | ||
| "" => {} | ||
| _ => { | ||
| let domains_vec = domains.split(",").collect::<Vec<&str>>(); | ||
|
|
||
| let origins = domains_vec | ||
| .into_iter() | ||
| .map(|domain| domain.parse().unwrap()) | ||
| .collect::<Vec<HeaderValue>>(); | ||
|
|
||
| cors = Some(CorsLayer::new() | ||
| .allow_methods([Method::GET, Method::POST]) | ||
| .allow_origin(origins) | ||
| .allow_headers(Any)); |
There was a problem hiding this comment.
please extract this to a standalone function that returns Result<Option<CorsLayer>
crates/rpc/rpc-builder/src/lib.rs
Outdated
| let domains_vec = domains.split(",").collect::<Vec<&str>>(); | ||
|
|
||
| let origins = domains_vec | ||
| .into_iter() | ||
| .map(|domain| domain.parse().unwrap()) |
There was a problem hiding this comment.
can be combined into domains.split(",").into_iter()
crates/rpc/rpc-builder/src/lib.rs
Outdated
| .collect::<Vec<HeaderValue>>(); | ||
|
|
There was a problem hiding this comment.
once move to function, collect into Result:
| .collect::<Vec<HeaderValue>>(); | |
| .collect::<Result<Vec<HeaderValue>,_>(); | |
crates/rpc/rpc-builder/src/lib.rs
Outdated
| match cors { | ||
| Some(cors) => { |
There was a problem hiding this comment.
nit:
use if let Some() else instead of match
| /// Http Servers Enum | ||
| pub enum HttpServer { | ||
| /// Http server | ||
| Plain(Server), | ||
| /// Http server with cors | ||
| WithCors(Server<Stack<CorsLayer, Identity>>), | ||
| } |
There was a problem hiding this comment.
this is okay for now, perhaps we find a better solution but for now this is ok
gakonst
left a comment
There was a problem hiding this comment.
Good with me, couple nits which we can do in follow up
| let origins = domains | ||
| .split(",") | ||
| .map(|domain| domain.parse::<HeaderValue>()) | ||
| .collect::<Result<Vec<HeaderValue>, _>>(); |
There was a problem hiding this comment.
Nit I'd add a CoraDomains type that impls FromStr and includes this logic, and do Option in the upstream cli code
| if let Some(cors) = cors { | ||
| let middleware = tower::ServiceBuilder::new().layer(cors); | ||
| let http_server = | ||
| builder.set_middleware(middleware).build(http_socket_addr).await?; | ||
| server.http_local_addr = http_server.local_addr().ok(); | ||
| server.http = Some(HttpServer::WithCors(http_server)); | ||
| } else { | ||
| let http_server = builder.build(http_socket_addr).await?; | ||
| server.http_local_addr = http_server.local_addr().ok(); | ||
| server.http = Some(HttpServer::Plain(http_server)); | ||
| } |
There was a problem hiding this comment.
Towers middlewares are so powerful. We should investigate what else is available @mattsse may let us do nice rate limiting etc features in the node
Codecov Report
📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more @@ Coverage Diff @@
## main #1305 +/- ##
==========================================
- Coverage 76.23% 76.05% -0.18%
==========================================
Files 357 358 +1
Lines 41118 42303 +1185
==========================================
+ Hits 31347 32175 +828
- Misses 9771 10128 +357
Flags with carried forward coverage won't be shown. Click here to find out more.
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
mattsse
left a comment
There was a problem hiding this comment.
lgtm,
please enable edits by maintainer next PR
I will, Thanks you so much for all the help :) |
Closes #1230