use std::net::SocketAddr;
use std::str::FromStr;
pub(crate) mod network {
use super::*;
const LOCALHOST_IP: &str = "127.0.0.1";
const DEFAULT_PORT: u16 = 8089;
pub(crate) fn default_listen_addr() -> SocketAddr {
SocketAddr::from_str(&format!("{LOCALHOST_IP}:{DEFAULT_PORT}"))
.expect("Valid default diagnostics listen address")
}
}
pub(crate) mod routes {
pub(crate) mod js_resources {
pub(crate) const BACKTRACE_PROCESSOR: &str = "backtrace-processor.js";
pub(crate) const VIZ_JS_INTEGRATION: &str = "viz-js-integration.js";
pub(crate) const FLAMEGRAPH_RENDERER: &str = "flamegraph-renderer.js";
pub(crate) const CALLGRAPH_SVG_RENDERER: &str = "callgraph-svg-renderer.js";
pub(crate) const DATA_ACCESS: &str = "data-access.js";
pub(crate) const MAIN: &str = "main.js";
pub(crate) const CUSTOM_ELEMENTS: &str = "custom_elements.js";
}
pub(crate) mod css_resources {
pub(crate) const STYLES: &str = "styles.css";
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_listen_addr_is_localhost() {
let addr = network::default_listen_addr();
assert!(
addr.ip().is_loopback(),
"Diagnostics endpoint MUST bind to localhost (127.0.0.1) for security, got: {}",
addr.ip()
);
}
#[test]
fn test_all_resource_paths_are_unique() {
use std::collections::HashSet;
let all_resources = [
routes::js_resources::BACKTRACE_PROCESSOR,
routes::js_resources::VIZ_JS_INTEGRATION,
routes::js_resources::FLAMEGRAPH_RENDERER,
routes::js_resources::CALLGRAPH_SVG_RENDERER,
routes::js_resources::DATA_ACCESS,
routes::js_resources::MAIN,
routes::js_resources::CUSTOM_ELEMENTS,
routes::css_resources::STYLES,
];
let unique: HashSet<_> = all_resources.iter().collect();
assert_eq!(
unique.len(),
all_resources.len(),
"All resource paths must be unique"
);
}
}