Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some domains can be optional - rename cdn to local_cdn to be more clear #117

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/config-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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(','),
}
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down Expand Up @@ -252,7 +254,6 @@ if (!config.datastore.signature_secret) {
}
}


if (!configValid) {
LOG_ERROR('Config is invalid. Exiting');
process.exit(0);
Expand Down
4 changes: 2 additions & 2 deletions src/services/local-cdn/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
4 changes: 3 additions & 1 deletion src/types/common/config.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down