Skip to content

Commit

Permalink
chore: add support for CORS in gRPC-web (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalezzfelipe authored Oct 7, 2024
1 parent 4a8d319 commit b0b1486
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ console-subscriber = { version = "0.3.0", optional = true }
flate2 = "1.0.33"
tar = "0.4.41"
reqwest = { version = "0.12.7", features = ["blocking"] }
tower-http = "0.4.4"

[dev-dependencies]
tempfile = "3.3.0"
Expand Down
1 change: 1 addition & 0 deletions src/bin/dolos/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ impl ConfigEditor {
self.0.serve.grpc = dolos::serve::grpc::Config {
listen_address: "[::]:50051".into(),
tls_client_ca_root: None,
cors_enabled: true,
}
.into();
} else {
Expand Down
9 changes: 8 additions & 1 deletion src/serve/grpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use tokio_util::sync::CancellationToken;
use tonic::transport::{Certificate, Server, ServerTlsConfig};
use tower_http::cors::CorsLayer;
use tracing::info;

use crate::mempool::Mempool;
Expand All @@ -22,6 +23,7 @@ mod watch;
pub struct Config {
pub listen_address: String,
pub tls_client_ca_root: Option<PathBuf>,
pub cors_enabled: bool,
}

pub async fn serve(
Expand Down Expand Up @@ -57,7 +59,12 @@ pub async fn serve(
.build()
.unwrap();

let mut server = Server::builder().accept_http1(true);
let cors_layer = if config.cors_enabled {
CorsLayer::permissive()
} else {
CorsLayer::new()
};
let mut server = Server::builder().accept_http1(true).layer(cors_layer);

if let Some(pem) = config.tls_client_ca_root {
let pem = std::env::current_dir().unwrap().join(pem);
Expand Down

0 comments on commit b0b1486

Please sign in to comment.