Server half of sccache-dash

Polls sccache's counters every 10 s, keeps a JSON-lines history, and serves
the page and its API on 127.0.0.1: one difference-with-reset mechanism behind
every range, per-language time saved, problems, a copyable summary, and
extra tables for multi-level and distributed caches.

The page is embedded from crates/ui/dist when it exists, with a placeholder
until then. POST /api/zero refuses foreign Host and Origin headers.
Tests run against a stand-in sccache script; sample fixtures for the
multi-level and distributed shapes are made up.
This commit is contained in:
swaits committed 2026-11-28 11:23:39 -07:00
1 parent a8e7bd66cf
commit 3cbc02e631
18 files changed
+3654

No files matched your search

+163
View File
@@ -0,0 +1,163 @@
//! `sccache-dash`: serves the dashboard, or installs itself as a user service.
use std::process::{Command as Process, Stdio};
use std::sync::Arc;
use sccache_dash::app::{unix_now, App, Config};
use sccache_dash::{http, systemd};
const USAGE: &str = "\
sccache-dash [--port N] [--no-open] serve the dashboard on http://127.0.0.1:8226 and open it
sccache-dash install start it at login, as a systemd user service
sccache-dash uninstall remove that service again";
fn main() {
let args: Vec<String> = std::env::args().skip(1).collect();
if let Err(e) = run(&args) {
eprintln!("sccache-dash: {e}");
std::process::exit(1);
}
}
fn run(args: &[String]) -> Result<(), String> {
match parse(args)? {
Command::Help => println!("{USAGE}"),
Command::Install => systemd::install()?,
Command::Uninstall => systemd::uninstall()?,
Command::Serve { port, open } => serve(port, open)?,
}
Ok(())
}
/// What the command line asks for.
#[derive(Debug)]
enum Command {
Serve { port: u16, open: bool },
Install,
Uninstall,
Help,
}
fn parse(args: &[String]) -> Result<Command, String> {
let mut serve = Command::Serve {
port: sccache_dash_api::DEFAULT_PORT,
open: true,
};
let mut i = 0;
while let Some(arg) = args.get(i) {
i += 1;
match arg.as_str() {
"--no-open" => {
if let Command::Serve { open, .. } = &mut serve {
*open = false;
}
}
"--help" | "-h" => return Ok(Command::Help),
"install" => return Ok(Command::Install),
"uninstall" => return Ok(Command::Uninstall),
a if a == "--port" || a.starts_with("--port=") => {
let value = match a.strip_prefix("--port=") {
Some(value) => value.to_owned(),
None => {
i += 1;
args.get(i - 1)
.ok_or("--port needs a number, as in --port 8226")?
.clone()
}
};
let port = value
.parse()
.map_err(|_| format!("`{value}` is not a port number"))?;
if let Command::Serve { port: p, .. } = &mut serve {
*p = port;
}
}
a => return Err(format!("unknown argument `{a}`\n\n{USAGE}")),
}
}
Ok(serve)
}
fn serve(port: u16, open: bool) -> Result<(), String> {
let app = Arc::new(App::new(Config::from_env()?, unix_now())?);
let server = http::listen(port)?;
let port = http::port_of(&server);
let url = format!("http://127.0.0.1:{port}");
println!(
"sccache-dash: serving {url}, history in {}",
app.cfg.history.display()
);
app.clone().keep_updated();
if open {
// Left to finish on its own: a browser that will not open is not worth stopping for.
let browser = Process::new("xdg-open")
.arg(&url)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn();
if let Ok(mut browser) = browser {
std::thread::spawn(move || {
let _ = browser.wait();
});
}
}
http::answer(server, app);
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
fn parse_args(args: &[&str]) -> Result<Command, String> {
parse(&args.iter().map(|a| a.to_string()).collect::<Vec<_>>())
}
#[test]
fn the_command_line() {
assert!(matches!(
parse_args(&[]),
Ok(Command::Serve {
port: 8226,
open: true
})
));
assert!(matches!(
parse_args(&["--port", "18226"]),
Ok(Command::Serve {
port: 18226,
open: true
})
));
assert!(matches!(
parse_args(&["--port=9000", "--no-open"]),
Ok(Command::Serve {
port: 9000,
open: false
})
));
assert!(matches!(
parse_args(&["--no-open", "--port", "1"]),
Ok(Command::Serve {
port: 1,
open: false
})
));
assert!(matches!(parse_args(&["install"]), Ok(Command::Install)));
assert!(matches!(parse_args(&["uninstall"]), Ok(Command::Uninstall)));
assert!(matches!(parse_args(&["--help"]), Ok(Command::Help)));
assert!(parse_args(&["18226"])
.unwrap_err()
.starts_with("unknown argument `18226`"));
assert!(parse_args(&["--port"])
.unwrap_err()
.contains("--port needs a number"));
assert!(parse_args(&["--port", "x"])
.unwrap_err()
.contains("`x` is not a port number"));
assert!(parse_args(&["--port", "70000"])
.unwrap_err()
.contains("is not a port number"));
}
}