1
Fork 0
mirror of https://github.com/Steffo99/todocolors.git synced 2024-11-22 08:14:18 +00:00

Endpoints which return the version now return a JSON object

This commit is contained in:
Steffo 2023-08-22 04:18:01 +02:00
parent 823de0af36
commit 8bcedd9fbf
Signed by: steffo
GPG key ID: 2A24051445686895
4 changed files with 23 additions and 10 deletions

View file

@ -17,10 +17,10 @@ async fn main() {
.expect("to be able to connect to Redis"); .expect("to be able to connect to Redis");
let router = axum::Router::new() let router = axum::Router::new()
.route("/", get(routes::root::version)) .route("/", get(routes::version_route))
.route("/version", get(routes::root::version)) .route("/version", get(routes::version_route))
.route("/", post(routes::root::healthcheck)) .route("/", post(routes::healthcheck_route))
.route("/healthcheck", post(routes::root::healthcheck)) .route("/healthcheck", post(routes::healthcheck_route))
.route("/board/:board/ws", get(routes::board::board_websocket)) .route("/board/:board/ws", get(routes::board::board_websocket))
.layer(axum::Extension(rclient)) .layer(axum::Extension(rclient))
.layer(tower_http::cors::CorsLayer::new() .layer(tower_http::cors::CorsLayer::new()

View file

@ -1,2 +1,6 @@
pub(crate) mod root; pub mod board;
pub(crate) 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;

View file

@ -2,23 +2,26 @@ use axum::{Extension, Json};
use axum::http::StatusCode; use axum::http::StatusCode;
use crate::outcome::{Response, LoggableOutcome}; use crate::outcome::{Response, LoggableOutcome};
use crate::routes::structs::VersionInfo;
const MAJOR: u32 = pkg_version::pkg_version_major!(); const MAJOR: u32 = pkg_version::pkg_version_major!();
const MINOR: u32 = pkg_version::pkg_version_minor!(); const MINOR: u32 = pkg_version::pkg_version_minor!();
const PATCH: u32 = pkg_version::pkg_version_patch!(); const PATCH: u32 = pkg_version::pkg_version_patch!();
fn compose_version() -> String { fn compose_version() -> VersionInfo {
format!("{}.{}.{}", MAJOR, MINOR, PATCH) VersionInfo {
version: format!("{}.{}.{}", MAJOR, MINOR, PATCH)
}
} }
pub async fn version() -> Response<Json<String>> { pub async fn version() -> Response<Json<VersionInfo>> {
Ok(Json(compose_version())) Ok(Json(compose_version()))
} }
pub async fn healthcheck( pub async fn healthcheck(
Extension(rclient): Extension<redis::Client> Extension(rclient): Extension<redis::Client>
) -> Response<Json<String>> { ) -> Response<Json<VersionInfo>> {
let mut rconn = rclient.get_async_connection().await let mut rconn = rclient.get_async_connection().await
.log_err_to_error("Failed to connect to Redis") .log_err_to_error("Failed to connect to Redis")
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;

View file

@ -0,0 +1,6 @@
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct VersionInfo {
pub version: String,
}