Load env files in notification worker

This commit is contained in:
zv
2026-06-12 21:26:52 +02:00
parent ea1dd4b5cb
commit 310da3a87f
3 changed files with 25 additions and 3 deletions

View File

@@ -1,8 +1,14 @@
import { existsSync, readFileSync } from "node:fs";
const DEFAULT_APP_URL = "http://127.0.0.1:3000";
const DEFAULT_WARNING_INTERVAL_MINUTES = 5;
const DEFAULT_MORNING_BRIEF_TIME = "07:00";
const LOOP_INTERVAL_MS = 30_000;
const initialEnvKeys = new Set(Object.keys(process.env));
loadEnvFile(".env", false);
loadEnvFile(".env.local", true);
const appUrl = normalizeAppUrl(process.env.WTR_APP_URL ?? process.env.NEXT_PUBLIC_APP_URL ?? DEFAULT_APP_URL);
const cronSecret = process.env.NOTIFICATIONS_CRON_SECRET ?? "";
const warningIntervalMinutes = readPositiveNumber(process.env.NOTIFICATIONS_WARNING_INTERVAL_MINUTES, DEFAULT_WARNING_INTERVAL_MINUTES);
@@ -12,6 +18,22 @@ let lastWarningCheckAt = 0;
let lastMorningBriefDate = "";
let isRunningTick = false;
function loadEnvFile(path, overrideLoadedValues) {
if (!existsSync(path)) return;
const lines = readFileSync(path, "utf8").split(/\r?\n/);
for (const line of lines) {
const trimmedLine = line.trim();
if (!trimmedLine || trimmedLine.startsWith("#")) continue;
const separatorIndex = trimmedLine.indexOf("=");
if (separatorIndex === -1) continue;
const key = trimmedLine.slice(0, separatorIndex).trim();
const rawValue = trimmedLine.slice(separatorIndex + 1).trim();
if (!key || initialEnvKeys.has(key)) continue;
if (!overrideLoadedValues && process.env[key] !== undefined) continue;
process.env[key] = rawValue.replace(/^['"]|['"]$/g, "");
}
}
function normalizeAppUrl(value) {
return value.replace(/\/+$/, "");
}