openzeppelin_relayer/api/routes/
health.rs

1//! This module provides a health check endpoint for the API.
2//!
3//! The `/health` endpoint can be used to verify that the service is running and responsive.
4use actix_web::{get, web, HttpResponse};
5
6/// Health routes implementation
7///
8/// Note: OpenAPI documentation for these endpoints can be found in the `openapi.rs` file
9/// Handles the `/health` endpoint.
10///
11/// Returns an `HttpResponse` with a status of `200 OK` and a body of `"OK"`.
12#[utoipa::path(
13    get,
14    path = "/v1/health",
15    tag = "Health",
16    responses(
17        (status = 200, description = "Service is healthy", body = String),
18        (status = 500, description = "Internal server error", body = String),
19    )
20)]
21#[get("/health")]
22async fn health() -> Result<HttpResponse, actix_web::Error> {
23    Ok(HttpResponse::Ok().body("OK"))
24}
25
26/// Initializes the health check service.
27///
28/// Registers the `health` endpoint with the provided service configuration.
29pub fn init(cfg: &mut web::ServiceConfig) {
30    cfg.service(health);
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36    use actix_web::{test, App};
37
38    #[actix_web::test]
39    async fn test_health_endpoint() {
40        // Arrange
41        let app = test::init_service(App::new().configure(init)).await;
42
43        // Act
44        let req = test::TestRequest::get().uri("/health").to_request();
45        let resp = test::call_service(&app, req).await;
46
47        // Assert
48        assert!(resp.status().is_success());
49
50        let body = test::read_body(resp).await;
51        assert_eq!(body, "OK");
52    }
53}