From d642469fa7396cb1ddc5e6c13f295c789e1951dc Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Mon, 13 Nov 2023 21:45:31 +0100 Subject: [PATCH] tmp --- server/calendar/src/main.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/server/calendar/src/main.rs b/server/calendar/src/main.rs index 49578b123..d29b9d07e 100644 --- a/server/calendar/src/main.rs +++ b/server/calendar/src/main.rs @@ -6,9 +6,10 @@ use actix_cors::Cors; use actix_web::{get, middleware, web, App, HttpResponse, HttpServer}; use actix_web_prom::PrometheusMetricsBuilder; use sqlx::postgres::PgPoolOptions; -use sqlx::PgPool; +use sqlx::{Executor, PgPool}; use std::collections::HashMap; use std::error::Error; +use log::error; use structured_logger::async_json::new_writer; use structured_logger::Builder; @@ -20,14 +21,22 @@ pub struct AppData { const MAX_JSON_PAYLOAD: usize = 1024 * 1024; // 1 MB #[get("/api/calendar/status")] -async fn health_status_handler() -> HttpResponse { +async fn health_status_handler(data: web::Data) -> HttpResponse { let github_link = match std::env::var("GIT_COMMIT_SHA") { Ok(hash) => format!("https://github.com/TUM-Dev/navigatum/tree/{hash}"), Err(_) => "unknown commit hash, probably running in development".to_string(), }; - HttpResponse::Ok() - .content_type("text/plain") - .body(format!("healthy\nsource_code: {github_link}")) + return match data.db.execute("SELECT 1").await { + Ok(_) => HttpResponse::Ok() + .content_type("text/plain") + .body(format!("healthy\nsource_code: {github_link}")), + Err(e) => { + error!("database error: {e:?}",); + HttpResponse::InternalServerError() + .content_type("text/plain") + .body(format!("unhealthy\nsource_code: {github_link}")) + } + }; } #[tokio::main]