1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
//! nested `/api`
//!
//! Route handler for `/api` routes.
//!
//! | Handler | Route |
//! | - | - |
//! | [`auth::register`] | POST `/api/register` |
//! | [`auth::login`] | POST `/api/login` |
//! | [`auth::logout`] | POST `/api/logout` |
//! | [`stats`] | GET `/api/stats/now/{name}` |
mod auth;
use axum::{
Router,
body::Body,
extract::{Path, State},
response::Response,
routing::{get, post},
};
use sessionhash::RequestContext;
use crate::{RuntimeError, fs, state::SplormState};
/// Routes nested under /api
pub fn routes() -> Router<SplormState> {
Router::new()
.route("/auth/register", post(auth::register))
.route("/auth/login", post(auth::login))
.route("/auth/logout", post(auth::logout))
.route("/stats/now/{name}", get(stats))
}
/// GET /api/stats/now/{name}
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
}
.map_err(|_| RuntimeError::Unknown)?;
let file_path = match name.as_str() {
"current_hostmeminfo.gif" => graphs.meminfo,
"current_hostnetinfo.gif" => graphs.netinfo,
"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)
}
|