mirror of
https://github.com/spacebarchat/server.git
synced 2026-04-16 02:35:41 +00:00
Introduce EnvConfig classes
This commit is contained in:
54
src/util/config/EnvConfig.ts
Normal file
54
src/util/config/EnvConfig.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
|
||||
Copyright (C) 2025 Spacebar and Spacebar Contributors
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { LogEnvConfiguration } from "./envTypes/LogEnvConfiguration";
|
||||
import { DatabaseEnvConfiguration } from "./envTypes/DatabaseEnvConfiguration";
|
||||
import os from "os";
|
||||
import { ConfigurationEnvConfiguration } from "./envTypes/ConfigurationEnvConfiguration";
|
||||
|
||||
export class EnvConfig {
|
||||
private static logConfig = new LogEnvConfiguration();
|
||||
private static databaseConfig = new DatabaseEnvConfiguration();
|
||||
private static configurationConfig = new ConfigurationEnvConfiguration();
|
||||
|
||||
static get logging(): LogEnvConfiguration {
|
||||
return this.logConfig;
|
||||
}
|
||||
|
||||
static get database(): DatabaseEnvConfiguration {
|
||||
return this.databaseConfig;
|
||||
}
|
||||
|
||||
static get configuration(): ConfigurationEnvConfiguration {
|
||||
return this.configurationConfig;
|
||||
}
|
||||
|
||||
static get threads(): number {
|
||||
try {
|
||||
return Number(process.env.THREADS) || os.cpus().length;
|
||||
} catch {
|
||||
console.log("[EnvConfig] Failed to get thread count! Using 1...");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Deprecations:
|
||||
if (process.env.GATEWAY) console.warn("[EnvConfig] GATEWAY is deprecated and no longer does anything. Please configure cdn_endpointPublic in configuration instead.");
|
||||
|
||||
if (process.env.CDN) console.warn("[EnvConfig] CDN is deprecated and no longer does anything. Please configure cdn_endpointPublic in configuration instead.");
|
||||
49
src/util/config/envTypes/ConfigurationEnvConfiguration.ts
Normal file
49
src/util/config/envTypes/ConfigurationEnvConfiguration.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
|
||||
Copyright (C) 2025 Spacebar and Spacebar Contributors
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
export class ConfigurationEnvConfiguration {
|
||||
get enabled(): boolean {
|
||||
return process.env.CONFIG_PATH !== undefined;
|
||||
}
|
||||
|
||||
get path(): string {
|
||||
if (process.env.CONFIG_PATH === undefined && this.enabled) {
|
||||
throw new Error("BUGBUG: enabled() determined that JSON config is enabled, but it is not?");
|
||||
}
|
||||
|
||||
return process.env.CONFIG_PATH!;
|
||||
}
|
||||
|
||||
get writebackEnabled(): boolean {
|
||||
return process.env.CONFIG_WRITEBACK === "true" || process.env.CONFIG_WRITEBACK === undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the configuration mode.
|
||||
* - "override": Config file overrides settings at runtime
|
||||
* - "overwrite": Config file overwrites database values at runtime
|
||||
* - "single": Database is not used
|
||||
*/
|
||||
get mode(): ("override" | "overwrite" | "single") {
|
||||
if (process.env.CONFIG_MODE === "override" || process.env.CONFIG_MODE === "overwrite" || process.env.CONFIG_MODE === "single") {
|
||||
return process.env.CONFIG_MODE;
|
||||
}
|
||||
|
||||
return "override";
|
||||
}
|
||||
}
|
||||
45
src/util/config/envTypes/DatabaseEnvConfiguration.ts
Normal file
45
src/util/config/envTypes/DatabaseEnvConfiguration.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
|
||||
Copyright (C) 2025 Spacebar and Spacebar Contributors
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
export class DatabaseEnvConfiguration {
|
||||
get unsafeSchemaSync(): boolean {
|
||||
if (process.env.DB_UNSAFE_SCHEMA_SYNC !== undefined) {
|
||||
return process.env.DB_UNSAFE_SCHEMA_SYNC === "true";
|
||||
}
|
||||
|
||||
if (process.env.DB_SYNC !== undefined) {
|
||||
console.warn("[EnvConfig] DB_SYNC is deprecated. Please use DB_UNSAFE_SCHEMA_SYNC instead.");
|
||||
return process.env.DB_SYNC === "true";
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
get disableJoins(): boolean {
|
||||
if (process.env.DB_DISABLE_JOINS !== undefined) {
|
||||
return process.env.DB_DISABLE_JOINS === "true";
|
||||
}
|
||||
|
||||
if (process.env.DB_NO_JOINS !== undefined) {
|
||||
console.warn("[EnvConfig] DB_NO_JOINS is deprecated. Please use DB_DISABLE_JOINS instead.");
|
||||
return process.env.DB_NO_JOINS === "true";
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
65
src/util/config/envTypes/LogEnvConfiguration.ts
Normal file
65
src/util/config/envTypes/LogEnvConfiguration.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
|
||||
Copyright (C) 2025 Spacebar and Spacebar Contributors
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published
|
||||
by the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
export class LogEnvConfiguration {
|
||||
get logGatewayEvents(): boolean {
|
||||
if (process.env.LOG_GATEWAY_EVENTS === "true") return true;
|
||||
if (process.env.WS_LOGEVENTS !== undefined) {
|
||||
console.warn("[EnvConfig] WS_LOGEVENTS is deprecated. Please use LOG_GATEWAY_EVENTS instead.");
|
||||
return process.env.WS_LOGEVENTS === "true";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
get logDatabaseQueries(): boolean {
|
||||
if (process.env.LOG_DATABASE_QUERIES === "true") return true;
|
||||
if (process.env.DB_LOGGING !== undefined) {
|
||||
console.warn("[EnvConfig] DB_LOGGING is deprecated. Please use LOG_DATABASE_QUERIES instead.");
|
||||
return process.env.DB_LOGGING === "true";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
get logRequests(): string {
|
||||
return process.env.LOG_REQUESTS || "";
|
||||
}
|
||||
|
||||
get logValidationErrors(): boolean {
|
||||
return process.env.LOG_VALIDATION_ERRORS === "true";
|
||||
}
|
||||
|
||||
get logGatewayTraces(): boolean {
|
||||
return process.env.LOG_GATEWAY_TRACES === "true";
|
||||
}
|
||||
|
||||
get logProtoUpdates(): string[] {
|
||||
if (process.env.LOG_PROTO_UPDATES === "true") return ["FRECENCY", "SETTINGS"];
|
||||
return (process.env.LOG_PROTO_UPDATES || "")
|
||||
.split(",")
|
||||
.map((s) => s.trim().toUpperCase())
|
||||
.filter((s) => s.length > 0);
|
||||
}
|
||||
|
||||
get logAuthentication(): boolean {
|
||||
return process.env.LOG_AUTHENTICATION === "true";
|
||||
}
|
||||
|
||||
get logCdnSignatures(): boolean {
|
||||
return process.env.LOG_CDN_SIGNATURES === "true";
|
||||
}
|
||||
}
|
||||
@@ -17,3 +17,4 @@
|
||||
*/
|
||||
|
||||
export * from "./Config";
|
||||
export * from "./EnvConfig";
|
||||
|
||||
Reference in New Issue
Block a user