Skip to main content

nlfmt_serve/
lib.rs

1mod args;
2mod assets;
3mod auth;
4mod color;
5mod models;
6mod qrcode;
7mod routes;
8mod state;
9mod updater;
10mod util;
11mod utils;
12mod server;
13
14pub use args::ServeArgs;
15
16#[macro_use]
17extern crate rocket;
18
19use color::{GRAY, GREEN, LBLUE, RST};
20use qrcode::qr_string;
21use qrcode_generator::QrCodeEcc;
22use server::launch_server;
23use state::AppState;
24use util::path::{get_root_dir, pretty_path};
25use utils::connection_string;
26
27pub async fn run(args: ServeArgs) -> anyhow::Result<()> {
28    if args.update {
29        return updater::run_update().await.map_err(anyhow::Error::msg);
30    }
31
32    updater::run_background_check();
33
34    let root_dir = get_root_dir(&args.root_dir)?;
35    let addr = connection_string(args.interface, args.port);
36    let app_state = AppState::new(&args, &root_dir);
37
38    if args.qr {
39        let matrix =
40            qrcode_generator::to_matrix(&addr, QrCodeEcc::Low).expect("Couldn't create QR Code");
41        print!("\n{}", qr_string(matrix));
42    }
43
44    if args.symlinks {
45        println!("\x1b[91mSecurity Warning:\x1b[0m You've enabled symlinks, this can allow users to access arbitrary files on your system. Use with caution.\n")
46    }
47
48    println!(
49        "{GREEN}serve running {RST}@ {LBLUE}{}{RST}\nāžœ {GRAY}root: {RST}{}",
50        addr,
51        pretty_path(&root_dir),
52    );
53
54    let perms = app_state.get_perms();
55    if perms.len() > 0 {
56        println!("āžœ {GRAY}enabled: {RST}{}", perms.join(", "))
57    }
58    println!("");
59
60    // run vite dev server in debug mode
61    #[cfg(debug_assertions)]
62    launch_dev_server();
63
64    launch_server(args, app_state).await?;
65    Ok(())
66}
67
68#[cfg(debug_assertions)]
69fn launch_dev_server() {
70    use std::{path::PathBuf, process::Command};
71
72    let project_root =
73        PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set"));
74
75    std::env::set_current_dir(&project_root).unwrap();
76    Command::new("powershell")
77        .arg("-c")
78        .arg("pnpm dev")
79        .current_dir("./app")
80        .spawn()
81        .unwrap();
82}