Skip to main content

tokenburn_web/
app.rs

1//! Server setup: options, router construction and the serve loop.
2
3use topcoat::{
4    asset::{AssetBundle, RouterBuilderAssetExt},
5    context::{Cx, app_context},
6    router::{Router, RouterBuilderDiscoverExt},
7    runtime::RouterBuilderRuntimeExt,
8};
9
10/// Where and how to serve.
11#[derive(Debug, Clone)]
12pub struct ServerOptions {
13    /// Interface to bind. Defaults to `127.0.0.1`: token/cost data is private.
14    pub host: String,
15    pub port: u16,
16    /// Open the dashboard in the default browser once the server is listening.
17    pub open_browser: bool,
18}
19
20impl Default for ServerOptions {
21    fn default() -> Self {
22        Self {
23            host: "127.0.0.1".into(),
24            port: 3000,
25            open_browser: false,
26        }
27    }
28}
29
30/// Whether the browser runtime (signals/shards/WebSocket) is available.
31#[derive(Clone, Copy, Debug, PartialEq, Eq)]
32pub struct Interactive(pub bool);
33
34/// Read the [`Interactive`] flag from the router's app context.
35pub(crate) fn is_interactive(cx: &Cx) -> bool {
36    app_context::<Interactive>(cx).0
37}
38
39/// Build the router. `Some(bundle)` enables live mode; `None` serves the
40/// static dashboard.
41pub fn router(bundle: Option<AssetBundle>) -> Router {
42    let builder = Router::builder().discover();
43    match bundle {
44        Some(bundle) => builder
45            .app_context(Interactive(true))
46            .assets(bundle)
47            .runtime()
48            .build(),
49        None => builder.app_context(Interactive(false)).build(),
50    }
51}
52
53/// Directories searched for the Topcoat asset bundle (the browser runtime),
54/// in order. `exe_dir` is the directory of the running executable.
55///
56/// 1. `$TOKENBURN_WEB_ASSETS`
57/// 2. `<exe_dir>/assets` — release archives, `cargo run`, Windows installs
58/// 3. `<exe_dir>/../share/tokenburn-web/assets` — Homebrew / prefix installs
59/// 4. `/usr/share/tokenburn-web/assets`, `/usr/local/share/tokenburn-web/assets` — .deb/.rpm/AUR
60pub fn bundle_candidates(
61    exe_dir: Option<&std::path::Path>,
62    env_override: Option<std::ffi::OsString>,
63) -> Vec<std::path::PathBuf> {
64    use std::path::PathBuf;
65    let mut out: Vec<PathBuf> = Vec::new();
66    out.extend(env_override.filter(|v| !v.is_empty()).map(PathBuf::from));
67    if let Some(dir) = exe_dir {
68        out.push(dir.join("assets"));
69        out.push(dir.join("../share/tokenburn-web/assets"));
70    }
71    out.push(PathBuf::from("/usr/share/tokenburn-web/assets"));
72    out.push(PathBuf::from("/usr/local/share/tokenburn-web/assets"));
73    out
74}
75
76/// Find and load the asset bundle, or `None` (→ static dashboard).
77fn find_bundle() -> Option<AssetBundle> {
78    let exe_dir = std::env::current_exe()
79        .ok()
80        .and_then(|p| p.parent().map(std::path::Path::to_path_buf));
81    let candidates =
82        bundle_candidates(exe_dir.as_deref(), std::env::var_os("TOKENBURN_WEB_ASSETS"));
83    candidates
84        .iter()
85        .find_map(|dir| AssetBundle::load_dir(dir).ok())
86}
87
88/// Bind and serve until the process is killed.
89pub async fn serve(opts: ServerOptions) -> std::io::Result<()> {
90    let bundle = find_bundle();
91    if bundle.is_none() {
92        eprintln!(
93            "tokenburn-web: no asset bundle found; serving the static dashboard.\n\
94             For the live version run `just run-web`, use a release archive/package, or set \
95             TOKENBURN_WEB_ASSETS to a `topcoat asset bundle` directory."
96        );
97    }
98    let live = bundle.is_some();
99
100    let listener = tokio::net::TcpListener::bind((opts.host.as_str(), opts.port)).await?;
101    let addr = listener.local_addr()?;
102    println!(
103        "tokenburn-web listening on http://{addr} ({})",
104        if live { "live" } else { "static" }
105    );
106    if !addr.ip().is_loopback() {
107        eprintln!(
108            "tokenburn-web: warning — bound to {addr}; there is no auth, anyone who can reach it sees your token usage."
109        );
110    }
111    if opts.open_browser {
112        // The listener is already bound, so the browser can connect right away.
113        let url = browser_url(addr);
114        if let Err(e) = open_in_browser(&url) {
115            eprintln!("tokenburn-web: could not open a browser ({e}); visit {url}");
116        }
117    }
118    topcoat::serve(listener, router(bundle)).await
119}
120
121/// URL to open for a bound address (`0.0.0.0` / `::` are not browsable → localhost).
122pub fn browser_url(addr: std::net::SocketAddr) -> String {
123    if addr.ip().is_unspecified() {
124        format!("http://localhost:{}", addr.port())
125    } else if addr.is_ipv6() {
126        format!("http://[{}]:{}", addr.ip(), addr.port())
127    } else {
128        format!("http://{addr}")
129    }
130}
131
132/// Open `url` with the platform's default handler (no extra dependency).
133fn open_in_browser(url: &str) -> std::io::Result<()> {
134    use std::process::{Command, Stdio};
135
136    #[cfg(target_os = "macos")]
137    let mut cmd = Command::new("open");
138    #[cfg(target_os = "windows")]
139    let mut cmd = {
140        let mut c = Command::new("cmd");
141        c.args(["/C", "start", ""]);
142        c
143    };
144    #[cfg(all(not(target_os = "macos"), not(target_os = "windows")))]
145    let mut cmd = Command::new("xdg-open");
146
147    cmd.arg(url)
148        .stdin(Stdio::null())
149        .stdout(Stdio::null())
150        .stderr(Stdio::null())
151        .spawn()
152        .map(|_| ())
153}
154
155#[cfg(test)]
156mod tests {
157    use super::*;
158
159    #[test]
160    fn bundle_search_order() {
161        let exe = std::path::Path::new("/opt/tb");
162        let c = bundle_candidates(Some(exe), Some("/custom".into()));
163        assert_eq!(c[0], std::path::PathBuf::from("/custom"));
164        assert_eq!(c[1], std::path::PathBuf::from("/opt/tb/assets"));
165        assert!(
166            c.iter()
167                .any(|p| p.ends_with("usr/share/tokenburn-web/assets"))
168        );
169    }
170
171    #[test]
172    fn empty_override_and_unknown_exe_dir_are_tolerated() {
173        let c = bundle_candidates(None, Some("".into()));
174        assert!(c.iter().all(|p| p.starts_with("/usr")));
175    }
176
177    #[test]
178    fn unspecified_addresses_open_localhost() {
179        assert_eq!(
180            browser_url("0.0.0.0:3000".parse().unwrap()),
181            "http://localhost:3000"
182        );
183        assert_eq!(
184            browser_url("[::]:8080".parse().unwrap()),
185            "http://localhost:8080"
186        );
187    }
188
189    #[test]
190    fn concrete_addresses_are_used_as_is() {
191        assert_eq!(
192            browser_url("127.0.0.1:3000".parse().unwrap()),
193            "http://127.0.0.1:3000"
194        );
195        assert_eq!(
196            browser_url("[::1]:3000".parse().unwrap()),
197            "http://[::1]:3000"
198        );
199    }
200}