fix conditional loadingof settings

This commit is contained in:
Alex Holliday committed 2024-09-27 13:10:29 +08:00
1 parent c3edbb779a
commit f4bbbc5fa9
1 file changed
+25 -30
+25 -30
View File
@@ -1,34 +1,23 @@
const { env } = require("process");
const AppSettings = require("../models/AppSettings"); const AppSettings = require("../models/AppSettings");
const SERVICE_NAME = "SettingsService"; const SERVICE_NAME = "SettingsService";
const envConfig = {
CLIENT_HOST = process.env.CLIENT_HOST; clientHost: process.env.CLIENT_HOST,
JWT_SECRET = process.env.JWT_SECRET; jwtSecret: process.env.JWT_SECRET,
DB_TYPE = process.env.DB_TYPE; dbType: process.env.DB_TYPE,
DB_CONNECTION_STRING = process.env.DB_CONNECTION_STRING; dbConnectionString: process.env.DB_CONNECTION_STRING,
REDIS_HOST = process.env.REDIS_HOST; redisHost: process.env.REDIS_HOST,
REDIS_PORT = process.env.REDIS_PORT; redisPort: process.env.REDIS_PORT,
TOKEN_TTL = process.env.TOKEN_TTL; tokenTTL: process.env.TOKEN_TTL,
PAGESPEED_API_KEY = process.env.PAGESPEED_API_KEY; pagespeedApiKey: process.env.PAGESPEED_API_KEY,
SYSTEM_EMAIL_HOST = process.env.SYSTEM_EMAIL_HOST; systemEmailHost: process.env.SYSTEM_EMAIL_HOST,
SYSTEM_EMAIL_PORT = process.env.SYSTEM_EMAIL_PORT; systemEmailPort: process.env.SYSTEM_EMAIL_PORT,
SYSTEM_EMAIL_ADDRESS = process.env.SYSTEM_EMAIL_ADDRESS; systemEmailAddress: process.env.SYSTEM_EMAIL_ADDRESS,
SYSTEM_EMAIL_PASSWORD = process.env.SYSTEM_EMAIL_PASSWORD; systemEmailPassword: process.env.SYSTEM_EMAIL_PASSWORD,
};
class SettingsService { class SettingsService {
constructor() { constructor() {
this.settings = { this.settings = envConfig;
clientHost: CLIENT_HOST,
jwtSecret: JWT_SECRET,
dbType: DB_TYPE,
dbConnectionString: DB_CONNECTION_STRING,
redisHost: REDIS_HOST,
redisPort: REDIS_PORT,
tokenTTL: TOKEN_TTL,
pagespeedApiKey: PAGESPEED_API_KEY,
systemEmailHost: SYSTEM_EMAIL_HOST,
systemEmailPort: SYSTEM_EMAIL_PORT,
systemEmailAddress: SYSTEM_EMAIL_ADDRESS,
systemEmailPassword: SYSTEM_EMAIL_PASSWORD,
};
} }
async loadSettings() { async loadSettings() {
@@ -37,8 +26,8 @@ class SettingsService {
if (!this.settings) { if (!this.settings) {
throw new Error("Settings not found"); throw new Error("Settings not found");
} }
for (const key in this.settings) { for (const key in envConfig) {
if (this.settings[key] === undefined && dbSettings[key] !== undefined) { if (envConfig[key] === undefined && dbSettings[key] !== undefined) {
this.settings[key] = dbSettings[key]; this.settings[key] = dbSettings[key];
} }
} }
@@ -52,7 +41,13 @@ class SettingsService {
async reloadSettings() { async reloadSettings() {
try { try {
this.settings = await AppSettings.findOne(); const dbSettings = await AppSettings.findOne();
for (const key in envConfig) {
if (envConfig[key] === undefined && dbSettings[key] !== undefined) {
this.settings[key] = dbSettings[key];
}
}
if (!this.settings) { if (!this.settings) {
throw new Error("Settings not found"); throw new Error("Settings not found");
} }