mas_handlers/
health.rs

1// Copyright 2024 New Vector Ltd.
2// Copyright 2021-2024 The Matrix.org Foundation C.I.C.
3//
4// SPDX-License-Identifier: AGPL-3.0-only
5// Please see LICENSE in the repository root for full details.
6
7use axum::{extract::State, response::IntoResponse};
8use mas_axum_utils::FancyError;
9use sqlx::PgPool;
10use tracing::{Instrument, info_span};
11
12pub async fn get(State(pool): State<PgPool>) -> Result<impl IntoResponse, FancyError> {
13    let mut conn = pool.acquire().await?;
14
15    sqlx::query("SELECT $1")
16        .bind(1_i64)
17        .execute(&mut *conn)
18        .instrument(info_span!("DB health"))
19        .await?;
20
21    Ok("ok")
22}
23
24#[cfg(test)]
25mod tests {
26    use hyper::{Request, StatusCode};
27
28    use super::*;
29    use crate::test_utils::{RequestBuilderExt, ResponseExt, TestState, setup};
30
31    #[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]
32    async fn test_get_health(pool: PgPool) {
33        setup();
34        let state = TestState::from_pool(pool).await.unwrap();
35        let request = Request::get("/health").empty();
36
37        let response = state.request(request).await;
38        response.assert_status(StatusCode::OK);
39        assert_eq!(response.body(), "ok");
40    }
41}