Skip to main content

tokenburn_web/
api.rs

1//! JSON and static routes.
2
3use topcoat::{
4    Result,
5    context::Cx,
6    router::{
7        content::{Css, Json},
8        query_params, route,
9    },
10};
11
12use crate::{data::load, params::UsageParams, style::CSS};
13
14/// The same report as JSON: `/api/report?tool=pi&bucket=daily`.
15#[route(GET "/api/report")]
16async fn report(cx: &Cx) -> Result<Json<Vec<tokenburn_core::ReportRow>>> {
17    let query = query_params::<UsageParams>(cx)?.to_query();
18    Ok(Json(load(query, &Default::default()).await.report))
19}
20
21/// Liveness probe.
22#[route(GET "/api/health")]
23async fn health() -> Result<Json<&'static str>> {
24    Ok(Json("ok"))
25}
26
27/// Every colour theme with its palette (the same ones the TUI and GUI offer).
28#[route(GET "/api/themes")]
29async fn themes() -> Result<Json<serde_json::Value>> {
30    Ok(Json(crate::theme::catalogue()))
31}
32
33#[route(GET "/style.css")]
34async fn style() -> Result<Css<&'static str>> {
35    Ok(Css(CSS))
36}