From 88441821fe2fa782d6a77d9eeb516c098e48587e Mon Sep 17 00:00:00 2001 From: nocode Date: Sun, 13 Sep 2026 12:19:08 +0200 Subject: [PATCH] feat(config): load and expose environment settings Refs: #15 Co-Authored-By: Claude Opus 5 (1M context) --- src/config/env.js | 86 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/config/env.js diff --git a/src/config/env.js b/src/config/env.js new file mode 100644 index 0000000..f1ae751 --- /dev/null +++ b/src/config/env.js @@ -0,0 +1,86 @@ +import { existsSync } from 'node:fs'; + +if (existsSync('.env')) { + process.loadEnvFile('.env'); +} + +const read = (key, fallback = '') => process.env[key]?.trim() || fallback; +const readInt = (key, fallback) => Number.parseInt(read(key), 10) || fallback; +const readBool = (key, fallback = false) => { + const value = read(key); + return value === '' ? fallback : value === 'true'; +}; + +export const env = Object.freeze({ + nodeEnv: read('NODE_ENV', 'development'), + logLevel: read('LOG_LEVEL', 'info'), + logDir: read('LOG_DIR', './logs'), + logRetentionDays: readInt('LOG_RETENTION_DAYS', 30), + + discord: { + token: read('DISCORD_TOKEN'), + clientId: read('DISCORD_CLIENT_ID'), + guildId: read('DISCORD_GUILD_ID'), + }, + + roles: { + owner: read('ROLE_OWNER_ID'), + dev: read('ROLE_DEV_ID'), + staff: read('ROLE_STAFF_ID'), + member: read('ROLE_MEMBER_ID'), + }, + + channels: { + welcome: read('CHANNEL_WELCOME_ID'), + rules: read('CHANNEL_RULES_ID'), + announcements: read('CHANNEL_ANNOUNCEMENTS_ID'), + logs: read('CHANNEL_LOGS_ID'), + modLogs: read('CHANNEL_MOD_LOGS_ID'), + ticketLogs: read('CHANNEL_TICKETS_LOGS_ID'), + notifIssues: read('CHANNEL_NOTIF_ISSUES_ID'), + notifPullRequests: read('CHANNEL_NOTIF_PULL_REQUESTS_ID'), + notifCommits: read('CHANNEL_NOTIF_COMMITS_ID'), + notifReleases: read('CHANNEL_NOTIF_RELEASES_ID'), + notifErrors: read('CHANNEL_NOTIF_ERRORS_ID'), + ticketCategory: read('CATEGORY_TICKETS_ID'), + }, + + gitea: { + url: read('GITEA_URL').replace(/\/+$/, ''), + token: read('GITEA_TOKEN'), + owner: read('GITEA_OWNER'), + repo: read('GITEA_REPO'), + timeoutMs: readInt('GITEA_TIMEOUT_MS', 10_000), + maxRetries: readInt('GITEA_MAX_RETRIES', 2), + }, + + webhook: { + enabled: readBool('WEBHOOK_ENABLED'), + host: read('WEBHOOK_HOST', '0.0.0.0'), + port: readInt('WEBHOOK_PORT', 3000), + path: read('WEBHOOK_PATH', '/webhooks/gitea'), + secret: read('GITEA_WEBHOOK_SECRET'), + }, + + database: { + path: read('DATABASE_PATH', './data/fws-bot.sqlite'), + backupDir: read('BACKUP_DIR', './data/backups'), + }, + + links: { + website: read('FWS_WEBSITE_URL'), + docs: read('FWS_DOCS_URL'), + discordInvite: read('FWS_DISCORD_INVITE_URL'), + archRepo: read('FWS_ARCH_REPO_URL'), + }, +}); + +/** Values that must never appear in Discord messages or logs. */ +export const secrets = [env.discord.token, env.gitea.token, env.webhook.secret].filter(Boolean); + +export function assertRequiredEnv(keys) { + const missing = keys.filter((key) => !read(key)); + if (missing.length > 0) { + throw new Error(`Missing required environment variables: ${missing.join(', ')}`); + } +}