Files
FWS-DiscordBot/src/config/env.js
T
nocodeandClaude Opus 5 a1e1de7734 feat(web): publish a public status page at /status
Checking whether the service is up required an account, so the answer
lived where the people asking could not reach it.

The page is rendered on the server with no script at all and refreshes
through a meta tag: a status page that needs a working browser is
useless at the moment it is read. Two rules follow from it being public.
A repository the forge does not list as public keeps its environment
visible but loses its name, and the banner always states the worst true
thing — a failed deployment forbids the green headline even while the
bot answers. WEB_PUBLIC_STATUS=false removes page and route together.

/api/status gives the same in JSON, which is what a monitor should watch
rather than /api/health — the latter only says the process is alive.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-24 18:46:26 +02:00

131 lines
4.3 KiB
JavaScript

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';
};
/** Durations accept 0, which `readInt` cannot express because 0 is falsy. */
const readDuration = (key, fallback) => {
const value = read(key);
if (value === '') return fallback;
const parsed = Number.parseInt(value, 10);
return Number.isFinite(parsed) && parsed >= 0 ? parsed : fallback;
};
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'),
voiceTrigger: read('CHANNEL_VOICE_TRIGGER_ID'),
voiceCategory: read('CATEGORY_VOICE_ID'),
},
voice: {
emptyGraceMs: readDuration('VOICE_EMPTY_GRACE_MS', 5_000),
},
bot: {
statusIntervalSeconds: readDuration('BOT_STATUS_INTERVAL_SECONDS', 60),
},
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'),
},
web: {
enabled: readBool('WEB_ENABLED'),
host: read('WEB_HOST', '127.0.0.1'),
port: readInt('WEB_PORT', 3080),
publicUrl: read('WEB_PUBLIC_URL', '').replace(/\/+$/, ''),
sessionSecret: read('WEB_SESSION_SECRET'),
sessionTtlHours: readInt('WEB_SESSION_TTL_HOURS', 12),
oauthClientSecret: read('DISCORD_CLIENT_SECRET'),
setupToken: read('WEB_SETUP_TOKEN'),
// Running shell commands from a browser is the most dangerous capability of the
// dashboard, so it stays off until it is explicitly enabled (M16-T06).
allowCommandDeploys: readBool('WEB_ALLOW_COMMAND_DEPLOYS'),
allowRestart: readBool('WEB_ALLOW_RESTART'),
// The public status page is opt-out rather than opt-in: a bot people rely on
// owes them a place to check before asking.
publicStatus: readBool('WEB_PUBLIC_STATUS', true),
deployTimeoutMs: readInt('WEB_DEPLOY_TIMEOUT_MS', 900_000),
rateLimitPerMinute: readInt('WEB_RATE_LIMIT_PER_MINUTE', 240),
},
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,
env.web.sessionSecret,
env.web.oauthClientSecret,
env.web.setupToken,
].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(', ')}`);
}
}