Skip to content

Commit 30ce451

Browse files
committed
gateway: support cors
Signed-off-by: Élyse Viard <[email protected]>
1 parent ca256f7 commit 30ce451

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

docker/gateway.dev.simple.toml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
secret_key = "NOT+A+SECRET++NOT+A+SECRET++NOT+A+SECRET++NOT+A+SECRET++NOT+A+SECRET++NOT+A+SECRET++NOT+A+SECRET"
66

77
listen_addr = "0.0.0.0"
8+
allowed_origins = ["http://localhost:3000"]
89

910
[telemetry.tracing]
1011
enable = true

gateway/Cargo.lock

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gateway/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ smallvec = "1.13.2"
2222
thiserror = "1.0.69"
2323

2424
# main crate
25+
actix-cors = "0.7.0"
2526
actix-files = "0.6"
2627
actix-session = "0.9"
2728
actix-web = "4.9"
@@ -71,6 +72,7 @@ humantime-serde.workspace = true
7172
serde.workspace = true
7273

7374
# web server
75+
actix-cors.workspace = true
7476
actix-files.workspace = true
7577
actix-session = { workspace = true, features = ["cookie-session"] }
7678
actix-web.workspace = true

gateway/src/config.rs

+3
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ pub struct ProxyConfig {
173173
pub auth: AuthConfig,
174174
/// Telemetry configuration
175175
pub telemetry: Telemetry,
176+
/// Allowed origins for CORS (when empty, CORS is disabled entirely)
177+
pub allowed_origins: Option<Vec<String>>,
176178
}
177179

178180
#[derive(Deserialize, Serialize, Clone)]
@@ -199,6 +201,7 @@ impl Default for ProxyConfig {
199201
telemetry: Telemetry {
200202
tracing: TracingTelemetry::None,
201203
},
204+
allowed_origins: None,
202205
}
203206
}
204207
}

gateway/src/main.rs

+14
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ async fn main() -> std::io::Result<()> {
6161
// Enable telemetry
6262
config.telemetry.enable();
6363

64+
// CORS configuration
65+
let allowed_origins = config.allowed_origins.clone();
66+
6467
// Start server
6568
HttpServer::new(move || {
6669
let session_middleware =
@@ -71,7 +74,18 @@ async fn main() -> std::io::Result<()> {
7174
.cookie_name("gateway".to_string())
7275
.build();
7376

77+
let cors = if let Some(allowed_origins) = &allowed_origins {
78+
let mut cors = actix_cors::Cors::default();
79+
for origin in allowed_origins {
80+
cors = cors.allowed_origin(origin);
81+
}
82+
cors.allow_any_method().allow_any_header().max_age(3600)
83+
} else {
84+
actix_cors::Cors::default()
85+
};
86+
7487
let mut app = App::new()
88+
.wrap(cors)
7589
.wrap(RequestTracing::new())
7690
.wrap(Compress::default()) // enable compress
7791
.route("/health", web::get().to(|| async { "OK" }))

0 commit comments

Comments
 (0)