From a82da927b0c3da7e291aa6132d5c7932f445b8a6 Mon Sep 17 00:00:00 2001 From: Ethan Girouard Date: Sat, 3 May 2025 05:30:17 +0000 Subject: [PATCH 1/3] Move redis connection to util/redis.rs --- src/main.rs | 17 +++-------------- src/util/mod.rs | 1 + src/util/redis.rs | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 14 deletions(-) create mode 100644 src/util/redis.rs diff --git a/src/main.rs b/src/main.rs index f116dc8..0060fdd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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); diff --git a/src/util/mod.rs b/src/util/mod.rs index 6242368..2824260 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -7,6 +7,7 @@ cfg_if! { pub mod fileserv; pub mod database; pub mod auth_backend; + pub mod redis; } } diff --git a/src/util/redis.rs b/src/util/redis.rs new file mode 100644 index 0000000..a3652e4 --- /dev/null +++ b/src/util/redis.rs @@ -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() +} -- 2.49.0 From 6bb6322aa4dbe4ae2b80f17c61b6f569874bd277 Mon Sep 17 00:00:00 2001 From: Ethan Girouard Date: Sat, 3 May 2025 05:30:34 +0000 Subject: [PATCH 2/3] Create health check endpoint --- src/api/health.rs | 23 +++++++++++++++++++++++ src/api/mod.rs | 1 + 2 files changed, 24 insertions(+) create mode 100644 src/api/health.rs diff --git a/src/api/health.rs b/src/api/health.rs new file mode 100644 index 0000000..d18668d --- /dev/null +++ b/src/api/health.rs @@ -0,0 +1,23 @@ +use leptos::prelude::*; + +use crate::util::serverfn_client::Client; + +#[server(endpoint = "health", client = Client)] +pub async fn health() -> Result { + 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::::ServerError(format!("Database error: {e}")))?; + + get_redis_conn() + .ping::<()>(None) + .await + .map_err(|e| ServerFnError::::ServerError(format!("Redis error: {e}")))?; + + Ok("ok".to_string()) +} diff --git a/src/api/mod.rs b/src/api/mod.rs index e0d7f22..c49aed9 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -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; -- 2.49.0 From e2a395ae7cfd7ba7fd932a6eec5a06f80724e38e Mon Sep 17 00:00:00 2001 From: Ethan Girouard Date: Sat, 3 May 2025 05:30:47 +0000 Subject: [PATCH 3/3] Don't require auth for health check endpoint --- src/util/require_auth.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/util/require_auth.rs b/src/util/require_auth.rs index f8cd860..4ee1c58 100644 --- a/src/util/require_auth.rs +++ b/src/util/require_auth.rs @@ -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", ]; -- 2.49.0