feat: implement API versioning and health check endpoints

This commit is contained in:
itsscb 2025-03-12 22:36:54 +01:00
parent 94247c5693
commit 8499636209
7 changed files with 53 additions and 12 deletions

View File

@ -1,11 +1,13 @@
[package] [package]
name = "digitaler-frieden" name = "digitaler-frieden"
version = "0.1.0" version = "0.1.1"
edition = "2021" edition = "2024"
[dependencies] [dependencies]
axum = "0.8.1" axum = "0.8.1"
shuttle-axum = "0.52.0" serde_json = "1.0.140"
shuttle-runtime = "0.52.0" shuttle-axum = "0.53.0"
shuttle-runtime = "0.53.0"
tokio = "1.28.2" tokio = "1.28.2"
tower-http = { version = "0.6.2", features = ["fs"] } tower-http = { version = "0.6.2", features = ["fs"] }
tracing = "0.1.41"

13
src/api.rs Normal file
View File

@ -0,0 +1,13 @@
use crate::api::v1::{get_version, health_check};
use axum::{routing, Router};
pub mod v1;
pub fn new() -> Router {
Router::new().nest(
"/v1",
Router::new()
.route("/version", routing::get(get_version))
.route("/health", routing::get(health_check)),
)
}

5
src/api/v1.rs Normal file
View File

@ -0,0 +1,5 @@
mod health;
mod version;
pub use health::health_check;
pub use version::get_version;

5
src/api/v1/health.rs Normal file
View File

@ -0,0 +1,5 @@
use axum::response::IntoResponse;
pub async fn health_check() -> impl IntoResponse {
"OK"
}

10
src/api/v1/version.rs Normal file
View File

@ -0,0 +1,10 @@
use crate::VERSION;
use axum::response::IntoResponse;
use axum::Json;
use serde_json;
use tracing::instrument;
#[instrument()]
pub async fn get_version() -> impl IntoResponse {
Json(serde_json::json!({ "version": VERSION }))
}

13
src/lib.rs Normal file
View File

@ -0,0 +1,13 @@
mod api;
use axum::Router;
use tower_http::services::{ServeDir, ServeFile};
const VERSION: &str = env!("CARGO_PKG_VERSION");
pub fn new() -> Router {
Router::new().nest("/api", api::new()).fallback_service(
ServeDir::new("frontend/dist/frontend/browser")
.not_found_service(ServeFile::new("frontend/dist/frontend/browser/index.html")),
)
}

View File

@ -1,13 +1,6 @@
use axum::Router;
use tower_http::services::{ServeDir, ServeFile};
#[shuttle_runtime::main] #[shuttle_runtime::main]
#[allow(clippy::unused_async)] #[allow(clippy::unused_async)]
async fn main() -> shuttle_axum::ShuttleAxum { async fn main() -> shuttle_axum::ShuttleAxum {
let router = Router::new().fallback_service( let router = digitaler_frieden::new();
ServeDir::new("frontend/dist/frontend/browser")
.not_found_service(ServeFile::new("frontend/dist/frontend/browser/index.html")),
);
Ok(router.into()) Ok(router.into())
} }