Remove DB settings from settings service

This commit is contained in:
Alex Holliday committed 2025-05-08 13:11:51 -07:00
1 parent c466edb751
commit 91c2f7f0d5
6 files changed
+23 -85

No files matched your search

+1
View File
@@ -32,6 +32,7 @@ class SettingsController {
} }
try { try {
console.log(req.body);
await this.db.updateAppSettings(req.body); await this.db.updateAppSettings(req.body);
const updatedSettings = { ...(await this.settingsService.reloadSettings()) }; const updatedSettings = { ...(await this.settingsService.reloadSettings()) };
delete updatedSettings.jwtSecret; delete updatedSettings.jwtSecret;
+2 -41
View File
@@ -2,44 +2,9 @@ import mongoose from "mongoose";
const AppSettingsSchema = mongoose.Schema( const AppSettingsSchema = mongoose.Schema(
{ {
apiBaseUrl: { language: {
type: String, type: String,
required: true, default: "gb",
default: "http://localhost:5000/api/v1",
},
logLevel: {
type: String,
default: "debug",
enum: ["debug", "none", "error", "warn"],
},
clientHost: {
type: String,
required: true,
default: "http://localhost:5173",
},
jwtSecret: {
type: String,
required: true,
default: "my_secret",
},
dbType: {
type: String,
required: true,
default: "MongoDB",
},
dbConnectionString: {
type: String,
required: true,
default: "mongodb://localhost:27017/uptime_db",
},
redisUrl: {
type: String,
default: "redis://127.0.0.1:6379",
},
jwtTTL: {
type: String,
required: true,
default: "2h",
}, },
pagespeedApiKey: { pagespeedApiKey: {
type: String, type: String,
@@ -47,19 +12,15 @@ const AppSettingsSchema = mongoose.Schema(
}, },
systemEmailHost: { systemEmailHost: {
type: String, type: String,
default: "smtp.gmail.com",
}, },
systemEmailPort: { systemEmailPort: {
type: Number, type: Number,
default: 465,
}, },
systemEmailAddress: { systemEmailAddress: {
type: String, type: String,
default: "",
}, },
systemEmailPassword: { systemEmailPassword: {
type: String, type: String,
default: "",
}, },
singleton: { singleton: {
type: Boolean, type: Boolean,
+3 -3
View File
@@ -76,7 +76,8 @@ import * as diagnosticModule from "./modules/diagnosticModule.js";
class MongoDB { class MongoDB {
static SERVICE_NAME = "MongoDB"; static SERVICE_NAME = "MongoDB";
constructor() { constructor({ appSettings }) {
this.appSettings = appSettings;
Object.assign(this, userModule); Object.assign(this, userModule);
Object.assign(this, inviteModule); Object.assign(this, inviteModule);
Object.assign(this, recoveryModule); Object.assign(this, recoveryModule);
@@ -95,8 +96,7 @@ class MongoDB {
connect = async () => { connect = async () => {
try { try {
const connectionString = const connectionString =
process.env.DB_CONNECTION_STRING || "mongodb://localhost:27017/uptime_db"; this.appSettings.dbConnectionString || "mongodb://localhost:27017/uptime_db";
console.log("Connecting to MongoDB with connection string:", connectionString);
await mongoose.connect(connectionString); await mongoose.connect(connectionString);
// If there are no AppSettings, create one // If there are no AppSettings, create one
await AppSettings.findOneAndUpdate( await AppSettings.findOneAndUpdate(
+2 -1
View File
@@ -14,10 +14,11 @@ const getAppSettings = async () => {
const updateAppSettings = async (newSettings) => { const updateAppSettings = async (newSettings) => {
try { try {
console.log(newSettings);
const settings = await AppSettings.findOneAndUpdate( const settings = await AppSettings.findOneAndUpdate(
{}, {},
{ $set: newSettings }, { $set: newSettings },
{ new: true } { new: true, upsert: true }
); );
return settings; return settings;
} catch (error) { } catch (error) {
+9 -7
View File
@@ -139,19 +139,21 @@ const shutdown = async () => {
// Need to wrap server setup in a function to handle async nature of JobQueue // Need to wrap server setup in a function to handle async nature of JobQueue
const startApp = async () => { const startApp = async () => {
const app = express(); const app = express();
const allowedOrigin = process.env.CLIENT_HOST;
// Create and Register Primary services // Create and Register Primary services
const translationService = new TranslationService(logger); const translationService = new TranslationService(logger);
const stringService = new StringService(translationService); const stringService = new StringService(translationService);
ServiceRegistry.register(StringService.SERVICE_NAME, stringService); ServiceRegistry.register(StringService.SERVICE_NAME, stringService);
// Create DB
const db = new MongoDB();
await db.connect();
// Create services // Create services
const settingsService = new SettingsService(AppSettings); const settingsService = new SettingsService(AppSettings);
await settingsService.loadSettings(); const appSettings = settingsService.loadSettings();
// Create DB
const db = new MongoDB({ appSettings });
await db.connect();
// Set allowed origin
const allowedOrigin = appSettings.clientHost;
const networkService = new NetworkService( const networkService = new NetworkService(
axios, axios,
@@ -237,7 +239,7 @@ const startApp = async () => {
ServiceRegistry.get(SettingsService.SERVICE_NAME), ServiceRegistry.get(SettingsService.SERVICE_NAME),
ServiceRegistry.get(JobQueue.SERVICE_NAME), ServiceRegistry.get(JobQueue.SERVICE_NAME),
ServiceRegistry.get(StringService.SERVICE_NAME), ServiceRegistry.get(StringService.SERVICE_NAME),
ServiceRegistry.get(EmailService.SERVICE_NAME), ServiceRegistry.get(EmailService.SERVICE_NAME)
); );
const settingsController = new SettingsController( const settingsController = new SettingsController(
+6 -33
View File
@@ -1,6 +1,4 @@
const SERVICE_NAME = "SettingsService"; const SERVICE_NAME = "SettingsService";
import dotenv from "dotenv";
dotenv.config();
const envConfig = { const envConfig = {
logLevel: process.env.LOG_LEVEL, logLevel: process.env.LOG_LEVEL,
clientHost: process.env.CLIENT_HOST, clientHost: process.env.CLIENT_HOST,
@@ -20,8 +18,6 @@ const envConfig = {
* SettingsService * SettingsService
* *
* This service is responsible for loading and managing the application settings. * This service is responsible for loading and managing the application settings.
* It gives priority to environment variables and will only load settings
* from the database if they are not set in the environment.
*/ */
class SettingsService { class SettingsService {
static SERVICE_NAME = SERVICE_NAME; static SERVICE_NAME = SERVICE_NAME;
@@ -34,40 +30,17 @@ class SettingsService {
this.settings = { ...envConfig }; this.settings = { ...envConfig };
} }
/** /**
* Load settings from the database and merge with environment settings. * Load settings from env settings
* If there are any settings that weren't set by environment variables, use user settings from the database. * @returns {Object>} The settings.
* @returns {Promise<Object>} The merged settings. */
* @throws Will throw an error if settings are not found in the database or if settings have not been loaded. loadSettings() {
*/ async loadSettings() { return this.settings;
try {
const dbSettings = await this.appSettings.findOne();
if (!this.settings) {
throw new Error("Settings not found");
}
// If there are any settings that weren't set by environment variables, use user settings from DB
for (const key in envConfig) {
if (
typeof envConfig?.[key] === "undefined" &&
typeof dbSettings?.[key] !== "undefined"
) {
this.settings[key] = dbSettings[key];
}
}
await this.appSettings.updateOne({}, { $set: this.settings }, { upsert: true });
return this.settings;
} catch (error) {
error.service === undefined ? (error.service = SERVICE_NAME) : null;
error.method === undefined ? (error.method = "loadSettings") : null;
throw error;
}
} }
/** /**
* Reload settings by calling loadSettings. * Reload settings by calling loadSettings.
* @returns {Promise<Object>} The reloaded settings. * @returns {Promise<Object>} The reloaded settings.
*/ */
async reloadSettings() { reloadSettings() {
return this.loadSettings(); return this.loadSettings();
} }
/** /**