feat(infra): add external redis (#3639)
This commit is contained in:
12
packages/core/src/infrastructure/env/index.ts
vendored
Normal file
12
packages/core/src/infrastructure/env/index.ts
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
import { createEnv as createEnvT3 } from "@t3-oss/env-nextjs";
|
||||
|
||||
export const defaultEnvOptions = {
|
||||
emptyStringAsUndefined: true,
|
||||
skipValidation:
|
||||
Boolean(process.env.CI) || Boolean(process.env.SKIP_ENV_VALIDATION) || process.env.npm_lifecycle_event === "lint",
|
||||
} satisfies Partial<Parameters<typeof createEnvT3>[0]>;
|
||||
|
||||
export const createEnv: typeof createEnvT3 = (options) => createEnvT3({ ...defaultEnvOptions, ...options });
|
||||
|
||||
export * from "./prefix";
|
||||
export * from "./schemas";
|
||||
13
packages/core/src/infrastructure/env/prefix.ts
vendored
Normal file
13
packages/core/src/infrastructure/env/prefix.ts
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
export const runtimeEnvWithPrefix = (prefix: `${string}_`) =>
|
||||
Object.entries(process.env)
|
||||
.filter(([key]) => key.startsWith(prefix))
|
||||
.reduce(
|
||||
(acc, [key, value]) => {
|
||||
if (value === undefined) return acc;
|
||||
|
||||
const newKey = key.replace(prefix, "");
|
||||
acc[newKey] = value;
|
||||
return acc;
|
||||
},
|
||||
{} as Record<string, string>,
|
||||
);
|
||||
39
packages/core/src/infrastructure/env/schemas.ts
vendored
Normal file
39
packages/core/src/infrastructure/env/schemas.ts
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
import { z } from "zod";
|
||||
|
||||
const trueStrings = ["1", "yes", "t", "true"];
|
||||
const falseStrings = ["0", "no", "f", "false"];
|
||||
|
||||
export const createBooleanSchema = (defaultValue: boolean) =>
|
||||
z
|
||||
.string()
|
||||
.default(defaultValue.toString())
|
||||
.transform((value, ctx) => {
|
||||
const normalized = value.trim().toLowerCase();
|
||||
if (trueStrings.includes(normalized)) return true;
|
||||
if (falseStrings.includes(normalized)) return false;
|
||||
|
||||
throw new Error(`Invalid boolean value for ${ctx.path.join(".")}`);
|
||||
});
|
||||
|
||||
export const createDurationSchema = (defaultValue: `${number}${"s" | "m" | "h" | "d"}`) =>
|
||||
z
|
||||
.string()
|
||||
.regex(/^\d+[smhd]?$/)
|
||||
.default(defaultValue)
|
||||
.transform((duration) => {
|
||||
const lastChar = duration[duration.length - 1] as "s" | "m" | "h" | "d";
|
||||
if (!isNaN(Number(lastChar))) {
|
||||
return Number(defaultValue);
|
||||
}
|
||||
|
||||
const multipliers = {
|
||||
s: 1,
|
||||
m: 60,
|
||||
h: 60 * 60,
|
||||
d: 60 * 60 * 24,
|
||||
};
|
||||
const numberDuration = Number(duration.slice(0, -1));
|
||||
const multiplier = multipliers[lastChar];
|
||||
|
||||
return numberDuration * multiplier;
|
||||
});
|
||||
26
packages/core/src/infrastructure/redis/client.ts
Normal file
26
packages/core/src/infrastructure/redis/client.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import type { RedisOptions } from "ioredis";
|
||||
import { Redis } from "ioredis";
|
||||
|
||||
import { redisEnv } from "./env";
|
||||
|
||||
const defaultRedisOptions = {
|
||||
connectionName: "homarr",
|
||||
} satisfies RedisOptions;
|
||||
|
||||
export type { Redis as RedisClient } from "ioredis";
|
||||
|
||||
export const createRedisClient = () =>
|
||||
redisEnv.IS_EXTERNAL
|
||||
? new Redis({
|
||||
...defaultRedisOptions,
|
||||
host: redisEnv.HOST,
|
||||
port: redisEnv.PORT,
|
||||
tls: redisEnv.TLS_CA
|
||||
? {
|
||||
ca: redisEnv.TLS_CA,
|
||||
}
|
||||
: undefined,
|
||||
username: redisEnv.USERNAME,
|
||||
password: redisEnv.PASSWORD,
|
||||
})
|
||||
: new Redis(defaultRedisOptions);
|
||||
17
packages/core/src/infrastructure/redis/env.ts
Normal file
17
packages/core/src/infrastructure/redis/env.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import { createEnv } from "../env";
|
||||
import { runtimeEnvWithPrefix } from "../env/prefix";
|
||||
import { createBooleanSchema } from "../env/schemas";
|
||||
|
||||
export const redisEnv = createEnv({
|
||||
server: {
|
||||
IS_EXTERNAL: createBooleanSchema(false),
|
||||
HOST: z.string().optional(),
|
||||
PORT: z.coerce.number().default(6379).optional(),
|
||||
TLS_CA: z.string().optional(),
|
||||
USERNAME: z.string().optional(),
|
||||
PASSWORD: z.string().optional(),
|
||||
},
|
||||
runtimeEnv: runtimeEnvWithPrefix("REDIS_"),
|
||||
});
|
||||
Reference in New Issue
Block a user