|
3 | 3 | //! Starts the dev server with HMR and Forge Studio. |
4 | 4 | //! Listens on port 3000 (app) and port 3001 (Studio) by default. |
5 | 5 |
|
| 6 | +use crate::dev::hot_reload::{hmr_router, HmrMessage, HmrState}; |
6 | 7 | use crate::error::RuntimeError; |
| 8 | +use axum::Router; |
| 9 | +use camino::Utf8PathBuf; |
| 10 | +use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher}; |
| 11 | +use tokio::net::TcpListener; |
| 12 | +use tokio::sync::mpsc; |
| 13 | +use tracing::{error, info}; |
| 14 | + |
| 15 | +/// Configuration for the development server. |
| 16 | +#[derive(Debug, Clone)] |
| 17 | +pub struct DevServerConfig { |
| 18 | + /// Host to bind to (default: 127.0.0.1) |
| 19 | + pub host: String, |
| 20 | + /// Port for the dev server (default: 3000) |
| 21 | + pub port: u16, |
| 22 | + /// Port for Forge Studio (default: 3001) |
| 23 | + pub studio_port: u16, |
| 24 | + /// The project root directory |
| 25 | + pub project_root: Utf8PathBuf, |
| 26 | +} |
| 27 | + |
| 28 | +impl Default for DevServerConfig { |
| 29 | + fn default() -> Self { |
| 30 | + Self { |
| 31 | + host: "127.0.0.1".to_string(), |
| 32 | + port: 3000, |
| 33 | + studio_port: 3001, |
| 34 | + project_root: Utf8PathBuf::from("."), |
| 35 | + } |
| 36 | + } |
| 37 | +} |
7 | 38 |
|
8 | 39 | /// Start the development server. |
9 | | -pub async fn start_dev_server() -> Result<(), RuntimeError> { |
10 | | - // TODO: Initialize file watcher, incremental compiler, HMR, and Studio |
| 40 | +pub async fn start_dev_server(config: DevServerConfig) -> Result<(), RuntimeError> { |
| 41 | + let hmr_state = HmrState::new(); |
| 42 | + |
| 43 | + // 1. Setup file watcher |
| 44 | + let (tx, mut rx) = mpsc::channel(100); |
| 45 | + |
| 46 | + let mut watcher = RecommendedWatcher::new( |
| 47 | + move |res| { |
| 48 | + if let Err(e) = tx.try_send(res) { |
| 49 | + tracing::warn!("Failed to send watcher event: {}", e); |
| 50 | + } |
| 51 | + }, |
| 52 | + Config::default(), |
| 53 | + ) |
| 54 | + .map_err(|e| RuntimeError::Internal(format!("Failed to initialize watcher: {}", e)))?; |
| 55 | + |
| 56 | + // Watch relevant directories |
| 57 | + let watch_dirs = ["app", "public", "src"]; |
| 58 | + let mut watching_any = false; |
| 59 | + for dir in watch_dirs { |
| 60 | + let path = config.project_root.join(dir); |
| 61 | + if path.exists() { |
| 62 | + watcher |
| 63 | + .watch(path.as_std_path(), RecursiveMode::Recursive) |
| 64 | + .map_err(|e| { |
| 65 | + RuntimeError::Internal(format!("Failed to watch {} directory: {}", dir, e)) |
| 66 | + })?; |
| 67 | + info!("Watching {} for changes", path); |
| 68 | + watching_any = true; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if !watching_any { |
| 73 | + error!( |
| 74 | + "No recognizable source directories (app, public, src) found in {}", |
| 75 | + config.project_root |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + // Spawn a background task to process file watcher events and trigger HMR |
| 80 | + let hmr_state_clone = hmr_state.clone(); |
| 81 | + tokio::spawn(async move { |
| 82 | + while let Some(res) = rx.recv().await { |
| 83 | + match res { |
| 84 | + Ok(event) => { |
| 85 | + // For now, we broadcast a reload on any change. |
| 86 | + // Later, this will trigger the incremental compiler and only push updates for changed modules. |
| 87 | + if event.kind.is_modify() || event.kind.is_create() || event.kind.is_remove() { |
| 88 | + info!("File changed, triggering HMR reload"); |
| 89 | + hmr_state_clone.broadcast(HmrMessage::Reload); |
| 90 | + } |
| 91 | + } |
| 92 | + Err(e) => error!("Watch error: {:?}", e), |
| 93 | + } |
| 94 | + } |
| 95 | + // Keep watcher alive by moving it into this task |
| 96 | + let _watcher = watcher; |
| 97 | + }); |
| 98 | + |
| 99 | + // 2. Main App Server (incorporates HMR router) |
| 100 | + // TODO: Combine with the actual app router |
| 101 | + let app = hmr_router(hmr_state.clone()); |
| 102 | + let addr = format!("{}:{}", config.host, config.port); |
| 103 | + let listener = TcpListener::bind(&addr).await.map_err(RuntimeError::Io)?; |
| 104 | + |
| 105 | + // 3. Studio Server |
| 106 | + let studio_app = Router::new().route( |
| 107 | + "/", |
| 108 | + axum::routing::get(|| async { "Forge Studio (Coming soon)" }), |
| 109 | + ); |
| 110 | + let studio_addr = format!("{}:{}", config.host, config.studio_port); |
| 111 | + let studio_listener = TcpListener::bind(&studio_addr) |
| 112 | + .await |
| 113 | + .map_err(RuntimeError::Io)?; |
| 114 | + |
| 115 | + info!("Dev server listening on {}", addr); |
| 116 | + info!("Forge Studio listening on {}", studio_addr); |
| 117 | + |
| 118 | + // Run both servers concurrently |
| 119 | + tokio::try_join!( |
| 120 | + async { |
| 121 | + axum::serve(listener, app) |
| 122 | + .await |
| 123 | + .map_err(|e| RuntimeError::Http(e.to_string())) |
| 124 | + }, |
| 125 | + async { |
| 126 | + axum::serve(studio_listener, studio_app) |
| 127 | + .await |
| 128 | + .map_err(|e| RuntimeError::Http(e.to_string())) |
| 129 | + } |
| 130 | + )?; |
| 131 | + |
11 | 132 | Ok(()) |
12 | 133 | } |
0 commit comments