MCPcopy Index your code
hub / github.com/aiscriptdev/aiscript / run

Function run

aiscript-runtime/src/lib.rs:67–112  ·  view source on GitHub ↗
(path: Option<PathBuf>, port: u16, reload: bool)

Source from the content-addressed store, hash-verified

65}
66
67pub 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
114fn setup_watcher<F>(mut callback: F) -> notify::Result<RecommendedWatcher>
115where

Callers 1

mainFunction · 0.85

Calls 4

run_serverFunction · 0.85
setup_watcherFunction · 0.85
to_strMethod · 0.80
cloneMethod · 0.45

Tested by

no test coverage detected