2025-11-04 16:27:54 +01:00
|
|
|
//! Application metadata endpoint for retrieving version and name information.
|
|
|
|
|
|
2025-11-04 09:17:18 +01:00
|
|
|
use poem::Result;
|
|
|
|
|
use poem_openapi::{ApiResponse, Object, OpenApi, payload::Json};
|
|
|
|
|
|
|
|
|
|
use super::ApiCategory;
|
2025-11-04 16:27:54 +01:00
|
|
|
use crate::settings::ApplicationSettings;
|
2025-11-04 09:17:18 +01:00
|
|
|
|
|
|
|
|
#[derive(Object, Debug, Clone, serde::Serialize, serde::Deserialize)]
|
|
|
|
|
struct Meta {
|
|
|
|
|
version: String,
|
|
|
|
|
name: String,
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-04 16:27:54 +01:00
|
|
|
impl From<&MetaApi> for Meta {
|
|
|
|
|
fn from(value: &MetaApi) -> Self {
|
|
|
|
|
let version = value.version.clone();
|
|
|
|
|
let name = value.name.clone();
|
2025-11-04 09:17:18 +01:00
|
|
|
Self { version, name }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(ApiResponse)]
|
|
|
|
|
enum MetaResponse {
|
|
|
|
|
#[oai(status = 200)]
|
|
|
|
|
Meta(Json<Meta>),
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-04 16:27:54 +01:00
|
|
|
/// API for retrieving application metadata (name and version).
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct MetaApi {
|
|
|
|
|
name: String,
|
|
|
|
|
version: String,
|
|
|
|
|
}
|
2025-11-04 09:17:18 +01:00
|
|
|
|
2025-11-04 16:27:54 +01:00
|
|
|
impl From<&ApplicationSettings> for MetaApi {
|
|
|
|
|
fn from(value: &ApplicationSettings) -> Self {
|
|
|
|
|
let name = value.name.clone();
|
|
|
|
|
let version = value.version.clone();
|
|
|
|
|
Self { name, version }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[OpenApi(tag = "ApiCategory::Meta")]
|
2025-11-04 09:17:18 +01:00
|
|
|
impl MetaApi {
|
2025-11-04 16:27:54 +01:00
|
|
|
#[oai(path = "/meta", method = "get")]
|
|
|
|
|
async fn meta(&self) -> Result<MetaResponse> {
|
|
|
|
|
tracing::event!(target: "backend::meta", tracing::Level::DEBUG, "Accessing meta endpoint");
|
|
|
|
|
Ok(MetaResponse::Meta(Json(self.into())))
|
2025-11-04 09:17:18 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn meta_endpoint_returns_correct_data() {
|
|
|
|
|
let app = crate::get_test_app();
|
|
|
|
|
let cli = poem::test::TestClient::new(app);
|
2025-11-04 16:27:54 +01:00
|
|
|
let resp = cli.get("/api/meta").send().await;
|
2025-11-04 09:17:18 +01:00
|
|
|
resp.assert_status_is_ok();
|
|
|
|
|
|
2025-11-04 16:27:54 +01:00
|
|
|
let json_value: serde_json::Value = resp.json().await.value().deserialize();
|
2025-11-04 09:17:18 +01:00
|
|
|
|
2025-11-04 16:27:54 +01:00
|
|
|
assert!(
|
|
|
|
|
json_value.get("version").is_some(),
|
|
|
|
|
"Response should have version field"
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
|
|
|
|
json_value.get("name").is_some(),
|
|
|
|
|
"Response should have name field"
|
|
|
|
|
);
|
2025-11-04 09:17:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn meta_endpoint_returns_200_status() {
|
|
|
|
|
let app = crate::get_test_app();
|
|
|
|
|
let cli = poem::test::TestClient::new(app);
|
2025-11-04 16:27:54 +01:00
|
|
|
let resp = cli.get("/api/meta").send().await;
|
2025-11-04 09:17:18 +01:00
|
|
|
resp.assert_status_is_ok();
|
|
|
|
|
}
|
|
|
|
|
}
|