Skip to content

Commit 91c2f7f

Browse files
committed
Remove DB settings from settings service
1 parent c466edb commit 91c2f7f

File tree

6 files changed

+23
-85
lines changed

6 files changed

+23
-85
lines changed

server/controllers/settingsController.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class SettingsController {
3232
}
3333

3434
try {
35+
console.log(req.body);
3536
await this.db.updateAppSettings(req.body);
3637
const updatedSettings = { ...(await this.settingsService.reloadSettings()) };
3738
delete updatedSettings.jwtSecret;

server/db/models/AppSettings.js

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,25 @@ import mongoose from "mongoose";
22

33
const AppSettingsSchema = mongoose.Schema(
44
{
5-
apiBaseUrl: {
5+
language: {
66
type: String,
7-
required: true,
8-
default: "http://localhost:5000/api/v1",
9-
},
10-
logLevel: {
11-
type: String,
12-
default: "debug",
13-
enum: ["debug", "none", "error", "warn"],
14-
},
15-
clientHost: {
16-
type: String,
17-
required: true,
18-
default: "http://localhost:5173",
19-
},
20-
jwtSecret: {
21-
type: String,
22-
required: true,
23-
default: "my_secret",
24-
},
25-
dbType: {
26-
type: String,
27-
required: true,
28-
default: "MongoDB",
29-
},
30-
dbConnectionString: {
31-
type: String,
32-
required: true,
33-
default: "mongodb://localhost:27017/uptime_db",
34-
},
35-
redisUrl: {
36-
type: String,
37-
default: "redis://127.0.0.1:6379",
38-
},
39-
jwtTTL: {
40-
type: String,
41-
required: true,
42-
default: "2h",
7+
default: "gb",
438
},
449
pagespeedApiKey: {
4510
type: String,
4611
default: "",
4712
},
4813
systemEmailHost: {
4914
type: String,
50-
default: "smtp.gmail.com",
5115
},
5216
systemEmailPort: {
5317
type: Number,
54-
default: 465,
5518
},
5619
systemEmailAddress: {
5720
type: String,
58-
default: "",
5921
},
6022
systemEmailPassword: {
6123
type: String,
62-
default: "",
6324
},
6425
singleton: {
6526
type: Boolean,
@@ -73,4 +34,4 @@ const AppSettingsSchema = mongoose.Schema(
7334
}
7435
);
7536

76-
export default mongoose.model("AppSettings", AppSettingsSchema);
37+
export default mongoose.model("AppSettings", AppSettingsSchema);

server/db/mongo/MongoDB.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ import * as diagnosticModule from "./modules/diagnosticModule.js";
7676
class MongoDB {
7777
static SERVICE_NAME = "MongoDB";
7878

79-
constructor() {
79+
constructor({ appSettings }) {
80+
this.appSettings = appSettings;
8081
Object.assign(this, userModule);
8182
Object.assign(this, inviteModule);
8283
Object.assign(this, recoveryModule);
@@ -95,8 +96,7 @@ class MongoDB {
9596
connect = async () => {
9697
try {
9798
const connectionString =
98-
process.env.DB_CONNECTION_STRING || "mongodb://localhost:27017/uptime_db";
99-
console.log("Connecting to MongoDB with connection string:", connectionString);
99+
this.appSettings.dbConnectionString || "mongodb://localhost:27017/uptime_db";
100100
await mongoose.connect(connectionString);
101101
// If there are no AppSettings, create one
102102
await AppSettings.findOneAndUpdate(

server/db/mongo/modules/settingsModule.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ const getAppSettings = async () => {
1414

1515
const updateAppSettings = async (newSettings) => {
1616
try {
17+
console.log(newSettings);
1718
const settings = await AppSettings.findOneAndUpdate(
1819
{},
1920
{ $set: newSettings },
20-
{ new: true }
21+
{ new: true, upsert: true }
2122
);
2223
return settings;
2324
} catch (error) {

server/index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,21 @@ const shutdown = async () => {
139139
// Need to wrap server setup in a function to handle async nature of JobQueue
140140
const startApp = async () => {
141141
const app = express();
142-
const allowedOrigin = process.env.CLIENT_HOST;
143142
// Create and Register Primary services
144143
const translationService = new TranslationService(logger);
145144
const stringService = new StringService(translationService);
146145
ServiceRegistry.register(StringService.SERVICE_NAME, stringService);
147146

147+
// Create services
148+
const settingsService = new SettingsService(AppSettings);
149+
const appSettings = settingsService.loadSettings();
150+
148151
// Create DB
149-
const db = new MongoDB();
152+
const db = new MongoDB({ appSettings });
150153
await db.connect();
151154

152-
// Create services
153-
const settingsService = new SettingsService(AppSettings);
154-
await settingsService.loadSettings();
155+
// Set allowed origin
156+
const allowedOrigin = appSettings.clientHost;
155157

156158
const networkService = new NetworkService(
157159
axios,
@@ -237,7 +239,7 @@ const startApp = async () => {
237239
ServiceRegistry.get(SettingsService.SERVICE_NAME),
238240
ServiceRegistry.get(JobQueue.SERVICE_NAME),
239241
ServiceRegistry.get(StringService.SERVICE_NAME),
240-
ServiceRegistry.get(EmailService.SERVICE_NAME),
242+
ServiceRegistry.get(EmailService.SERVICE_NAME)
241243
);
242244

243245
const settingsController = new SettingsController(

server/service/settingsService.js

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
const SERVICE_NAME = "SettingsService";
2-
import dotenv from "dotenv";
3-
dotenv.config();
42
const envConfig = {
53
logLevel: process.env.LOG_LEVEL,
64
clientHost: process.env.CLIENT_HOST,
@@ -20,8 +18,6 @@ const envConfig = {
2018
* SettingsService
2119
*
2220
* This service is responsible for loading and managing the application settings.
23-
* It gives priority to environment variables and will only load settings
24-
* from the database if they are not set in the environment.
2521
*/
2622
class SettingsService {
2723
static SERVICE_NAME = SERVICE_NAME;
@@ -34,40 +30,17 @@ class SettingsService {
3430
this.settings = { ...envConfig };
3531
}
3632
/**
37-
* Load settings from the database and merge with environment settings.
38-
* If there are any settings that weren't set by environment variables, use user settings from the database.
39-
* @returns {Promise<Object>} The merged settings.
40-
* @throws Will throw an error if settings are not found in the database or if settings have not been loaded.
41-
*/ async loadSettings() {
42-
try {
43-
const dbSettings = await this.appSettings.findOne();
44-
if (!this.settings) {
45-
throw new Error("Settings not found");
46-
}
47-
48-
// If there are any settings that weren't set by environment variables, use user settings from DB
49-
for (const key in envConfig) {
50-
if (
51-
typeof envConfig?.[key] === "undefined" &&
52-
typeof dbSettings?.[key] !== "undefined"
53-
) {
54-
this.settings[key] = dbSettings[key];
55-
}
56-
}
57-
58-
await this.appSettings.updateOne({}, { $set: this.settings }, { upsert: true });
59-
return this.settings;
60-
} catch (error) {
61-
error.service === undefined ? (error.service = SERVICE_NAME) : null;
62-
error.method === undefined ? (error.method = "loadSettings") : null;
63-
throw error;
64-
}
33+
* Load settings from env settings
34+
* @returns {Object>} The settings.
35+
*/
36+
loadSettings() {
37+
return this.settings;
6538
}
6639
/**
6740
* Reload settings by calling loadSettings.
6841
* @returns {Promise<Object>} The reloaded settings.
6942
*/
70-
async reloadSettings() {
43+
reloadSettings() {
7144
return this.loadSettings();
7245
}
7346
/**

0 commit comments

Comments
 (0)