Skip to content

Commit

Permalink
Configuration of CORS headers added. (Zondax#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekhvalov authored Feb 23, 2024
1 parent 4bb3fa2 commit 8ff28e6
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ rand = { version = "0.8", default-features = false }
rand_core = { version = "0.6", default-features = false }
tokio = { version = "1.26.0", features = ["rt-multi-thread"] }
hex = { version = "0.4", features = ["serde"] }
http = "0.2.11"
futures-util = { version = "0.3", features = ["sink"] }
bytes = "1.0"
time = "0.3"
Expand Down Expand Up @@ -52,6 +53,7 @@ prost-types = "0.12.0"
futures = "0.3.28"
opentelemetry = "0.20.0"
tracing-opentelemetry = "0.20.0"
tower-http = { version = "0.4.4", features = ["cors"] }
opentelemetry-jaeger = "0.19.0"
opentelemetry_api = { version = "0.20.0", features = ["metrics"] }
axum-prometheus = { version = "0.4.0", optional = true }
Expand Down
1 change: 1 addition & 0 deletions config/Settings.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ create_index = true
[server]
serve_at = "0.0.0.0"
port = 30303
cors_allow_origins = []

[indexer]
tendermint_addr = "http://127.0.0.1"
Expand Down
5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct IndexerConfig {
pub struct ServerConfig {
pub serve_at: String,
pub port: u16,
pub cors_allow_origins: Vec<String>,
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -102,6 +103,7 @@ impl Default for ServerConfig {
Self {
serve_at: SERVER_ADDR.to_owned(),
port: SERVER_PORT,
cors_allow_origins: vec![],
}
}
}
Expand Down Expand Up @@ -147,6 +149,8 @@ pub struct CliSettings {
pub server_serve_at: String,
#[clap(long, env, default_value_t = SERVER_PORT)]
pub server_port: u16,
#[clap(long, env)]
pub server_cors_allow_origin: Vec<String>,
#[clap(long, env, default_value = "localhost")]
pub database_host: String,
#[clap(long, env, default_value = "postgres")]
Expand Down Expand Up @@ -221,6 +225,7 @@ impl From<CliSettings> for Settings {
server: ServerConfig {
serve_at: value.server_serve_at,
port: value.server_port,
cors_allow_origins: value.server_cors_allow_origin,
},
indexer: IndexerConfig {
tendermint_addr: value.indexer_tendermint_addr,
Expand Down
15 changes: 14 additions & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use axum::{routing::get, Router};
use tower_http::cors::{Any, CorsLayer};
use http::{HeaderValue, Method};

#[cfg(feature = "prometheus")]
use axum_prometheus::{PrometheusMetricLayerBuilder, AXUM_HTTP_REQUESTS_DURATION_SECONDS};
Expand Down Expand Up @@ -41,6 +43,9 @@ pub struct ServerState {
}

fn server_routes(state: ServerState) -> Router<()> {
let cors = CorsLayer::new()
.allow_methods([Method::GET, Method::POST])
.allow_origin(Any);
Router::new()
.route("/block/height/:block_height", get(get_block_by_height))
.route("/block/hash/:block_hash", get(get_block_by_hash))
Expand All @@ -53,6 +58,7 @@ fn server_routes(state: ServerState) -> Router<()> {
"/validator/:validator_address/uptime",
get(get_validator_uptime),
)
.layer(cors)
.with_state(state)
}

Expand Down Expand Up @@ -91,7 +97,14 @@ pub fn create_server(
.with_metrics_from_fn(|| prometheus_handle)
.build_pair();

let routes = server_routes(ServerState { db, checksums_map });
let mut routes = server_routes(ServerState { db, checksums_map });
if !config.cors_allow_origins.is_empty() {
let origins: Vec<HeaderValue> = config.cors_allow_origins.iter().map(|s| s.parse::<HeaderValue>().unwrap()).collect();
let cors = CorsLayer::new()
.allow_methods([Method::GET])
.allow_origin(origins);
routes = routes.layer(cors)
};

#[cfg(feature = "prometheus")]
let routes = routes
Expand Down
1 change: 1 addition & 0 deletions tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub fn start_server(db: Database) -> Result<SocketAddr, NError> {
// this ensure there would not be conflicts with other server instances started by other
// tests
port: 0,
cors_allow_origins: vec![],
};

let (socket, server) = create_server(db, &config)?;
Expand Down

0 comments on commit 8ff28e6

Please sign in to comment.