1mod api;
9mod assets;
10mod registry;
11mod watch;
12
13use std::net::{Ipv4Addr, SocketAddr};
14use std::path::PathBuf;
15use std::time::Duration;
16
17use axum::routing::{get, patch, post, put};
18use axum::Router;
19use tokio::sync::broadcast;
20use tower_http::cors::CorsLayer;
21
22use wipe_core::model::Exposure;
23
24pub use api::AppState;
25pub use registry::{list as list_projects, ProjectEntry};
26
27#[derive(Debug, Clone)]
29pub struct ServeConfig {
30 pub root: Option<PathBuf>,
34 pub port: u16,
36 pub expose: Exposure,
38 pub open: bool,
40 pub idle_timeout: Option<std::time::Duration>,
43}
44
45fn router(state: AppState) -> Router {
47 Router::new()
48 .route("/api/health", get(api::health))
49 .route("/api/config", get(api::app_config))
50 .route("/api/projects", get(api::projects))
51 .route("/api/board", get(api::board))
52 .route("/api/history", get(api::history))
53 .route("/api/board/at", get(api::board_at))
54 .route("/api/definitions", get(api::definitions))
55 .route("/api/graph", get(api::graph))
56 .route("/api/labels", post(api::create_label))
57 .route(
58 "/api/labels/{name}",
59 patch(api::recolor_label).delete(api::delete_label),
60 )
61 .route("/api/lists", post(api::add_list))
62 .route(
63 "/api/lists/{id}",
64 patch(api::rename_list).delete(api::remove_list),
65 )
66 .route("/api/lists/{id}/move", post(api::move_list))
67 .route("/api/identities", get(api::identities))
68 .route(
69 "/api/identities/{id}",
70 put(api::put_identity).delete(api::delete_identity),
71 )
72 .route("/api/tickets", post(api::create_ticket))
73 .route("/api/tickets/{id}", patch(api::patch_ticket))
74 .route("/api/tickets/{id}/move", post(api::move_ticket))
75 .route("/api/tickets/{id}/comments", post(api::add_comment))
76 .route(
77 "/api/tickets/{id}/attachments",
78 post(api::upload_attachment).delete(api::delete_attachment),
79 )
80 .route("/api/media/{*path}", get(api::serve_media))
81 .route("/api/forum", get(api::forum_list).post(api::forum_create))
82 .route("/api/forum/search", get(api::forum_search))
83 .route(
84 "/api/forum/{id}",
85 get(api::forum_thread)
86 .patch(api::forum_edit)
87 .delete(api::forum_delete),
88 )
89 .route("/api/forum/{id}/reply", post(api::forum_reply))
90 .route("/ws", get(api::ws_handler))
91 .fallback(assets::static_handler)
92 .layer(CorsLayer::permissive())
93 .with_state(state)
94}
95
96pub async fn serve(cfg: ServeConfig) -> anyhow::Result<()> {
98 if let Some(root) = &cfg.root {
99 registry::register(root);
100 }
101
102 let (tx, _rx) = broadcast::channel::<String>(64);
103 let clients = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
104 let state = AppState {
105 current: cfg.root.clone(),
106 tx: tx.clone(),
107 clients: clients.clone(),
108 };
109
110 let _watcher = cfg
114 .root
115 .as_ref()
116 .map(|root| watch::spawn(&root.join(".wipe"), tx.clone()));
117 if matches!(_watcher, Some(Err(_))) {
118 eprintln!("warning: file watching unavailable; live updates disabled");
119 }
120
121 let ip = match cfg.expose {
122 Exposure::None => Ipv4Addr::LOCALHOST,
123 Exposure::Tailscale | Exposure::Proxy => Ipv4Addr::UNSPECIFIED,
124 };
125 let addr = SocketAddr::from((ip, cfg.port));
126 let listener = tokio::net::TcpListener::bind(addr).await?;
127 let bound = listener.local_addr()?;
128
129 let shown = if bound.ip().is_unspecified() {
130 SocketAddr::from((Ipv4Addr::LOCALHOST, bound.port()))
131 } else {
132 bound
133 };
134 match cfg.idle_timeout {
135 Some(d) => println!(
136 "wipe UI serving on http://{shown} (Ctrl-C to stop; auto-stops after {}s idle)",
137 d.as_secs()
138 ),
139 None => println!("wipe UI serving on http://{shown} (Ctrl-C to stop)"),
140 }
141 if cfg.open {
142 open_browser(&format!("http://{shown}"));
143 }
144
145 let app = router(state);
146 let idle = cfg.idle_timeout;
147 axum::serve(listener, app)
148 .with_graceful_shutdown(async move { shutdown_signal(clients, idle).await })
149 .await?;
150 Ok(())
151}
152
153async fn shutdown_signal(
156 clients: std::sync::Arc<std::sync::atomic::AtomicUsize>,
157 idle: Option<Duration>,
158) {
159 tokio::select! {
160 _ = async { let _ = tokio::signal::ctrl_c().await; } => {}
161 _ = idle_watcher(clients, idle) => {
162 println!("wipe: idle with no viewers; shutting down.");
163 }
164 }
165}
166
167async fn idle_watcher(
170 clients: std::sync::Arc<std::sync::atomic::AtomicUsize>,
171 timeout: Option<Duration>,
172) {
173 use std::sync::atomic::Ordering;
174 let Some(timeout) = timeout else {
175 std::future::pending::<()>().await;
176 return;
177 };
178 let mut idle_since = Some(std::time::Instant::now());
179 let mut tick = tokio::time::interval(Duration::from_secs(5));
180 loop {
181 tick.tick().await;
182 if clients.load(Ordering::SeqCst) > 0 {
183 idle_since = None;
184 } else {
185 let since = idle_since.get_or_insert_with(std::time::Instant::now);
186 if since.elapsed() >= timeout {
187 return;
188 }
189 }
190 }
191}
192
193fn open_browser(url: &str) {
195 #[cfg(target_os = "windows")]
196 let _ = std::process::Command::new("cmd")
197 .args(["/C", "start", "", url])
198 .spawn();
199 #[cfg(target_os = "macos")]
200 let _ = std::process::Command::new("open").arg(url).spawn();
201 #[cfg(all(unix, not(target_os = "macos")))]
202 let _ = std::process::Command::new("xdg-open").arg(url).spawn();
203}
204
205#[cfg(test)]
206mod tests {
207 use super::*;
208 use axum::body::Body;
209 use axum::http::{Request, StatusCode};
210 use tower::ServiceExt; fn test_state(root: PathBuf) -> AppState {
213 let (tx, _rx) = broadcast::channel(8);
214 AppState {
215 current: Some(root),
216 tx,
217 clients: std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0)),
218 }
219 }
220
221 #[tokio::test]
222 async fn health_and_board_endpoints() {
223 let dir = tempfile::tempdir().unwrap();
224 let store = Store::init(dir.path(), "Daemon Test", chrono::Utc::now()).unwrap();
225 wipe_core::ops::create_ticket(
226 &store,
227 wipe_core::ops::NewTicket {
228 title: "Hello".into(),
229 ..Default::default()
230 },
231 "tester",
232 chrono::Utc::now(),
233 )
234 .unwrap();
235
236 let app = router(test_state(store.root().to_path_buf()));
237
238 let health = app
239 .clone()
240 .oneshot(
241 Request::builder()
242 .uri("/api/health")
243 .body(Body::empty())
244 .unwrap(),
245 )
246 .await
247 .unwrap();
248 assert_eq!(health.status(), StatusCode::OK);
249
250 let board = app
251 .oneshot(
252 Request::builder()
253 .uri("/api/board")
254 .body(Body::empty())
255 .unwrap(),
256 )
257 .await
258 .unwrap();
259 assert_eq!(board.status(), StatusCode::OK);
260 let bytes = axum::body::to_bytes(board.into_body(), 1 << 20)
261 .await
262 .unwrap();
263 let v: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
264 assert_eq!(v["board"], "Daemon Test");
265 assert_eq!(v["lists"][0]["tickets"][0]["title"], "Hello");
266 }
267
268 use wipe_core::Store;
269
270 fn enc(s: &str) -> String {
272 s.bytes()
273 .map(|b| match b {
274 b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
275 (b as char).to_string()
276 }
277 _ => format!("%{b:02X}"),
278 })
279 .collect()
280 }
281
282 async fn board_titles(app: &Router, project: &str) -> Vec<String> {
283 let res = app
284 .clone()
285 .oneshot(
286 Request::builder()
287 .uri(format!("/api/board?project={}", enc(project)))
288 .body(Body::empty())
289 .unwrap(),
290 )
291 .await
292 .unwrap();
293 let bytes = axum::body::to_bytes(res.into_body(), 1 << 20)
294 .await
295 .unwrap();
296 let v: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
297 v["lists"]
298 .as_array()
299 .unwrap()
300 .iter()
301 .flat_map(|l| l["tickets"].as_array().unwrap().clone())
302 .map(|t| t["title"].as_str().unwrap().to_string())
303 .collect()
304 }
305
306 #[tokio::test]
309 async fn mutations_target_the_requested_project_not_the_served_one() {
310 let served = tempfile::tempdir().unwrap();
311 let other = tempfile::tempdir().unwrap();
312 let served_store = Store::init(served.path(), "Served", chrono::Utc::now()).unwrap();
313 let other_store = Store::init(other.path(), "Other", chrono::Utc::now()).unwrap();
314 let other_root = other_store.root().display().to_string();
315
316 let app = router(test_state(served_store.root().to_path_buf()));
318
319 let res = app
321 .clone()
322 .oneshot(
323 Request::builder()
324 .method("POST")
325 .uri(format!("/api/tickets?project={}", enc(&other_root)))
326 .header("content-type", "application/json")
327 .body(Body::from(r#"{"title":"lands in other"}"#))
328 .unwrap(),
329 )
330 .await
331 .unwrap();
332 assert_eq!(res.status(), StatusCode::OK);
333
334 assert_eq!(
336 board_titles(&app, &other_root).await,
337 vec!["lands in other".to_string()]
338 );
339 assert!(
340 board_titles(&app, &served_store.root().display().to_string())
341 .await
342 .is_empty()
343 );
344 }
345}