From 1cd72598529031f516371a5b6d4d6893ac984d97 Mon Sep 17 00:00:00 2001 From: "M. Palanikannan" <73993394+Palanikannan1437@users.noreply.github.com> Date: Tue, 3 Sep 2024 17:29:03 +0530 Subject: [PATCH] fix: parse redis url to get hostname and port (#5502) * fix: parse redis url to get hostname and port * fix: redis url accepted for connection * chore: add redis url to example env * fix: let users add redis port and host incase redis url is not present * chore: create url from host and port variables * fix: return empty string incase of no config --- live/.env.example | 7 +++++-- live/src/core/config/redis-config.ts | 17 +++++++++++++++++ live/src/server.ts | 8 ++++---- 3 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 live/src/core/config/redis-config.ts diff --git a/live/.env.example b/live/.env.example index de5e5c1f0..b7c27e825 100644 --- a/live/.env.example +++ b/live/.env.example @@ -1,3 +1,6 @@ API_BASE_URL="http://api:8000" -REDIS_HOST="localhost" -REDIS_PORT="6379" \ No newline at end of file +REDIS_URL="redis://localhost:6379" + +# If you prefer not to provide a Redis URL, you can set the REDIS_HOST and REDIS_PORT environment variables instead. +REDIS_PORT=6379 +REDIS_HOST=localhost diff --git a/live/src/core/config/redis-config.ts b/live/src/core/config/redis-config.ts new file mode 100644 index 000000000..ff52fea41 --- /dev/null +++ b/live/src/core/config/redis-config.ts @@ -0,0 +1,17 @@ +type RedisConfig = string | { host: string; port: number } | null; + +export function getRedisConfig(): RedisConfig { + const redisUrl = process.env.REDIS_URL?.trim(); + const redisHost = process.env.REDIS_HOST?.trim(); + const redisPort = process.env.REDIS_PORT?.trim(); + + if (redisUrl) { + return redisUrl; + } + + if (redisHost && redisPort && !Number.isNaN(Number(redisPort))) { + return `redis://${redisHost}:${redisPort}`; + } + + return ""; +} diff --git a/live/src/server.ts b/live/src/server.ts index b1c53b225..661004236 100644 --- a/live/src/server.ts +++ b/live/src/server.ts @@ -11,6 +11,8 @@ import { fetchPageDescriptionBinary, updatePageDescription, } from "@/core/lib/page.js"; +// config +import { getRedisConfig } from "./core/config/redis-config.js"; // types import { TDocumentTypes } from "@/core/types/common.js"; // plane live lib @@ -46,10 +48,8 @@ const server = Server.configure({ } }, extensions: [ - new Redis({ - host: process.env.REDIS_HOST || "localhost", - port: Number(process.env.REDIS_PORT || 6379), - }), + // @ts-expect-error - redis from hocuspocus is not typed properly + new Redis(getRedisConfig()), new Logger(), new Database({ fetch: async ({