feat(health): add /health endpoint + tests + docs
Some checks failed
CI / rust (push) Failing after 30s
Docker Image / docker (push) Failing after 19s

This commit is contained in:
Your Name 2025-08-26 11:16:33 +02:00
parent 8f456f0cd5
commit 7aebbca98d
4 changed files with 30 additions and 1 deletions

View File

@ -68,6 +68,10 @@
```
## GET /retrieve/:key
## GET /health
- Aucune donnée d'entrée.
- Réponse 200 avec `ApiResponse` `{ "message": "ok" }`.
- Paramètre de chemin: `key` (hex 64).
- Objet réponse (succès): `RetrieveResponse`
- Objet réponse (erreur): `ApiResponse`

View File

@ -4,5 +4,6 @@
- Unitaires et intégration via `cargo test`.
## Monitoring
- Healthcheck HTTP: endpoint `/health` retourne `{ "message": "ok" }` et code 200.
- Exposer métriques avec un reverse proxy/sidecar si nécessaire.
- Ajouter des healthchecks HTTP au niveau de l'orchestrateur.
- Configurer l'orchestrateur pour vérifier périodiquement `/health`.

View File

@ -156,6 +156,12 @@ pub struct ApiResponse { pub message: String }
#[derive(Serialize)]
pub struct RetrieveResponse { pub key: String, pub value: String }
pub async fn handle_health(_req: Request<StorageService>) -> tide::Result<Response> {
Ok(Response::builder(StatusCode::Ok)
.body(serde_json::to_value(&ApiResponse { message: "ok".into() })?)
.build())
}
pub async fn handle_store(mut req: Request<StorageService>, no_ttl_permanent: bool) -> tide::Result<Response> {
let data: StoreRequest = match req.body_json().await {
Ok(data) => data,
@ -246,6 +252,7 @@ pub async fn handle_retrieve(req: Request<StorageService>) -> tide::Result<Respo
pub fn create_app(no_ttl_permanent: bool, storage_dir: impl Into<String>) -> tide::Server<StorageService> {
let svc = StorageService::new(storage_dir);
let mut app = tide::with_state(svc);
app.at("/health").get(handle_health);
app.at("/store").post(move |req| handle_store(req, no_ttl_permanent));
app.at("/retrieve/:key").get(handle_retrieve);
app

View File

@ -116,3 +116,20 @@ async fn http_retrieve_success_and_invalid_and_notfound() {
let res3 = client.get(format!("{}/retrieve/{}", base, k2)).await.unwrap();
assert_eq!(res3.status(), 404);
}
#[async_std::test]
async fn http_health_ok() {
let td = TempDir::new().unwrap();
let storage = td.path().to_string_lossy().to_string();
let mut app = create_app(true, storage);
let listener = async_std::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
async_std::task::spawn(async move { app.listen(listener).await.unwrap() });
let client = Client::new();
let base = format!("http://{}", addr);
let mut res = client.get(format!("{}/health", base)).await.unwrap();
assert!(res.status().is_success());
let v: serde_json::Value = res.body_json().await.unwrap();
assert_eq!(v["message"].as_str().unwrap(), "ok");
}