Create health check endpoint #213

Merged
eta357 merged 3 commits from 206-create-health-check-endpoint into main 2025-05-03 05:34:05 +00:00
6 changed files with 63 additions and 15 deletions

23
src/api/health.rs Normal file
View File

@ -0,0 +1,23 @@
use leptos::prelude::*;
use crate::util::serverfn_client::Client;
#[server(endpoint = "health", client = Client)]
pub async fn health() -> Result<String, ServerFnError> {
use crate::util::database::get_db_conn;
use crate::util::redis::get_redis_conn;
use diesel::connection::SimpleConnection;
use server_fn::error::NoCustomError;
use tower_sessions_redis_store::fred::interfaces::ClientLike;
get_db_conn()
.batch_execute("SELECT 1")
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Database error: {e}")))?;
get_redis_conn()
.ping::<()>(None)
.await
.map_err(|e| ServerFnError::<NoCustomError>::ServerError(format!("Redis error: {e}")))?;
Ok("ok".to_string())
}

View File

@ -2,6 +2,7 @@ pub mod album;
pub mod albums;
pub mod artists;
pub mod auth;
pub mod health;
pub mod history;
pub mod profile;
pub mod search;

View File

@ -16,10 +16,10 @@ async fn main() {
use libretunes::util::fileserv::{
file_and_error_handler, get_asset_file, get_static_file, AssetType,
};
use libretunes::util::redis::get_redis_pool;
use libretunes::util::require_auth::require_auth_middleware;
use log::*;
use tower_sessions_redis_store::fred;
use tower_sessions_redis_store::{fred::prelude::*, RedisStore};
use tower_sessions_redis_store::RedisStore;
flexi_logger::Logger::try_with_env_or_str("debug")
.unwrap()
@ -39,18 +39,7 @@ async fn main() {
libretunes::util::database::migrate();
debug!("Connecting to Redis...");
let redis_url = std::env::var("REDIS_URL").expect("REDIS_URL must be set");
let redis_config = fred::types::config::Config::from_url(&redis_url)
.unwrap_or_else(|_| panic!("Unable to parse Redis URL: {redis_url}"));
let redis_pool = fred::clients::Pool::new(redis_config, None, None, None, 1)
.expect("Unable to create Redis pool");
redis_pool.connect();
redis_pool
.wait_for_connect()
.await
.expect("Unable to connect to Redis");
let redis_pool = get_redis_pool();
let session_store = RedisStore::new(redis_pool);
let session_layer = SessionManagerLayer::new(session_store);

View File

@ -7,6 +7,7 @@ cfg_if! {
pub mod fileserv;
pub mod database;
pub mod auth_backend;
pub mod redis;
}
}

33
src/util/redis.rs Normal file
View File

@ -0,0 +1,33 @@
use leptos::logging::log;
use lazy_static::lazy_static;
use std::env;
use tower_sessions_redis_store::fred;
use tower_sessions_redis_store::fred::prelude::*;
lazy_static! {
static ref REDIS_POOL: fred::clients::Pool = init_redis_pool();
}
fn init_redis_pool() -> fred::clients::Pool {
let redis_url = env::var("REDIS_URL").expect("REDIS_URL must be set");
let redis_config = fred::types::config::Config::from_url(&redis_url)
.unwrap_or_else(|_| panic!("Unable to parse Redis URL: {redis_url}"));
let redis_pool = fred::clients::Pool::new(redis_config, None, None, None, 1)
.expect("Unable to create Redis pool");
log!("Connecting to Redis: {redis_url}");
redis_pool.connect();
redis_pool
}
pub fn get_redis_pool() -> fred::clients::Pool {
REDIS_POOL.clone()
}
pub fn get_redis_conn() -> fred::clients::Client {
REDIS_POOL.next().clone()
}

View File

@ -10,11 +10,12 @@ use crate::util::auth_backend::AuthBackend;
use axum::extract::FromRequestParts;
// Things in pkg/ are allowed automatically. This includes the CSS/JS/WASM files
const ALLOWED_PATHS: [&str; 5] = [
const ALLOWED_PATHS: [&str; 6] = [
"/login",
"/signup",
"/api/login",
"/api/signup",
"/api/health",
"/favicon.ico",
];