From 8bcedd9fbfd53e5f40ded50f57979487f38dfaab Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 22 Aug 2023 04:18:01 +0200 Subject: [PATCH] Endpoints which return the version now return a JSON object --- todored/src/main.rs | 8 ++++---- todored/src/routes/mod.rs | 8 ++++++-- todored/src/routes/root.rs | 11 +++++++---- todored/src/routes/structs.rs | 6 ++++++ 4 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 todored/src/routes/structs.rs diff --git a/todored/src/main.rs b/todored/src/main.rs index 2e1bee5..407f7cc 100644 --- a/todored/src/main.rs +++ b/todored/src/main.rs @@ -17,10 +17,10 @@ async fn main() { .expect("to be able to connect to Redis"); let router = axum::Router::new() - .route("/", get(routes::root::version)) - .route("/version", get(routes::root::version)) - .route("/", post(routes::root::healthcheck)) - .route("/healthcheck", post(routes::root::healthcheck)) + .route("/", get(routes::version_route)) + .route("/version", get(routes::version_route)) + .route("/", post(routes::healthcheck_route)) + .route("/healthcheck", post(routes::healthcheck_route)) .route("/board/:board/ws", get(routes::board::board_websocket)) .layer(axum::Extension(rclient)) .layer(tower_http::cors::CorsLayer::new() diff --git a/todored/src/routes/mod.rs b/todored/src/routes/mod.rs index 0de7e88..cf9096e 100644 --- a/todored/src/routes/mod.rs +++ b/todored/src/routes/mod.rs @@ -1,2 +1,6 @@ -pub(crate) mod root; -pub(crate) mod board; +pub mod board; +pub mod structs; +pub(self) mod root; + +pub(crate) use root::version as version_route; +pub(crate) use root::healthcheck as healthcheck_route; diff --git a/todored/src/routes/root.rs b/todored/src/routes/root.rs index 2189170..b35d7cb 100644 --- a/todored/src/routes/root.rs +++ b/todored/src/routes/root.rs @@ -2,23 +2,26 @@ use axum::{Extension, Json}; use axum::http::StatusCode; use crate::outcome::{Response, LoggableOutcome}; +use crate::routes::structs::VersionInfo; const MAJOR: u32 = pkg_version::pkg_version_major!(); const MINOR: u32 = pkg_version::pkg_version_minor!(); const PATCH: u32 = pkg_version::pkg_version_patch!(); -fn compose_version() -> String { - format!("{}.{}.{}", MAJOR, MINOR, PATCH) +fn compose_version() -> VersionInfo { + VersionInfo { + version: format!("{}.{}.{}", MAJOR, MINOR, PATCH) + } } -pub async fn version() -> Response> { +pub async fn version() -> Response> { Ok(Json(compose_version())) } pub async fn healthcheck( Extension(rclient): Extension -) -> Response> { +) -> Response> { let mut rconn = rclient.get_async_connection().await .log_err_to_error("Failed to connect to Redis") .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; diff --git a/todored/src/routes/structs.rs b/todored/src/routes/structs.rs new file mode 100644 index 0000000..80d1116 --- /dev/null +++ b/todored/src/routes/structs.rs @@ -0,0 +1,6 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct VersionInfo { + pub version: String, +}