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;
|
44
src/main.rs
44
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<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());
|
||||
|
||||
|
|
Loading…
Reference in a new issue