2024-08-10 09:33:15 +00:00
|
|
|
use poem::Result;
|
|
|
|
|
use poem_openapi::{payload::Json, ApiResponse, Object, OpenApi};
|
|
|
|
|
|
|
|
|
|
use crate::settings::Settings;
|
|
|
|
|
|
|
|
|
|
use super::ApiCategory;
|
|
|
|
|
|
|
|
|
|
#[derive(Object, Debug, Clone, serde::Serialize, serde::Deserialize)]
|
|
|
|
|
struct Meta {
|
|
|
|
|
version: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<poem::web::Data<&Settings>> for Meta {
|
|
|
|
|
fn from(value: poem::web::Data<&Settings>) -> Self {
|
|
|
|
|
let version = value.application.version.clone();
|
|
|
|
|
Self { version }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(ApiResponse)]
|
|
|
|
|
enum VersionResponse {
|
|
|
|
|
#[oai(status = 200)]
|
|
|
|
|
Version(Json<Meta>),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct VersionApi;
|
|
|
|
|
|
2024-08-10 11:06:18 +02:00
|
|
|
#[OpenApi(prefix_path = "/v1/api/version", tag = "ApiCategory::Version")]
|
2024-08-10 09:33:15 +00:00
|
|
|
impl VersionApi {
|
|
|
|
|
#[oai(path = "/", method = "get")]
|
|
|
|
|
async fn version(&self, settings: poem::web::Data<&Settings>) -> Result<VersionResponse> {
|
|
|
|
|
tracing::event!(target: "gege-jdr-backend", tracing::Level::DEBUG, "Accessing version endpoint.");
|
|
|
|
|
Ok(VersionResponse::Version(Json(settings.into())))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn version_works() {
|
|
|
|
|
let app = crate::get_test_app(None).await;
|
|
|
|
|
let cli = poem::test::TestClient::new(app);
|
2024-08-10 11:06:18 +02:00
|
|
|
let resp = cli.get("/v1/api/version").send().await;
|
2024-08-10 09:33:15 +00:00
|
|
|
resp.assert_status_is_ok();
|
|
|
|
|
let json = resp.json().await;
|
|
|
|
|
let json_value = json.value();
|
|
|
|
|
json_value.object().get("version").assert_not_null();
|
|
|
|
|
}
|