Do some feng shui programming and move all api code into a new api namespace

This commit is contained in:
Daan Boerlage 2023-04-06 18:25:57 +02:00
parent 375717af28
commit f8de92c8d1
Signed by: daan
GPG key ID: FCE070E1E4956606
5 changed files with 45 additions and 39 deletions

12
src/api/endpoints/mod.rs Normal file
View 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")
}

View 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))
}

View 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
View file

@ -0,0 +1 @@
pub mod endpoints;