Commit e3e9d850 authored by ThinhNC's avatar ThinhNC

Merge branch 'refactor/add-redis-url' into 'develop'

Refactor/add redis url

See merge request !21
parents 5f5319e7 9c360e12
...@@ -51,7 +51,8 @@ ...@@ -51,7 +51,8 @@
"rate-limit-redis": "^6.0.1", "rate-limit-redis": "^6.0.1",
"swagger-ui-express": "^5.0.1", "swagger-ui-express": "^5.0.1",
"turndown": "^7.2.0", "turndown": "^7.2.0",
"zod": "^3.24.1" "zod": "^3.24.1",
"prisma": "^5.22.0"
}, },
"devDependencies": { "devDependencies": {
"@types/archiver": "^6.0.3", "@types/archiver": "^6.0.3",
...@@ -73,7 +74,6 @@ ...@@ -73,7 +74,6 @@
"eslint": "^9.17.0", "eslint": "^9.17.0",
"jest": "^30.4.2", "jest": "^30.4.2",
"prettier": "^3.4.2", "prettier": "^3.4.2",
"prisma": "^5.22.0",
"swagger-autogen": "^2.23.7", "swagger-autogen": "^2.23.7",
"ts-jest": "^29.4.11", "ts-jest": "^29.4.11",
"ts-node": "^10.9.2", "ts-node": "^10.9.2",
......
...@@ -77,6 +77,9 @@ importers: ...@@ -77,6 +77,9 @@ importers:
nodemailer: nodemailer:
specifier: ^9.0.3 specifier: ^9.0.3
version: 9.0.3 version: 9.0.3
prisma:
specifier: ^5.22.0
version: 5.22.0
rate-limit-redis: rate-limit-redis:
specifier: ^6.0.1 specifier: ^6.0.1
version: 6.0.1(express-rate-limit@8.5.2(express@4.22.2)) version: 6.0.1(express-rate-limit@8.5.2(express@4.22.2))
...@@ -147,9 +150,6 @@ importers: ...@@ -147,9 +150,6 @@ importers:
prettier: prettier:
specifier: ^3.4.2 specifier: ^3.4.2
version: 3.9.4 version: 3.9.4
prisma:
specifier: ^5.22.0
version: 5.22.0
swagger-autogen: swagger-autogen:
specifier: ^2.23.7 specifier: ^2.23.7
version: 2.23.7 version: 2.23.7
......
...@@ -4,7 +4,7 @@ services: ...@@ -4,7 +4,7 @@ services:
env: node env: node
plan: free plan: free
region: singapore region: singapore
buildCommand: pnpm install --frozen-lockfile && pnpm prisma:generate && pnpm build buildCommand: pnpm install --frozen-lockfile --prod=false && pnpm prisma:generate && pnpm build
startCommand: pnpm start startCommand: pnpm start
healthCheckPath: /health/liveness healthCheckPath: /health/liveness
envVars: envVars:
......
...@@ -14,12 +14,25 @@ if (!process.env.DATABASE_URL) { ...@@ -14,12 +14,25 @@ if (!process.env.DATABASE_URL) {
process.env.DATABASE_URL = `postgresql://${user}:${password}@${host}:${port}/${name}?schema=public${ssl}`; process.env.DATABASE_URL = `postgresql://${user}:${password}@${host}:${port}/${name}?schema=public${ssl}`;
} }
const path = require("path");
const fs = require("fs");
const args = process.argv.slice(2); const args = process.argv.slice(2);
const cmd = process.platform === "win32" ? "npx.cmd" : "npx"; const isWin = process.platform === "win32";
const child = spawn(cmd, ["prisma", ...args], { const localBin = path.resolve(
__dirname,
`../node_modules/.bin/prisma${isWin ? ".cmd" : ""}`
);
const [cmd, cmdArgs] = fs.existsSync(localBin)
? [localBin, args]
: [isWin ? "npx.cmd" : "npx", ["prisma", ...args]];
const child = spawn(cmd, cmdArgs, {
stdio: "inherit", stdio: "inherit",
env: process.env, env: process.env,
shell: true, shell: isWin,
}); });
child.on("exit", (code) => process.exit(code ?? 1)); child.on("exit", (code) => process.exit(code ?? 1));
...@@ -9,12 +9,14 @@ export function getRedisClientOptions(customOpts: RedisOptions = {}): { ...@@ -9,12 +9,14 @@ export function getRedisClientOptions(customOpts: RedisOptions = {}): {
url?: string; url?: string;
options: RedisOptions; options: RedisOptions;
} { } {
const isTls = envConfig.redis.url.startsWith("rediss://");
const commonOpts: RedisOptions = { const commonOpts: RedisOptions = {
maxRetriesPerRequest: 1, maxRetriesPerRequest: 1,
lazyConnect: true, lazyConnect: true,
connectTimeout: 5000, connectTimeout: 5000,
retryStrategy: () => null, retryStrategy: () => null,
enableOfflineQueue: false, enableOfflineQueue: false,
...(isTls ? { tls: { rejectUnauthorized: false } } : {}),
...customOpts, ...customOpts,
}; };
...@@ -42,8 +44,10 @@ export function getBullMQConnection( ...@@ -42,8 +44,10 @@ export function getBullMQConnection(
extraOpts: Record<string, unknown> = {}, extraOpts: Record<string, unknown> = {},
): ConnectionOptions { ): ConnectionOptions {
if (envConfig.redis.url) { if (envConfig.redis.url) {
const isTls = envConfig.redis.url.startsWith("rediss://");
return { return {
url: envConfig.redis.url, url: envConfig.redis.url,
...(isTls ? { tls: { rejectUnauthorized: false } } : {}),
...extraOpts, ...extraOpts,
}; };
} }
......
function sanitizeRedisUrl(rawUrl: string | undefined): string {
if (!rawUrl) return "";
let url = rawUrl.trim();
if (
(url.startsWith('"') && url.endsWith('"')) ||
(url.startsWith("'") && url.endsWith("'"))
) {
url = url.slice(1, -1).trim();
}
const match = url.match(/rediss?:\/\/[^\s'"]+/i);
if (match) {
url = match[0];
}
if (
url.startsWith("redis://") &&
(url.includes("upstash.io") || rawUrl.includes("--tls"))
) {
url = url.replace(/^redis:\/\//i, "rediss://");
}
return url;
}
export const envConfig = { export const envConfig = {
nodeEnv: process.env.NODE_ENV || "development", nodeEnv: process.env.NODE_ENV || "development",
port: parseInt(process.env.PORT || "9898", 10), port: parseInt(process.env.PORT || "9898", 10),
...@@ -52,12 +74,13 @@ export const envConfig = { ...@@ -52,12 +74,13 @@ export const envConfig = {
), ),
}, },
redis: { redis: {
url: process.env.REDIS_URL || "", url: sanitizeRedisUrl(process.env.REDIS_URL),
host: process.env.REDIS_HOST || "127.0.0.1", host: process.env.REDIS_HOST || "127.0.0.1",
port: parseInt(process.env.REDIS_PORT || "6379", 10), port: parseInt(process.env.REDIS_PORT || "6379", 10),
password: process.env.REDIS_PASSWORD || undefined, password: process.env.REDIS_PASSWORD || undefined,
enabled: enabled:
process.env.REDIS_ENABLED === "true" || Boolean(process.env.REDIS_URL), process.env.REDIS_ENABLED === "true" ||
Boolean(sanitizeRedisUrl(process.env.REDIS_URL)),
}, },
rateLimit: { rateLimit: {
windowMs: parseInt(process.env.RATE_LIMIT_WINDOW_MS || "900000", 10), windowMs: parseInt(process.env.RATE_LIMIT_WINDOW_MS || "900000", 10),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment