From 7ac3c51c1bf686c766a0c1c51e4b54e81957f321 Mon Sep 17 00:00:00 2001 From: axiomatic-aardvark Date: Thu, 12 Oct 2023 17:51:39 -0500 Subject: [PATCH] feat: enable cors --- .gitignore | 3 ++- Cargo.lock | 2 ++ subgraph-radio/Cargo.toml | 2 ++ subgraph-radio/src/server/mod.rs | 13 +++++++++++-- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 7573099..05b48d6 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,5 @@ data configs node_modules *.json -!schema.json \ No newline at end of file +!schema.json +dist diff --git a/Cargo.lock b/Cargo.lock index 9b86304..abd4881 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5866,6 +5866,7 @@ dependencies = [ "graphcast-sdk", "graphql_client 0.12.0", "hex", + "http", "itertools 0.11.0", "metrics 0.21.1", "num-traits", @@ -5883,6 +5884,7 @@ dependencies = [ "sha3", "thiserror", "tokio", + "tower-http 0.4.4", "tracing", "tracing-opentelemetry", "tracing-subscriber", diff --git a/subgraph-radio/Cargo.toml b/subgraph-radio/Cargo.toml index d61e0b3..4b1bd9c 100644 --- a/subgraph-radio/Cargo.toml +++ b/subgraph-radio/Cargo.toml @@ -55,6 +55,8 @@ opentelemetry = { version = "0.19.0", features = ["rt-tokio", "trace"] } tracing-opentelemetry = "0.18.0" clap = { version = "4.3.1", features = ["derive", "env"] } confy = "0.5.1" +tower-http = { version = "0.4.3", features = ["cors"] } +http = "0.2.6" [dev-dependencies] criterion = { version = "0.4", features = ["async", "async_futures"] } diff --git a/subgraph-radio/src/server/mod.rs b/subgraph-radio/src/server/mod.rs index 8c3cfcf..edfcc4f 100644 --- a/subgraph-radio/src/server/mod.rs +++ b/subgraph-radio/src/server/mod.rs @@ -1,9 +1,11 @@ +use axum::http::Method; use axum::{extract::Extension, routing::get, Router}; use axum_server::Handle; +use http::header::HeaderName; use std::net::SocketAddr; use std::str::FromStr; - use std::sync::Arc; +use tower_http::cors::{Any, CorsLayer}; use tracing::{debug, info}; use crate::{ @@ -33,6 +35,11 @@ pub async fn run_server(config: Config, persisted_state: &'static PersistedState debug!("Setting up HTTP service"); + let cors = CorsLayer::new() + .allow_methods(vec![Method::GET, Method::POST]) + .allow_origin(Any) + .allow_headers(vec![HeaderName::from_static("content-type")]); + let app = Router::new() .route("/health", get(health)) .route( @@ -40,7 +47,9 @@ pub async fn run_server(config: Config, persisted_state: &'static PersistedState get(graphql_playground).post(graphql_handler), ) .layer(Extension(schema)) - .layer(Extension(context)); + .layer(Extension(context)) + .layer(cors); + let addr = SocketAddr::from_str(&format!( "{}:{}", config.radio_infrastructure().server_host,