feat(health): add /health endpoint + tests + docs
This commit is contained in:
parent
8f456f0cd5
commit
7aebbca98d
@ -68,6 +68,10 @@
|
|||||||
```
|
```
|
||||||
|
|
||||||
## GET /retrieve/:key
|
## 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).
|
- Paramètre de chemin: `key` (hex 64).
|
||||||
- Objet réponse (succès): `RetrieveResponse`
|
- Objet réponse (succès): `RetrieveResponse`
|
||||||
- Objet réponse (erreur): `ApiResponse`
|
- Objet réponse (erreur): `ApiResponse`
|
||||||
|
@ -4,5 +4,6 @@
|
|||||||
- Unitaires et intégration via `cargo test`.
|
- Unitaires et intégration via `cargo test`.
|
||||||
|
|
||||||
## Monitoring
|
## Monitoring
|
||||||
|
- Healthcheck HTTP: endpoint `/health` retourne `{ "message": "ok" }` et code 200.
|
||||||
- Exposer métriques avec un reverse proxy/sidecar si nécessaire.
|
- 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`.
|
||||||
|
@ -156,6 +156,12 @@ pub struct ApiResponse { pub message: String }
|
|||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct RetrieveResponse { pub key: String, pub value: String }
|
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> {
|
pub async fn handle_store(mut req: Request<StorageService>, no_ttl_permanent: bool) -> tide::Result<Response> {
|
||||||
let data: StoreRequest = match req.body_json().await {
|
let data: StoreRequest = match req.body_json().await {
|
||||||
Ok(data) => data,
|
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> {
|
pub fn create_app(no_ttl_permanent: bool, storage_dir: impl Into<String>) -> tide::Server<StorageService> {
|
||||||
let svc = StorageService::new(storage_dir);
|
let svc = StorageService::new(storage_dir);
|
||||||
let mut app = tide::with_state(svc);
|
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("/store").post(move |req| handle_store(req, no_ttl_permanent));
|
||||||
app.at("/retrieve/:key").get(handle_retrieve);
|
app.at("/retrieve/:key").get(handle_retrieve);
|
||||||
app
|
app
|
||||||
|
@ -116,3 +116,20 @@ async fn http_retrieve_success_and_invalid_and_notfound() {
|
|||||||
let res3 = client.get(format!("{}/retrieve/{}", base, k2)).await.unwrap();
|
let res3 = client.get(format!("{}/retrieve/{}", base, k2)).await.unwrap();
|
||||||
assert_eq!(res3.status(), 404);
|
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");
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user