Skip to content

Commit

Permalink
Add cors support
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Sep 18, 2020
1 parent 9202f89 commit 1f7445b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 16 additions & 3 deletions beacon_node/http_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ pub fn serve<T: BeaconChainTypes>(
) -> Result<(SocketAddr, impl Future<Output = ()>), Error> {
let config = ctx.config.clone();
let log = ctx.log.clone();
let allow_origin = config.allow_origin.clone();

if !config.enabled {
crit!(log, "Cannot start disabled HTTP server");
Expand All @@ -177,8 +178,6 @@ pub fn serve<T: BeaconChainTypes>(
));
}

// TODO: cors

let eth1_v1 = warp::path(API_PREFIX).and(warp::path(API_VERSION));

let beacon_proposer_cache = ctx
Expand Down Expand Up @@ -1583,7 +1582,21 @@ pub fn serve<T: BeaconChainTypes>(
.boxed()
.recover(warp_utils::reject::handle_rejection)
.with(slog_logging(log.clone()))
.with(prometheus_metrics());
.with(prometheus_metrics())
.map(|reply| warp::reply::with_header(reply, "Server", &version_with_platform()))
.map(move |reply| {
let reply: Box<dyn warp::Reply> = if let Some(allow_origin) = allow_origin.as_ref() {
Box::new(warp::reply::with_header(
reply,
"Access-Control-Allow-Origin",
allow_origin,
))
} else {
Box::new(reply)
};

reply
});

let (listening_socket, server) = warp::serve(routes).try_bind_with_graceful_shutdown(
SocketAddrV4::new(config.listen_addr, config.listen_port),
Expand Down
1 change: 1 addition & 0 deletions beacon_node/http_metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ slot_clock = { path = "../../common/slot_clock" }
lighthouse_metrics = { path = "../../common/lighthouse_metrics" }
lazy_static = "1.4.0"
eth2 = { path = "../../common/eth2" }
lighthouse_version = { path = "../../common/lighthouse_version" }

[dev-dependencies]
tokio = { version = "0.2.21", features = ["sync"] }
Expand Down
16 changes: 16 additions & 0 deletions beacon_node/http_metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern crate lazy_static;
mod metrics;

use beacon_chain::{BeaconChain, BeaconChainTypes};
use lighthouse_version::version_with_platform;
use serde::{Deserialize, Serialize};
use slog::{crit, info, Logger};
use std::future::Future;
Expand Down Expand Up @@ -63,6 +64,7 @@ pub fn serve<T: BeaconChainTypes>(
) -> Result<(SocketAddr, impl Future<Output = ()>), Error> {
let config = &ctx.config;
let log = ctx.log.clone();
let allow_origin = config.allow_origin.clone();

if !config.enabled {
crit!(log, "Cannot start disabled metrics HTTP server");
Expand All @@ -86,6 +88,20 @@ pub fn serve<T: BeaconChainTypes>(
.unwrap()
}),
)
})
.map(|reply| warp::reply::with_header(reply, "Server", &version_with_platform()))
.map(move |reply| {
let reply: Box<dyn warp::Reply> = if let Some(allow_origin) = allow_origin.as_ref() {
Box::new(warp::reply::with_header(
reply,
"Access-Control-Allow-Origin",
allow_origin,
))
} else {
Box::new(reply)
};

reply
});

let (listening_socket, server) = warp::serve(routes).try_bind_with_graceful_shutdown(
Expand Down

0 comments on commit 1f7445b

Please sign in to comment.