Skip to main content

pitchfork_cli/proxy/
mod.rs

1//! Reverse proxy server for pitchfork daemons.
2//!
3//! Routes `<slug>.<tld>:<port>` to the daemon's actual listening port.
4//! Slugs are defined in the global config (`~/.config/pitchfork/config.toml`)
5//! under `[slugs]`. Each slug maps to a project directory and daemon name.
6//!
7//! # URL Routing
8//!
9//! ```text
10//! myapp.localhost:7777          →  localhost:8080  (via slug)
11//! ```
12
13pub mod hosts;
14pub mod lan_ip;
15pub mod mdns;
16pub mod server;
17pub mod trust;
18pub mod worktree;
19
20/// Build a proxy URL from an optional slug and settings.
21///
22/// Returns `None` if:
23/// - `slug` is `None` (not proxied)
24/// - Proxy is disabled in settings
25/// - `proxy.port` is invalid (out of range or zero)
26pub fn build_proxy_url(slug: Option<&str>, s: &crate::settings::Settings) -> Option<String> {
27    if !s.proxy.enable {
28        return None;
29    }
30    let slug = slug?;
31
32    let scheme = if s.proxy.https { "https" } else { "http" };
33    let tld = &s.proxy.tld;
34    let standard_port = if s.proxy.https { 443u16 } else { 80u16 };
35
36    let effective_port = u16::try_from(s.proxy.port).ok().filter(|&p| p > 0)?;
37
38    let host = format!("{slug}.{tld}");
39
40    Some(if effective_port == standard_port {
41        format!("{scheme}://{host}")
42    } else {
43        format!("{scheme}://{host}:{effective_port}")
44    })
45}