diff --git a/src/api/endpoints/mod.rs b/src/api/endpoints/mod.rs new file mode 100644 index 0000000..46a3ca9 --- /dev/null +++ b/src/api/endpoints/mod.rs @@ -0,0 +1,12 @@ +pub mod stats; +pub mod worlds; +use crate::collectors::PromMetric; + +fn convert_into_prom_metrics(data: Vec) -> String { + let metrics: Vec = data + .into_iter() + .map(|w| w.to_metric_string()) + .collect(); + + metrics.join("\n") +} \ No newline at end of file diff --git a/src/api/endpoints/stats.rs b/src/api/endpoints/stats.rs new file mode 100644 index 0000000..6b46ca6 --- /dev/null +++ b/src/api/endpoints/stats.rs @@ -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) -> 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)) +} \ No newline at end of file diff --git a/src/api/endpoints/worlds.rs b/src/api/endpoints/worlds.rs new file mode 100644 index 0000000..665a17e --- /dev/null +++ b/src/api/endpoints/worlds.rs @@ -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)) +} \ No newline at end of file diff --git a/src/api/mod.rs b/src/api/mod.rs new file mode 100644 index 0000000..e0d74d3 --- /dev/null +++ b/src/api/mod.rs @@ -0,0 +1 @@ +pub mod endpoints; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 17affd0..e3ffaaa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,56 +1,22 @@ +mod api; mod collectors; mod lifecycle; mod transport; use axum::{Router, BoxError}; -use axum::extract::Path; -use axum::http::StatusCode; -use axum::response::{IntoResponse}; use axum::routing::get; use axum_tracing_opentelemetry::{response_with_trace_layer, opentelemetry_tracing_layer}; use std::net::SocketAddr; -use collectors::PromMetric; - -fn convert_into_metrics(data: Vec) -> String { - let metrics: Vec = data - .into_iter() - .map(|w| w.to_metric_string()) - .collect(); - - metrics.join("\n") -} - -async fn worlds() -> impl IntoResponse { - let resp = match collectors::player_count::get_player_count().await { - Ok(r) => r, - Err(_) => return (StatusCode::INTERNAL_SERVER_ERROR, "Nope".to_string()) - }; - - (StatusCode::OK, convert_into_metrics(resp)) -} - -async fn stats(Path(rsn): Path) -> impl IntoResponse { - let resp = match collectors::stats::get_player_stats(&rsn).await { - Ok(r) => r, - Err(_) => return (StatusCode::INTERNAL_SERVER_ERROR, "Nope".to_string()) - }; - - (StatusCode::OK, convert_into_metrics(resp)) -} - -async fn get_trace() -> impl IntoResponse { - let trace_id = axum_tracing_opentelemetry::find_current_trace_id(); - axum::Json(serde_json::json!({ "my_trace_id": trace_id })) -} +use api::endpoints::stats; +use api::endpoints::worlds; #[tokio::main] async fn main() -> Result<(), BoxError> { axum_tracing_opentelemetry::tracing_subscriber_ext::init_subscribers()?; let app = Router::new() - .route("/worlds", get(worlds)) - .route("/stats/:rsn", get(stats)) - .route("/trace", get(get_trace)) + .route("/worlds", get(worlds::get_worlds)) + .route("/stats/:rsn", get(stats::get_stats)) .layer(response_with_trace_layer()) .layer(opentelemetry_tracing_layer());