From ddde3ab04ed92613c19e131f18a57764d61cb954 Mon Sep 17 00:00:00 2001 From: mrjvs Date: Wed, 3 Sep 2025 11:58:44 +0200 Subject: [PATCH] chore: remove unused config fields + make use of type inference + make domains configurable --- src/config-manager.ts | 36 ++++++++---------------- src/database.ts | 3 +- src/types/common/config.ts | 56 -------------------------------------- 3 files changed, 12 insertions(+), 83 deletions(-) delete mode 100644 src/types/common/config.ts diff --git a/src/config-manager.ts b/src/config-manager.ts index 0b8574a..ffa09bc 100644 --- a/src/config-manager.ts +++ b/src/config-manager.ts @@ -3,8 +3,6 @@ import path from 'node:path'; import fs from 'fs-extra'; import dotenv from 'dotenv'; import { LOG_INFO, LOG_WARN, LOG_ERROR } from '@/logger'; -import type mongoose from 'mongoose'; -import type { DisabledFeatures, Config } from '@/types/common/config'; dotenv.config(); @@ -22,20 +20,12 @@ LOG_INFO('Loading config'); const warnings: string[] = []; const errors: string[] = []; -let mongooseConnectOptionsMain: mongoose.ConnectOptions = {}; - -if (process.env.PN_BOSS_CONFIG_MONGOOSE_CONNECT_OPTIONS_PATH?.trim()) { - mongooseConnectOptionsMain = fs.readJSONSync(process.env.PN_BOSS_CONFIG_MONGOOSE_CONNECT_OPTIONS_PATH?.trim()); -} else { - warnings.push('No Mongoose connection options found for main connection. To add connection options, set PN_BOSS_CONFIG_MONGOOSE_CONNECT_OPTIONS_PATH to the path of your options JSON file'); -} - -export const disabledFeatures: DisabledFeatures = { +export const disabledFeatures = { s3: false, spr: false }; -export const config: Config = { +export const config = { http: { port: Number(process.env.PN_BOSS_CONFIG_HTTP_PORT?.trim() || '') }, @@ -66,11 +56,9 @@ export const config: Config = { } }, mongoose: { - connection_string: process.env.PN_BOSS_CONFIG_MONGO_CONNECTION_STRING?.trim() || '', - options: mongooseConnectOptionsMain + connection_string: process.env.PN_BOSS_CONFIG_MONGO_CONNECTION_STRING?.trim() || '' }, cdn: { - download_url: process.env.PN_BOSS_CONFIG_CDN_DOWNLOAD_URL?.trim() || '', s3: { endpoint: process.env.PN_BOSS_CONFIG_S3_ENDPOINT?.trim() || '', region: process.env.PN_BOSS_CONFIG_S3_REGION?.trim() || '', @@ -82,6 +70,14 @@ export const config: Config = { }, spr: { enabled: process.env.PN_BOSS_CONFIG_STREETPASS_RELAY_ENABLED?.trim().toLowerCase() === 'true' + }, + domains: { + npdi: (process.env.PN_BOSS_CONFIG_DOMAINS_NPDI || 'npdi.cdn.pretendo.cc').split(','), + npdl: (process.env.PN_BOSS_CONFIG_DOMAINS_NPDL || 'npdl.cdn.pretendo.cc').split(','), + npfl: (process.env.PN_BOSS_CONFIG_DOMAINS_NPFL || 'npfl.c.app.pretendo.cc').split(','), + nppl: (process.env.PN_BOSS_CONFIG_DOMAINS_NPPL || 'nppl.app.pretendo.cc,nppl.c.app.pretendo.cc').split(','), + npts: (process.env.PN_BOSS_CONFIG_DOMAINS_NPTS || 'npts.app.pretendo.cc').split(','), + spr: (process.env.PN_BOSS_CONFIG_DOMAINS_SPR || 'service.spr.app.pretendo.cc').split(',') } }; @@ -143,16 +139,6 @@ if (!config.mongoose.connection_string) { errors.push('Failed to find MongoDB connection string. Set the PN_BOSS_CONFIG_MONGO_CONNECTION_STRING environment variable'); } -if (!config.cdn.download_url) { - errors.push('Failed to find CDN content download URL. Set the PN_BOSS_CONFIG_CDN_DOWNLOAD_URL environment variable'); -} else { - const parsedURL = new URL(config.cdn.download_url); - - if (!parsedURL.hostname.startsWith('npdi.cdn')) { - errors.push('CDN content download URL *MUST* use the subdomain `npdi.cdn`'); - } -} - if (!config.cdn.s3.endpoint) { warnings.push('Failed to find s3 endpoint config. Disabling feature. To enable feature set the PN_BOSS_CONFIG_S3_ENDPOINT environment variable'); disabledFeatures.s3 = true; diff --git a/src/database.ts b/src/database.ts index f5d11c6..f0cdec8 100644 --- a/src/database.ts +++ b/src/database.ts @@ -10,12 +10,11 @@ import type { HydratedTaskDocument, ITask } from '@/types/mongoose/task'; import type { HydratedFileDocument, IFile } from '@/types/mongoose/file'; const connection_string: string = config.mongoose.connection_string; -const options: mongoose.ConnectOptions = config.mongoose.options; let _connection: mongoose.Connection; export async function connect(): Promise { - await mongoose.connect(connection_string, options); + await mongoose.connect(connection_string); _connection = mongoose.connection; _connection.on('error', console.error.bind(console, 'connection error:')); diff --git a/src/types/common/config.ts b/src/types/common/config.ts deleted file mode 100644 index e677d2a..0000000 --- a/src/types/common/config.ts +++ /dev/null @@ -1,56 +0,0 @@ -import type mongoose from 'mongoose'; - -export interface DisabledFeatures { - s3: boolean; - spr: boolean; -} - -export interface Config { - http: { - port: number; - }; - crypto: { - wup: { - aes_key: string; - hmac_key: string; - }; - ctr: { - aes_key: Buffer; - }; - }; - grpc: { - boss: { - address: string; - port: number; - api_key: string; - }; - account: { - address: string; - port: number; - api_key: string; - }; - friends: { - address: string; - port: number; - api_key: string; - }; - }; - mongoose: { - connection_string: string; - options: mongoose.ConnectOptions; - }; - cdn: { - download_url: string; - s3: { - endpoint: string; - region: string; - bucket: string; - key: string; - secret: string; - }; - disk_path: string; - }; - spr: { - enabled: boolean; - }; -}