diff --git a/src/config-manager.ts b/src/config-manager.ts index cb63c94..fb9d1c7 100644 --- a/src/config-manager.ts +++ b/src/config-manager.ts @@ -3,7 +3,7 @@ import mongoose from 'mongoose'; import dotenv from 'dotenv'; import isValidHostname from 'is-valid-hostname'; import { LOG_INFO, LOG_WARN, LOG_ERROR, formatHostnames } from '@/logger'; -import { type Config, domainServices } from '@/types/common/config'; +import { type Config, domainServices, optionalDomainServices } from '@/types/common/config'; dotenv.config(); @@ -85,7 +85,7 @@ export const config: Config = { cbvc: (process.env.PN_ACT_CONFIG_DOMAINS_CBVC || 'cbvc.cdn.pretendo.cc').split(','), conntest: (process.env.PN_ACT_CONFIG_DOMAINS_CONNTEST || 'conntest.pretendo.cc').split(','), datastore: (process.env.PN_ACT_CONFIG_DOMAINS_DATASTORE || 'datastore.pretendo.cc').split(','), - cdn: (process.env.PN_ACT_CONFIG_DOMAINS_CDN || '').split(','), + local_cdn: (process.env.PN_ACT_CONFIG_DOMAINS_LOCAL_CDN || '').split(','), nasc: (process.env.PN_ACT_CONFIG_DOMAINS_NASC || 'nasc.pretendo.cc').split(','), nnas: (process.env.PN_ACT_CONFIG_DOMAINS_NNAS || 'c.account.pretendo.cc,account.pretendo.cc').split(','), } @@ -99,7 +99,7 @@ if (process.env.PN_ACT_CONFIG_STRIPE_SECRET_KEY) { // * Add the old config option for backwards compatibility if (config.cdn.subdomain) { - config.domains.cdn.push(config.cdn.subdomain); + config.domains.local_cdn.push(config.cdn.subdomain); } let configValid = true; @@ -116,7 +116,7 @@ for (const service of domainServices) { isValidHostname(domain) ? validDomains.push(domain) : invalidDomains.push(domain); } - if (validDomains.length === 0) { + if (validDomains.length === 0 && !optionalDomainServices.includes(service)) { LOG_ERROR(`No valid domains found for ${service}. Set the PN_ACT_CONFIG_DOMAINS_${service.toUpperCase()} environment variable to a valid domain`); configValid = false; } @@ -201,8 +201,8 @@ if (!config.server_environment) { } if (disabledFeatures.s3) { - if (config.domains.cdn.length === 0) { - LOG_ERROR('S3 file storage is disabled and no CDN subdomain was set. Set the PN_ACT_CONFIG_DOMAINS_CDN environment variable'); + if (config.domains.local_cdn.length === 0) { + LOG_ERROR('S3 file storage is disabled and no CDN domain was set. Set the PN_ACT_CONFIG_DOMAINS_LOCAL_CDN environment variable'); configValid = false; } @@ -211,11 +211,13 @@ if (disabledFeatures.s3) { configValid = false; } - LOG_WARN(`S3 file storage disabled. Using disk-based file storage. Please ensure cdn.base_url config or PN_ACT_CONFIG_CDN_BASE env variable is set to point to this server with the subdomain being ${config.cdn.subdomain}`); + if (configValid) { + LOG_WARN(`S3 file storage disabled. Using disk-based file storage. Please ensure cdn.base_url config or PN_ACT_CONFIG_CDN_BASE env variable is set to point to this server with the domain being one of ${formatHostnames(config.domains.local_cdn)}`); - if (disabledFeatures.redis) { - LOG_WARN('Both S3 and Redis are disabled. Large CDN files will use the in-memory cache, which may result in high memory use. Please enable S3 if you\'re running a production server.'); - } + if (disabledFeatures.redis) { + LOG_WARN('Both S3 and Redis are disabled. Large CDN files will use the in-memory cache, which may result in high memory use. Please enable S3 if you\'re running a production server.'); + } + } } if (!config.aes_key) { @@ -252,7 +254,6 @@ if (!config.datastore.signature_secret) { } } - if (!configValid) { LOG_ERROR('Config is invalid. Exiting'); process.exit(0); diff --git a/src/services/local-cdn/index.ts b/src/services/local-cdn/index.ts index 1f1faad..5c837ee 100644 --- a/src/services/local-cdn/index.ts +++ b/src/services/local-cdn/index.ts @@ -18,8 +18,8 @@ if (disabledFeatures.s3) { localcdn.use(get); // * Create domains - LOG_INFO(`[LOCAL-CDN] Creating cdn router with domains: ${formatHostnames(config.domains.cdn)}`); - router.use(restrictHostnames(config.domains.cdn, localcdn)); + LOG_INFO(`[LOCAL-CDN] Creating cdn router with domains: ${formatHostnames(config.domains.local_cdn)}`); + router.use(restrictHostnames(config.domains.local_cdn, localcdn)); } else { LOG_INFO('[LOCAL-CDN] s3 enabled, skipping local CDN'); } diff --git a/src/types/common/config.ts b/src/types/common/config.ts index 20f5755..5585280 100644 --- a/src/types/common/config.ts +++ b/src/types/common/config.ts @@ -1,8 +1,10 @@ import mongoose from 'mongoose'; -export const domainServices = ['api', 'assets', 'cbvc', 'conntest', 'datastore', 'nasc', 'nnas', 'cdn'] as const; +export const domainServices = ['api', 'assets', 'cbvc', 'conntest', 'datastore', 'nasc', 'nnas', 'local_cdn'] as const; export type DomainService = typeof domainServices[number]; +export const optionalDomainServices: DomainService[] = ['local_cdn']; + export interface Config { http: { port: number;