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
This commit is contained in:
M. Palanikannan 2024-09-03 17:29:03 +05:30 committed by GitHub
parent 5840b40d96
commit 1cd7259852
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 26 additions and 6 deletions

View file

@ -1,3 +1,6 @@
API_BASE_URL="http://api:8000" API_BASE_URL="http://api:8000"
REDIS_HOST="localhost" REDIS_URL="redis://localhost:6379"
REDIS_PORT="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

View file

@ -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 "";
}

View file

@ -11,6 +11,8 @@ import {
fetchPageDescriptionBinary, fetchPageDescriptionBinary,
updatePageDescription, updatePageDescription,
} from "@/core/lib/page.js"; } from "@/core/lib/page.js";
// config
import { getRedisConfig } from "./core/config/redis-config.js";
// types // types
import { TDocumentTypes } from "@/core/types/common.js"; import { TDocumentTypes } from "@/core/types/common.js";
// plane live lib // plane live lib
@ -46,10 +48,8 @@ const server = Server.configure({
} }
}, },
extensions: [ extensions: [
new Redis({ // @ts-expect-error - redis from hocuspocus is not typed properly
host: process.env.REDIS_HOST || "localhost", new Redis(getRedisConfig()),
port: Number(process.env.REDIS_PORT || 6379),
}),
new Logger(), new Logger(),
new Database({ new Database({
fetch: async ({ fetch: async ({