Skip to main content

pitchfork_cli/web/
mod.rs

1pub(crate) mod 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
25/// Origin (`https://host:port`, no path prefix) of the web UI when it
26/// terminates TLS itself; `None` while it serves plain HTTP.
27///
28/// The reverse proxy only speaks plain HTTP to its backends, so when this is
29/// set it redirects `pitchfork.<tld>` here instead of forwarding to the port.
30static WEB_HTTPS_ORIGIN: OnceLock<String> = OnceLock::new();
31
32pub fn https_origin() -> Option<String> {
33    WEB_HTTPS_ORIGIN.get().cloned()
34}
35
36pub(crate) fn normalize_base_path(path: Option<&str>) -> crate::Result<String> {
37    match path {
38        None => Ok(String::new()),
39        Some(p) => {
40            let trimmed = p.trim().trim_matches('/');
41            if trimmed.is_empty() {
42                Ok(String::new())
43            } else if !trimmed
44                .chars()
45                .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
46            {
47                Err(miette::miette!(
48                    "PITCHFORK_WEB_PATH must contain only alphanumeric characters, hyphens, or underscores, got: {trimmed:?}"
49                ))
50            } else {
51                Ok(format!("/{trimmed}"))
52            }
53        }
54    }
55}