diff options
| author | gennyble <gen@nyble.dev> | 2026-09-24 01:52:48 -0500 |
|---|---|---|
| committer | gennyble <gen@nyble.dev> | 2026-09-24 01:52:48 -0500 |
| commit | f2a9bc39cb9aa8a9d9e186bfd71dfd2405fe1ab6 (patch) | |
| tree | 9a5fdc35930ce7d0a039fe5eae154555cf0231fb /splorm/src/handler/api.rs | |
| parent | 32cbf8332bdaafae8c0934ff2ab45048bc8f2361 (diff) | |
splorm, meow
Diffstat (limited to 'splorm/src/handler/api.rs')
| -rw-r--r-- | splorm/src/handler/api.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/splorm/src/handler/api.rs b/splorm/src/handler/api.rs new file mode 100644 index 0000000..f360b87 --- /dev/null +++ b/splorm/src/handler/api.rs @@ -0,0 +1,43 @@ +use axum::{ + Router, + body::Body, + extract::{Path, State}, + response::Response, + routing::get, +}; +use sessionhash::RequestContext; + +use crate::{RuntimeError, fs, state::SplormState}; + +pub fn routes() -> Router<SplormState> { + Router::new().route("/stats/now/{name}", get(stats)) +} + +async fn stats( + Path(name): Path<String>, + State(state): State<SplormState>, + ctx: RequestContext, +) -> Result<Response, RuntimeError> { + tracing::trace!("{ctx} GET /api/stats/{name}"); + + let graphs = { + let mut connector = state.gatherer_thread().await; + connector.regular_graphs().await + }; + + let file_path = match name.as_str() { + "current_hostmeminfo.gif" => graphs.meminfo, + "current_hostnetinfo.gif" => graphs.meminfo, + "current_hostcpuinfo.gif" => graphs.cpuinfo, + _ => return Err(RuntimeError::generic_404()), + }; + + tracing::trace!("graph {name} system_path={file_path}"); + let file = fs::unsafe_read_file(file_path).await?; + + Response::builder() + .header("content-type", "image/gif") + .status(200) + .body(Body::from(file)) + .map_err(|_| RuntimeError::Unknown) +} |
