Skip to main content

pitchfork_cli/web/
mod.rs

1mod routes;
2mod server;
3mod static_files;
4
5pub use server::{serve, serve_api};
6
7use std::sync::OnceLock;
8
9static BASE_PATH: OnceLock<String> = OnceLock::new();
10
11static WEB_PORT: OnceLock<u16> = OnceLock::new();
12
13/// URL of the running web UI, derived from the actual bound address
14/// (which may differ from settings due to port bumping).
15static WEB_URL: OnceLock<String> = OnceLock::new();
16
17pub fn port() -> Option<u16> {
18    WEB_PORT.get().copied()
19}
20
21pub fn url() -> Option<String> {
22    WEB_URL.get().cloned()
23}
24
25pub(crate) fn normalize_base_path(path: Option<&str>) -> crate::Result<String> {
26    match path {
27        None => Ok(String::new()),
28        Some(p) => {
29            let trimmed = p.trim().trim_matches('/');
30            if trimmed.is_empty() {
31                Ok(String::new())
32            } else if !trimmed
33                .chars()
34                .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
35            {
36                Err(miette::miette!(
37                    "PITCHFORK_WEB_PATH must contain only alphanumeric characters, hyphens, or underscores, got: {trimmed:?}"
38                ))
39            } else {
40                Ok(format!("/{trimmed}"))
41            }
42        }
43    }
44}