Skip to content

Commit dbcf752

Browse files
feat: add --cors flag to enable cors for workers (#202)
1 parent 22a9816 commit dbcf752

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

crates/server/src/handlers/worker.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ use super::{assets::handle_assets, not_found::handle_not_found};
55
use crate::DataConnectors;
66
use actix_web::{
77
http::StatusCode,
8-
web::{Bytes, Data},
8+
web::{self, Bytes, Data},
99
HttpRequest, HttpResponse,
1010
};
1111
use std::{fs::File, io::Write, sync::RwLock};
1212
use wws_router::Routes;
1313
use wws_worker::io::WasmOutput;
1414

15+
const CORS_HEADER: &str = "Access-Control-Allow-Origin";
16+
1517
/// Process an HTTP request by passing it to the right Runner. The Runner
1618
/// will prepare the WASI environment and call the Wasm module with the data.
1719
///
@@ -29,7 +31,11 @@ use wws_worker::io::WasmOutput;
2931
///
3032
/// For these reasons, we are selecting the right handler at this point and not
3133
/// allowing Actix to select it for us.
32-
pub async fn handle_worker(req: HttpRequest, body: Bytes) -> HttpResponse {
34+
pub async fn handle_worker(
35+
req: HttpRequest,
36+
body: Bytes,
37+
cors_origins: web::Data<Option<Vec<String>>>,
38+
) -> HttpResponse {
3339
let routes = req.app_data::<Data<Routes>>().unwrap();
3440
let stderr_file = req.app_data::<Data<Option<File>>>().unwrap();
3541
let data_connectors = req
@@ -97,6 +103,16 @@ pub async fn handle_worker(req: HttpRequest, body: Bytes) -> HttpResponse {
97103
// Default content type
98104
builder.insert_header(("Content-Type", "text/html"));
99105

106+
// Check if cors config has any origins to register
107+
if let Some(origins) = cors_origins.as_ref() {
108+
// Check if worker has overridden the header, if not
109+
if !handler_result.headers.contains_key(CORS_HEADER) {
110+
// insert those origins in 'Access-Control-Allow-Origin' header
111+
let header_value = origins.join(",");
112+
builder.insert_header((CORS_HEADER, header_value));
113+
}
114+
}
115+
100116
for (key, val) in handler_result.headers.iter() {
101117
// Note that QuickJS is replacing the "-" character
102118
// with "_" on property keys. Here, we rollback it

crates/server/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub async fn serve(
3939
port: u16,
4040
panel: bool,
4141
stderr: Option<&Path>,
42+
cors_origins: Option<Vec<String>>,
4243
) -> Result<Server> {
4344
// Initializes the data connectors. For now, just KV
4445
let data = Data::new(RwLock::new(DataConnectors::default()));
@@ -59,6 +60,8 @@ pub async fn serve(
5960
stderr_file = Data::new(None);
6061
}
6162

63+
let cors_data = Data::new(cors_origins);
64+
6265
let server = HttpServer::new(move || {
6366
let mut app = App::new()
6467
// enable logger
@@ -68,7 +71,8 @@ pub async fn serve(
6871
.app_data(Data::clone(&routes))
6972
.app_data(Data::clone(&data))
7073
.app_data(Data::clone(&root_path))
71-
.app_data(Data::clone(&stderr_file));
74+
.app_data(Data::clone(&stderr_file))
75+
.app_data(Data::clone(&cors_data));
7276

7377
// Configure panel
7478
if panel {

src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ pub struct Args {
6868
/// Manage language runtimes in your project
6969
#[command(subcommand)]
7070
commands: Option<Main>,
71+
72+
/// CORS headers to add to all workers if not already set by the worker
73+
#[arg(long)]
74+
cors: Option<Vec<String>>,
7175
}
7276

7377
#[actix_web::main]
@@ -198,6 +202,7 @@ async fn main() -> std::io::Result<()> {
198202
args.port,
199203
args.enable_panel,
200204
None,
205+
args.cors,
201206
)
202207
.await
203208
.map_err(|err| Error::new(ErrorKind::AddrInUse, err))?;

0 commit comments

Comments
 (0)