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;

View file

@ -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<impl PromMetric>) -> String {
let metrics: Vec<String> = 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<String>) -> 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());