|
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 | + /// Port for the dev server (default: 3000) |
| 19 | + pub port: u16, |
| 20 | + /// Port for Forge Studio (default: 3001) |
| 21 | + pub studio_port: u16, |
| 22 | + /// The project root directory |
| 23 | + pub project_root: Utf8PathBuf, |
| 24 | +} |
| 25 | + |
| 26 | +impl Default for DevServerConfig { |
| 27 | + fn default() -> Self { |
| 28 | + Self { |
| 29 | + port: 3000, |
| 30 | + studio_port: 3001, |
| 31 | + project_root: Utf8PathBuf::from("."), |
| 32 | + } |
| 33 | + } |
| 34 | +} |
7 | 35 |
|
8 | 36 | /// Start the development server. |
9 | | -pub async fn start_dev_server(_port: u16, _studio_port: u16) -> Result<(), RuntimeError> { |
10 | | - // TODO: Initialize file watcher, incremental compiler, HMR, and Studio |
| 37 | +pub async fn start_dev_server(config: DevServerConfig) -> Result<(), RuntimeError> { |
| 38 | + let hmr_state = HmrState::new(); |
| 39 | + |
| 40 | + // 1. Setup file watcher |
| 41 | + let (tx, mut rx) = mpsc::channel(100); |
| 42 | + |
| 43 | + let mut watcher = RecommendedWatcher::new( |
| 44 | + move |res| { |
| 45 | + let _ = tx.blocking_send(res); |
| 46 | + }, |
| 47 | + Config::default(), |
| 48 | + ) |
| 49 | + .map_err(|e| RuntimeError::Internal(format!("Failed to initialize watcher: {}", e)))?; |
| 50 | + |
| 51 | + // Watch the src directory |
| 52 | + let src_dir = config.project_root.join("src"); |
| 53 | + if src_dir.exists() { |
| 54 | + watcher |
| 55 | + .watch(src_dir.as_std_path(), RecursiveMode::Recursive) |
| 56 | + .map_err(|e| RuntimeError::Internal(format!("Failed to watch src directory: {}", e)))?; |
| 57 | + info!("Watching {} for changes", src_dir); |
| 58 | + } else { |
| 59 | + error!("src directory not found in {}", config.project_root); |
| 60 | + } |
| 61 | + |
| 62 | + // Spawn a background task to process file watcher events and trigger HMR |
| 63 | + let hmr_state_clone = hmr_state.clone(); |
| 64 | + tokio::spawn(async move { |
| 65 | + while let Some(res) = rx.recv().await { |
| 66 | + match res { |
| 67 | + Ok(event) => { |
| 68 | + // For now, we broadcast a reload on any change. |
| 69 | + // Later, this will trigger the incremental compiler and only push updates for changed modules. |
| 70 | + if event.kind.is_modify() || event.kind.is_create() || event.kind.is_remove() { |
| 71 | + info!("File changed, triggering HMR reload"); |
| 72 | + hmr_state_clone.broadcast(HmrMessage::Reload); |
| 73 | + } |
| 74 | + } |
| 75 | + Err(e) => error!("Watch error: {:?}", e), |
| 76 | + } |
| 77 | + } |
| 78 | + // Keep watcher alive by moving it into this task |
| 79 | + let _watcher = watcher; |
| 80 | + }); |
| 81 | + |
| 82 | + // 2. Main App Server (incorporates HMR router) |
| 83 | + // TODO: Combine with the actual app router |
| 84 | + let app = hmr_router(hmr_state.clone()); |
| 85 | + let addr = format!("0.0.0.0:{}", config.port); |
| 86 | + let listener = TcpListener::bind(&addr).await.map_err(RuntimeError::Io)?; |
| 87 | + |
| 88 | + // 3. Studio Server |
| 89 | + let studio_app = Router::new().route( |
| 90 | + "/", |
| 91 | + axum::routing::get(|| async { "Forge Studio (Coming soon)" }), |
| 92 | + ); |
| 93 | + let studio_addr = format!("0.0.0.0:{}", config.studio_port); |
| 94 | + let studio_listener = TcpListener::bind(&studio_addr) |
| 95 | + .await |
| 96 | + .map_err(RuntimeError::Io)?; |
| 97 | + |
| 98 | + info!("Dev server listening on {}", addr); |
| 99 | + info!("Forge Studio listening on {}", studio_addr); |
| 100 | + |
| 101 | + // Run both servers concurrently |
| 102 | + tokio::try_join!( |
| 103 | + async { |
| 104 | + axum::serve(listener, app) |
| 105 | + .await |
| 106 | + .map_err(|e| RuntimeError::Http(e.to_string())) |
| 107 | + }, |
| 108 | + async { |
| 109 | + axum::serve(studio_listener, studio_app) |
| 110 | + .await |
| 111 | + .map_err(|e| RuntimeError::Http(e.to_string())) |
| 112 | + } |
| 113 | + )?; |
| 114 | + |
11 | 115 | Ok(()) |
12 | 116 | } |
0 commit comments