Do some feng shui programming and move all api code into a new api namespace
This commit is contained in:
parent
375717af28
commit
f8de92c8d1
5 changed files with 45 additions and 39 deletions
12
src/api/endpoints/mod.rs
Normal file
12
src/api/endpoints/mod.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
pub mod stats;
|
||||
pub mod worlds;
|
||||
use crate::collectors::PromMetric;
|
||||
|
||||
fn convert_into_prom_metrics(data: Vec<impl PromMetric>) -> String {
|
||||
let metrics: Vec<String> = data
|
||||
.into_iter()
|
||||
.map(|w| w.to_metric_string())
|
||||
.collect();
|
||||
|
||||
metrics.join("\n")
|
||||
}
|
14
src/api/endpoints/stats.rs
Normal file
14
src/api/endpoints/stats.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
use axum::extract::Path;
|
||||
use axum::http::StatusCode;
|
||||
use axum::response::IntoResponse;
|
||||
use crate::collectors::stats::get_player_stats;
|
||||
use super::convert_into_prom_metrics;
|
||||
|
||||
pub async fn get_stats(Path(rsn): Path<String>) -> impl IntoResponse {
|
||||
let resp = match get_player_stats(&rsn).await {
|
||||
Ok(r) => r,
|
||||
Err(_) => return (StatusCode::INTERNAL_SERVER_ERROR, "Nope".to_string())
|
||||
};
|
||||
|
||||
(StatusCode::OK, convert_into_prom_metrics(resp))
|
||||
}
|
13
src/api/endpoints/worlds.rs
Normal file
13
src/api/endpoints/worlds.rs
Normal file
|
@ -0,0 +1,13 @@
|
|||
use axum::http::StatusCode;
|
||||
use axum::response::IntoResponse;
|
||||
use crate::collectors::player_count::get_player_count;
|
||||
use super::convert_into_prom_metrics;
|
||||
|
||||
pub async fn get_worlds() -> impl IntoResponse {
|
||||
let resp = match get_player_count().await {
|
||||
Ok(r) => r,
|
||||
Err(_) => return (StatusCode::INTERNAL_SERVER_ERROR, "Nope".to_string())
|
||||
};
|
||||
|
||||
(StatusCode::OK, convert_into_prom_metrics(resp))
|
||||
}
|
1
src/api/mod.rs
Normal file
1
src/api/mod.rs
Normal file
|
@ -0,0 +1 @@
|
|||
pub mod endpoints;
|
Loading…
Add table
Add a link
Reference in a new issue