(path: Option<PathBuf>, port: u16, reload: bool)
| 65 | } |
| 66 | |
| 67 | pub async fn run(path: Option<PathBuf>, port: u16, reload: bool) { |
| 68 | if !reload { |
| 69 | // Run without reload functionality |
| 70 | run_server(path, port, None).await; |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | // Create a channel for reload coordination |
| 75 | let (tx, _) = broadcast::channel::<ReloadSignal>(1); |
| 76 | let tx = Arc::new(tx); |
| 77 | |
| 78 | // Set up file watcher |
| 79 | let watcher_tx = tx.clone(); |
| 80 | let mut watcher = setup_watcher(move |event| { |
| 81 | // Only trigger reload for .ai files |
| 82 | if let Some(path) = event.paths.first().and_then(|p| p.to_str()) { |
| 83 | if path.ends_with(".ai") { |
| 84 | watcher_tx.send(ReloadSignal).unwrap(); |
| 85 | } |
| 86 | } |
| 87 | }) |
| 88 | .expect("Failed to setup watcher"); |
| 89 | |
| 90 | // Watch the routes directory |
| 91 | watcher |
| 92 | .watch(Path::new("routes"), RecursiveMode::Recursive) |
| 93 | .expect("Failed to watch routes directory"); |
| 94 | |
| 95 | loop { |
| 96 | let mut rx = tx.subscribe(); |
| 97 | let server_handle = tokio::spawn(run_server(path.clone(), port, Some(rx.resubscribe()))); |
| 98 | |
| 99 | // Wait for reload signal |
| 100 | match rx.recv().await { |
| 101 | Ok(_) => { |
| 102 | println!("📑 Routes changed, reloading server..."); |
| 103 | // Give some time for pending requests to complete |
| 104 | tokio::time::sleep(Duration::from_millis(100)).await; |
| 105 | server_handle.abort(); |
| 106 | } |
| 107 | Err(_) => { |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | fn setup_watcher<F>(mut callback: F) -> notify::Result<RecommendedWatcher> |
| 115 | where |
no test coverage detected