Zod validated config for security and ease of getting started

This commit is contained in:
Kalle
2026-06-15 14:12:15 +03:00
parent 215243e071
commit 75179e5077
46 changed files with 386 additions and 275 deletions

View File

@@ -1,4 +1,5 @@
import type { CacheEntry } from "@epic-web/cachified";
import { ServerConfig } from "~/config.server";
import { LRUCache } from "~/modules/cache";
declare global {
@@ -11,8 +12,7 @@ export const cache = (global.__lruCache = global.__lruCache
? global.__lruCache
: new LRUCache<string, CacheEntry<unknown>>({ max: 5000 }));
export const ttl = (ms: number) =>
process.env.DISABLE_CACHE === "true" ? 0 : ms;
export const ttl = (ms: number) => (ServerConfig.disableCache ? 0 : ms);
export function syncCached<T>(key: string, getFreshValue: () => T) {
if (cache.has(key)) {

View File

@@ -6,6 +6,7 @@ import {
sql,
} from "kysely";
import { jsonArrayFrom, jsonBuildObject } from "kysely/helpers/sqlite";
import { Config } from "~/config";
import type { DB, Tables } from "~/db/tables";
import { IS_E2E_TEST_RUN } from "./e2e";
@@ -72,8 +73,7 @@ export function commonUserJsonObject(eb: ExpressionBuilder<Tables, "User">) {
}
const USER_SUBMITTED_IMAGE_ROOT =
(process.env.NODE_ENV === "development" &&
import.meta.env.VITE_PROD_MODE !== "true") ||
(process.env.NODE_ENV === "development" && !Config.prodMode) ||
IS_E2E_TEST_RUN ||
process.env.NODE_ENV === "test"
? "http://127.0.0.1:9000/sendou"
@@ -127,7 +127,7 @@ export function tournamentLogoWithDefault(
"UnvalidatedUserSubmittedImage.id",
)
.$asScalar(),
sql.lit(`${import.meta.env.VITE_TOURNAMENT_DEFAULT_LOGO}`),
sql.lit(Config.tournamentDefaultLogo),
),
);
}

View File

@@ -7,6 +7,7 @@ import type { Params, UIMatch } from "react-router";
import { data, redirect } from "react-router";
import type { z } from "zod";
import type { navItems } from "~/components/layout/nav-items";
import { ServerConfig } from "~/config.server";
import { uploadStreamToS3 } from "~/features/img-upload/s3.server";
import invariant from "./invariant";
import { logger } from "./logger";
@@ -224,8 +225,7 @@ const LOHI_TOKEN_HEADER_NAME = "Lohi-Token";
/** Some endpoints can only be accessed with an auth token. Used by Lohi bot and cron jobs. */
export function canAccessLohiEndpoint(request: Request) {
invariant(process.env.LOHI_TOKEN, "LOHI_TOKEN is required");
return request.headers.get(LOHI_TOKEN_HEADER_NAME) === process.env.LOHI_TOKEN;
return request.headers.get(LOHI_TOKEN_HEADER_NAME) === ServerConfig.lohiToken;
}
function errorToastRedirect(message: string) {