generated from phundrak/rust-poem-openapi-template
Initial commit
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
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;
|
||||
|
||||
#[OpenApi(prefix_path = "/v1/version", tag = "ApiCategory::Version")]
|
||||
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);
|
||||
let resp = cli.get("/v1/version").send().await;
|
||||
resp.assert_status_is_ok();
|
||||
let json = resp.json().await;
|
||||
let json_value = json.value();
|
||||
json_value.object().get("version").assert_not_null();
|
||||
}
|
||||
Reference in New Issue
Block a user