feat(infra): add external redis (#3639)

This commit is contained in:
Meier Lukas
2025-07-20 17:13:57 +02:00
committed by GitHub
parent 732bce72ae
commit 8e960324bc
32 changed files with 201 additions and 84 deletions

View File

@@ -0,0 +1,4 @@
import baseConfig from "@homarr/eslint-config/base";
/** @type {import('typescript-eslint').Config} */
export default [...baseConfig];

View File

@@ -0,0 +1,38 @@
{
"name": "@homarr/core",
"version": "0.1.0",
"private": true,
"license": "Apache-2.0",
"type": "module",
"exports": {
"./infrastructure/redis": "./src/infrastructure/redis/client.ts",
"./infrastructure/env": "./src/infrastructure/env/index.ts",
".": "./src/index.ts"
},
"typesVersions": {
"*": {
"*": [
"src/*"
]
}
},
"scripts": {
"clean": "rm -rf .turbo node_modules",
"format": "prettier --check . --ignore-path ../../.gitignore",
"lint": "eslint",
"typecheck": "tsc --noEmit"
},
"prettier": "@homarr/prettier-config",
"dependencies": {
"@t3-oss/env-nextjs": "^0.13.8",
"ioredis": "5.6.1",
"zod": "^3.25.76"
},
"devDependencies": {
"@homarr/eslint-config": "workspace:^0.2.0",
"@homarr/prettier-config": "workspace:^0.1.0",
"@homarr/tsconfig": "workspace:^0.1.0",
"eslint": "^9.31.0",
"typescript": "^5.8.3"
}
}

View 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";

View 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>,
);

View 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;
});

View 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);

View 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_"),
});

View File

@@ -0,0 +1,8 @@
{
"extends": "@homarr/tsconfig/base.json",
"compilerOptions": {
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
},
"include": ["*.ts", "src"],
"exclude": ["node_modules"]
}