1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-11-20 12:02:22 +03:00

Migrate /health

This commit is contained in:
Quentin Gliech
2022-03-25 10:51:55 +01:00
parent 9b5ecd5bc4
commit 5d3b4aa182
2 changed files with 11 additions and 27 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2021 The Matrix.org Foundation C.I.C.
// Copyright 2021, 2022 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -12,36 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use hyper::header::CONTENT_TYPE;
use mas_warp_utils::{
errors::WrapError,
filters::{self, database::connection},
};
use mime::TEXT_PLAIN;
use sqlx::{pool::PoolConnection, PgPool, Postgres};
use axum::{extract::Extension, response::IntoResponse};
use mas_axum_utils::{internal_error, FancyError};
use sqlx::PgPool;
use tracing::{info_span, Instrument};
use warp::{filters::BoxedFilter, reply::with_header, Filter, Rejection, Reply};
pub fn filter(pool: &PgPool) -> BoxedFilter<(Box<dyn Reply>,)> {
warp::path!("health")
.and(filters::trace::name("GET /health"))
.and(warp::get())
.and(connection(pool))
.and_then(get)
.boxed()
}
pub async fn get(Extension(pool): Extension<PgPool>) -> Result<impl IntoResponse, FancyError> {
let mut conn = pool.acquire().await.map_err(internal_error)?;
async fn get(mut conn: PoolConnection<Postgres>) -> Result<Box<dyn Reply>, Rejection> {
sqlx::query("SELECT $1")
.bind(1_i64)
.execute(&mut conn)
.instrument(info_span!("DB health"))
.await
.wrap_error()?;
.map_err(internal_error)?;
Ok(Box::new(with_header(
"ok",
CONTENT_TYPE,
TEXT_PLAIN.to_string(),
)))
Ok("ok")
}