fix: Strict env enforcement, rock-solid Alpine portal rendering, and Nabeh/Redis test diagnostics
This commit is contained in:
@@ -4,7 +4,7 @@ namespace App\Core;
|
||||
|
||||
/**
|
||||
* Core Redis Client for managing connections.
|
||||
* Handles Sessions, Rate Limiting, and caching using PHP Redis extension.
|
||||
* Strict environment variable enforcement (No default fallbacks).
|
||||
*/
|
||||
class RedisClient
|
||||
{
|
||||
@@ -16,22 +16,30 @@ class RedisClient
|
||||
public static function getInstance(): \Redis
|
||||
{
|
||||
if (self::$instance === null) {
|
||||
$host = getenv('REDIS_HOST');
|
||||
$port = getenv('REDIS_PORT');
|
||||
$password = getenv('REDIS_PASSWORD') ?: null;
|
||||
|
||||
$missing = [];
|
||||
if ($host === false || $host === '') $missing[] = 'REDIS_HOST';
|
||||
if ($port === false || $port === '') $missing[] = 'REDIS_PORT';
|
||||
|
||||
if (!empty($missing)) {
|
||||
throw new \RuntimeException("Redis Configuration Error: Missing environment variable(s): " . implode(', ', $missing));
|
||||
}
|
||||
|
||||
try {
|
||||
$redis = new \Redis();
|
||||
|
||||
$host = getenv('REDIS_HOST') ?: '127.0.0.1';
|
||||
$port = (int)(getenv('REDIS_PORT') ?: 6379);
|
||||
$password = getenv('REDIS_PASSWORD') ?: null;
|
||||
|
||||
// Connect with a 2 second timeout
|
||||
if (!$redis->connect($host, $port, 2.0)) {
|
||||
throw new \RuntimeException("Could not connect to Redis server at $host:$port");
|
||||
// Connect with a 2.5 second timeout
|
||||
if (!$redis->connect($host, (int)$port, 2.5)) {
|
||||
throw new \RuntimeException("Could not connect to Redis server at {$host}:{$port}");
|
||||
}
|
||||
|
||||
// Authenticate if password is provided
|
||||
if ($password) {
|
||||
if (!$redis->auth($password)) {
|
||||
throw new \RuntimeException("Redis authentication failed.");
|
||||
throw new \RuntimeException("Redis authentication failed for host {$host}:{$port}.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,9 +48,8 @@ class RedisClient
|
||||
|
||||
self::$instance = $redis;
|
||||
} catch (\Exception $e) {
|
||||
// In production, fallback gracefully or throw HTTP 500
|
||||
error_log("Redis Connection Error: " . $e->getMessage());
|
||||
throw new \RuntimeException("Redis is unavailable. Please ensure the Redis server is running.");
|
||||
throw new \RuntimeException("Redis connection failed ({$host}:{$port}): " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user