diff --git a/Cargo.toml b/Cargo.toml index e8e4f4f..03ca85a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ rust-version = "1.77.2" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -axum = { version = "0.7.4", features = ["macros", "original-uri"] } +axum = { version = "0.7.4", features = ["macros", "original-uri", "json"] } bytes = "1.5.0" clap = { version = "4.5.1", features = ["cargo"] } form_urlencoded = "1.2.1" diff --git a/src/api/ha_registry/v1/mod.rs b/src/api/ha_registry/v1/mod.rs index 1271a0c..f46640a 100644 --- a/src/api/ha_registry/v1/mod.rs +++ b/src/api/ha_registry/v1/mod.rs @@ -4,3 +4,5 @@ // SPDX-License-Identifier: AGPL-3.0-only pub mod routes; + +mod status; diff --git a/src/api/ha_registry/v1/routes.rs b/src/api/ha_registry/v1/routes.rs index 66810f5..35e830c 100644 --- a/src/api/ha_registry/v1/routes.rs +++ b/src/api/ha_registry/v1/routes.rs @@ -5,6 +5,8 @@ use axum::{routing::get, Router}; +use super::status::{self, handlers::status}; + pub fn get_routes() -> Router { - Router::new().route("/status", get(|| async { "working" })) + Router::new().route("/status", get(status)) } diff --git a/src/api/ha_registry/v1/status/handlers.rs b/src/api/ha_registry/v1/status/handlers.rs new file mode 100644 index 0000000..86e1068 --- /dev/null +++ b/src/api/ha_registry/v1/status/handlers.rs @@ -0,0 +1,17 @@ +// SPDX-FileCopyrightText: 2024 Christina Sørensen +// SPDX-FileContributor: Christina Sørensen +// +// SPDX-License-Identifier: AGPL-3.0-only + +use crate::data::status::{Status, ServerState}; +use axum::{Json, http::StatusCode}; + +const TEST_STATUS: Status = Status { server_state: ServerState::Healthy }; + +/// Handler for returning the server status. +pub async fn status() -> Result, StatusCode> { + Ok(Json(TEST_STATUS)) +} +// pub async fn status() -> StatusCode { +// StatusCode::OK +// } diff --git a/src/api/ha_registry/v1/status/mod.rs b/src/api/ha_registry/v1/status/mod.rs new file mode 100644 index 0000000..c3d4495 --- /dev/null +++ b/src/api/ha_registry/v1/status/mod.rs @@ -0,0 +1 @@ +pub mod handlers; diff --git a/src/data/mod.rs b/src/data/mod.rs index 03f932f..361c12d 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -1,7 +1,10 @@ -// SPDX-FileCopyrightText: 2023 Christina Sørensen +// SPDX-FileCopyrightText: 2024 Christina Sørensen // SPDX-FileContributor: Christina Sørensen // // SPDX-License-Identifier: AGPL-3.0-only +pub mod status; + mod config; + pub use self::config::{Config, CONFIG}; diff --git a/src/data/status.rs b/src/data/status.rs new file mode 100644 index 0000000..d3b151e --- /dev/null +++ b/src/data/status.rs @@ -0,0 +1,27 @@ +// SPDX-FileCopyrightText: 2024 Christina Sørensen +// SPDX-FileContributor: Christina Sørensen +// +// SPDX-License-Identifier: AGPL-3.0-only + +use serde::{Deserialize, Serialize}; + +/// The state of the server. +/// +/// - Healthy: everything is working +/// - Degraded: something is broken, but ha-registry can still run +/// - Unhealthy: ha-registry is not working +/// +/// May be expanded later. +#[derive(Serialize, Deserialize)] +pub enum ServerState { + Healthy, + Degraded, + Unhealthy, +} + +/// Status struct exposed over status endpoint. Used to see the current state of +/// ha-registry. +#[derive(Serialize, Deserialize)] +pub struct Status { + pub server_state: ServerState, +}