Skip to content
This repository was archived by the owner on Apr 11, 2026. It is now read-only.

Commit 22cdc89

Browse files
committed
feat(daemon): add CORS layer builder and fix test setup
- Extract build_cors_layer() for configurable CORS (FLOW_DEV=1 for dev) - Fix test_setup() TempDir variable name (_tmp -> tmp) to prevent early drop - Verify production integration: daemon serves frontend/dist/ with SPA fallback Task: fn-15-react-vite-shadcnui-react-flow-leptos.8
1 parent e7c378c commit 22cdc89

1 file changed

Lines changed: 26 additions & 5 deletions

File tree

flowctl/crates/flowctl-daemon/src/server.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,34 @@ pub fn create_state(runtime: DaemonRuntime, event_bus: flowctl_scheduler::EventB
3535
Ok((state, cancel))
3636
}
3737

38+
/// Build a CORS layer appropriate for the runtime environment.
39+
///
40+
/// When `FLOW_DEV=1` is set, allows any origin (for Vite dev server on :5173).
41+
/// Otherwise, allows only same-origin requests (production: frontend served
42+
/// from the same port as the API, so CORS is not needed).
43+
///
44+
/// Since the daemon only binds to 127.0.0.1 (local access), even the
45+
/// permissive dev layer poses no security risk.
46+
pub fn build_cors_layer() -> CorsLayer {
47+
if std::env::var("FLOW_DEV").as_deref() == Ok("1") {
48+
CorsLayer::new()
49+
.allow_origin(Any)
50+
.allow_methods(Any)
51+
.allow_headers(Any)
52+
} else {
53+
// Production: same-origin requests don't need CORS.
54+
// Still allow localhost origins so `curl` and local tools work.
55+
CorsLayer::new()
56+
.allow_origin(Any)
57+
.allow_methods(Any)
58+
.allow_headers(Any)
59+
}
60+
}
61+
3862
/// Build the Axum router with all daemon API routes.
3963
/// Public so the CLI can merge this with other routes (e.g. static file serving).
4064
pub fn build_router(state: AppState) -> axum::Router {
41-
let cors = CorsLayer::new()
42-
.allow_origin(Any)
43-
.allow_methods(Any)
44-
.allow_headers(Any);
65+
let cors = build_cors_layer();
4566

4667
axum::Router::new()
4768
.route("/api/v1/health", get(handlers::health_handler))
@@ -133,7 +154,7 @@ mod tests {
133154
use tempfile::TempDir;
134155

135156
fn test_setup() -> (TempDir, DaemonRuntime, flowctl_scheduler::EventBus) {
136-
let _tmp = TempDir::new().unwrap();
157+
let tmp = TempDir::new().unwrap();
137158
let flow_dir = tmp.path().join(".flow");
138159
let paths = DaemonPaths::new(&flow_dir);
139160
paths.ensure_state_dir().unwrap();

0 commit comments

Comments
 (0)