rejoice 0.12.0

A simple and delightful little web framework for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
use super::boilerplate::maybe_generate_boilerplate;
use super::islands::{generate_islands_registry, generate_vite_config, has_island_components};
use super::style;
use axum::{
    Router,
    extract::ws::{Message, WebSocket, WebSocketUpgrade},
    routing::get,
};
use colored::Colorize;
use futures::{SinkExt, StreamExt};
use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
use std::path::Path;
use std::process::{Child, Command, Stdio};
use std::sync::Arc;
use std::sync::atomic::{AtomicU32, Ordering};
use std::time::{Duration, Instant};
use tokio::sync::broadcast;

// Global to track the child process ID for signal handler
static CHILD_PID: AtomicU32 = AtomicU32::new(0);

fn run_bun_command(args: &[&str]) -> std::io::Result<std::process::ExitStatus> {
    Command::new("bun")
        .args(args)
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit())
        .status()
}

pub fn dev_command() {
    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(run_dev_server());
}

async fn run_dev_server() {
    style::print_banner();
    println!("\n  {}\n", "Starting development server...".dimmed());

    let client_dir = Path::new("client");
    let has_client = client_dir.exists();

    if has_client {
        setup_client_build();
    }

    let reload_tx = Arc::new(broadcast::channel::<&'static str>(16).0);

    // Start WebSocket server for live reload
    let reload_tx_clone = reload_tx.clone();
    tokio::spawn(async move {
        run_reload_server(reload_tx_clone).await;
    });

    // Set up file watcher
    let watcher = setup_file_watcher(has_client, client_dir);

    // Start the app
    style::print_compiling();
    let mut child = start_app();

    // Set up Ctrl+C handler to clean up child process
    let ctrl_c_handler = tokio::spawn(async {
        tokio::signal::ctrl_c().await.ok();
        cleanup_child_process();
        std::process::exit(0);
    });

    // Run the watch loop (this blocks)
    run_watch_loop(watcher, &mut child, has_client, reload_tx);

    // Clean up if we exit normally
    ctrl_c_handler.abort();
    cleanup_child_process();
}

/// Kill the child process and its descendants
fn cleanup_child_process() {
    let pid = CHILD_PID.load(Ordering::SeqCst);
    if pid == 0 {
        return;
    }

    #[cfg(unix)]
    {
        // Kill the entire process group (negative PID)
        unsafe {
            libc::kill(-(pid as i32), libc::SIGTERM);
        }
        // Give it a moment to terminate gracefully
        std::thread::sleep(Duration::from_millis(100));
        // Force kill if still running
        unsafe {
            libc::kill(-(pid as i32), libc::SIGKILL);
        }
    }

    #[cfg(windows)]
    {
        // On Windows, just kill the process (child processes may still linger)
        let _ = Command::new("taskkill")
            .args(["/PID", &pid.to_string(), "/T", "/F"])
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status();
    }
}

fn setup_client_build() {
    let has_islands = has_island_components();

    if has_islands {
        generate_islands_registry();
    }

    if !Path::new("node_modules").exists() {
        println!(
            "{} {}",
            "→".blue().bold(),
            "Installing dependencies...".white()
        );
        let status = run_bun_command(&["install"]);

        match status {
            Err(e) => {
                style::print_error(&format!("Failed to run bun install: {}", e));
                std::process::exit(1);
            }
            Ok(s) if !s.success() => {
                style::print_error(&format!(
                    "bun install failed with exit code {}",
                    s.code()
                        .map(|c| c.to_string())
                        .unwrap_or_else(|| "unknown".to_string())
                ));
                std::process::exit(1);
            }
            Ok(_) => {}
        }
        println!();
    }

    // Generate appropriate vite config based on whether we have islands
    generate_vite_config(has_islands);

    println!(
        "{} {}",
        "→".blue().bold(),
        "Building client assets...".white()
    );
    run_vite_build();
}

fn setup_file_watcher(
    has_islands: bool,
    client_dir: &Path,
) -> (
    RecommendedWatcher,
    std::sync::mpsc::Receiver<Result<notify::Event, notify::Error>>,
) {
    let (tx, rx) = std::sync::mpsc::channel();
    let mut watcher =
        RecommendedWatcher::new(tx, Config::default()).expect("Failed to create watcher");

    // Watch src directory
    let src_path = Path::new("src");
    if src_path.exists() {
        watcher
            .watch(src_path, RecursiveMode::Recursive)
            .expect("Failed to watch src directory");
    } else {
        style::print_error("No src/ directory found");
        std::process::exit(1);
    }

    // Watch Cargo.toml
    let cargo_toml = Path::new("Cargo.toml");
    if cargo_toml.exists() {
        watcher
            .watch(cargo_toml, RecursiveMode::NonRecursive)
            .expect("Failed to watch Cargo.toml");
    }

    // Watch client directory
    if has_islands {
        watcher
            .watch(client_dir, RecursiveMode::Recursive)
            .expect("Failed to watch client directory");
    }

    // Watch public directory for static assets
    let public_dir = Path::new("public");
    if public_dir.exists() {
        watcher
            .watch(public_dir, RecursiveMode::Recursive)
            .expect("Failed to watch public directory");
    }

    (watcher, rx)
}

fn run_watch_loop(
    _watcher: (
        RecommendedWatcher,
        std::sync::mpsc::Receiver<Result<notify::Event, notify::Error>>,
    ),
    child: &mut Child,
    has_islands: bool,
    reload_tx: Arc<broadcast::Sender<&'static str>>,
) {
    let (_watcher, rx) = _watcher;
    let mut last_restart = Instant::now();
    let debounce_duration = Duration::from_secs(1);

    loop {
        match rx.recv() {
            Ok(Ok(event)) => {
                use notify::EventKind::*;
                match event.kind {
                    Create(_) => {
                        // Check for new route files and generate boilerplate
                        for path in &event.paths {
                            maybe_generate_boilerplate(path);
                        }
                        if last_restart.elapsed() > debounce_duration {
                            handle_file_change(
                                &event,
                                child,
                                has_islands,
                                &reload_tx,
                                &mut last_restart,
                            );
                        }
                    }
                    Modify(_) | Remove(_) => {
                        if last_restart.elapsed() > debounce_duration {
                            handle_file_change(
                                &event,
                                child,
                                has_islands,
                                &reload_tx,
                                &mut last_restart,
                            );
                        }
                    }
                    _ => {}
                }
            }
            Ok(Err(e)) => {
                eprintln!("{} Watch error: {:?}", "!".red().bold(), e);
            }
            Err(e) => {
                eprintln!("{} Channel error: {:?}", "!".red().bold(), e);
                break;
            }
        }
    }
}

fn handle_file_change(
    event: &notify::Event,
    child: &mut Child,
    has_islands: bool,
    reload_tx: &Arc<broadcast::Sender<&'static str>>,
    last_restart: &mut Instant,
) {
    let is_client_only_change = event.paths.iter().all(|p| {
        let path_str = p.to_string_lossy();
        path_str.contains("/client/") || path_str.contains("\\client\\")
    });

    let is_public_only_change = event.paths.iter().all(|p| {
        let path_str = p.to_string_lossy();
        path_str.contains("/public/") || path_str.contains("\\public\\")
    });

    if is_public_only_change {
        // Static assets changed, just trigger a reload (no rebuild needed)
        handle_public_change(reload_tx, last_restart);
    } else if is_client_only_change && has_islands {
        handle_client_change(reload_tx, last_restart);
    } else {
        handle_rust_change(child, has_islands, reload_tx, last_restart);
    }
}

fn handle_public_change(
    reload_tx: &Arc<broadcast::Sender<&'static str>>,
    last_restart: &mut Instant,
) {
    println!(
        "{} {}",
        "↻".cyan().bold(),
        "Static assets changed...".cyan()
    );
    *last_restart = Instant::now();
    let _ = reload_tx.send("reload");
}

fn handle_client_change(
    reload_tx: &Arc<broadcast::Sender<&'static str>>,
    last_restart: &mut Instant,
) {
    println!("{} {}", "↻".cyan().bold(), "Rebuilding client...".cyan());
    let has_islands = has_island_components();
    if has_islands {
        generate_islands_registry();
    }
    generate_vite_config(has_islands);
    run_vite_build();
    *last_restart = Instant::now();
    let _ = reload_tx.send("full");
}

fn handle_rust_change(
    child: &mut Child,
    has_islands: bool,
    reload_tx: &Arc<broadcast::Sender<&'static str>>,
    last_restart: &mut Instant,
) {
    if has_islands {
        println!("{} {}", "↻".blue().bold(), "Rebuilding assets...".blue());
        run_vite_build();
    }

    style::print_compiling();

    // Kill the old process group
    cleanup_child_process();
    let _ = child.wait();

    *child = start_app();
    *last_restart = Instant::now();

    let reload_tx = reload_tx.clone();
    tokio::spawn(async move {
        tokio::time::sleep(Duration::from_millis(500)).await;
        let _ = reload_tx.send("reload");
    });
}

fn run_vite_build() {
    let _ = Command::new("bun")
        .args(["run", "build"])
        .stdout(Stdio::null())
        .stderr(Stdio::inherit())
        .status();
}

fn start_app() -> Child {
    #[cfg(unix)]
    {
        use std::os::unix::process::CommandExt;
        let child = unsafe {
            Command::new("cargo")
                .args(["run", "--quiet"])
                .env("REJOICE_DEV", "1")
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .pre_exec(|| {
                    // Create a new process group so we can kill all children
                    libc::setpgid(0, 0);
                    Ok(())
                })
                .spawn()
                .expect("Failed to start cargo run")
        };
        CHILD_PID.store(child.id(), Ordering::SeqCst);
        child
    }

    #[cfg(windows)]
    {
        use std::os::windows::process::CommandExt;
        // CREATE_NEW_PROCESS_GROUP (0x00000200) allows us to send Ctrl+C to the group
        const CREATE_NEW_PROCESS_GROUP: u32 = 0x00000200;
        let child = Command::new("cargo")
            .args(["run", "--quiet"])
            .env("REJOICE_DEV", "1")
            .stdout(Stdio::inherit())
            .stderr(Stdio::inherit())
            .creation_flags(CREATE_NEW_PROCESS_GROUP)
            .spawn()
            .expect("Failed to start cargo run");
        CHILD_PID.store(child.id(), Ordering::SeqCst);
        child
    }
}

// WebSocket reload server

async fn run_reload_server(reload_tx: Arc<broadcast::Sender<&'static str>>) {
    let app = Router::new().route(
        "/__reload",
        get(move |ws: WebSocketUpgrade| {
            let rx = reload_tx.subscribe();
            async move { ws.on_upgrade(|socket| handle_reload_socket(socket, rx)) }
        }),
    );

    let listener = match tokio::net::TcpListener::bind("127.0.0.1:3001").await {
        Ok(l) => l,
        Err(e) => {
            eprintln!(
                "  {} Live reload unavailable (port 3001 in use: {})",
                "!".yellow().bold(),
                e
            );
            return;
        }
    };

    let _ = axum::serve(listener, app).await;
}

async fn handle_reload_socket(socket: WebSocket, mut rx: broadcast::Receiver<&'static str>) {
    let (mut sender, mut receiver) = socket.split();

    tokio::spawn(async move { while let Some(Ok(_)) = receiver.next().await {} });

    while let Ok(msg) = rx.recv().await {
        if sender.send(Message::Text(msg.into())).await.is_err() {
            break;
        }
    }
}