Add app settings model, routes, db ops, service, use settings for issueToken
This commit is contained in:
1 parent
b2bc441c7f
commit
992bf099fa
12 files changed
+210
-19
No files matched your search
@@ -1,4 +1,4 @@
|
|||||||
const PORT = process.env.PORT || 5000;
|
const PORT = 5000;
|
||||||
|
|
||||||
const connectDbAndRunServer = async (app, db) => {
|
const connectDbAndRunServer = async (app, db) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -25,10 +25,15 @@ const { getTokenFromHeaders } = require("../utils/utils");
|
|||||||
* @param {Object} payload
|
* @param {Object} payload
|
||||||
* @returns {String}
|
* @returns {String}
|
||||||
*/
|
*/
|
||||||
const issueToken = (payload) => {
|
const issueToken = async (payload, appSettings) => {
|
||||||
//TODO Add proper expiration date
|
try {
|
||||||
const tokenTTL = process.env.TOKEN_TTL ? process.env.TOKEN_TTL : "2h";
|
const tokenTTL = appSettings.jwtTTL ? appSettings.jwtTTL : "2h";
|
||||||
return jwt.sign(payload, process.env.JWT_SECRET, { expiresIn: tokenTTL });
|
return jwt.sign(payload, appSettings.jwtSecret, { expiresIn: tokenTTL });
|
||||||
|
} catch (error) {
|
||||||
|
error.service === undefined ? (error.service = SERVICE_NAME) : null;
|
||||||
|
error.method === undefined ? (error.method = "issueToken") : null;
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -67,7 +72,8 @@ const registerController = async (req, res, next) => {
|
|||||||
delete userForToken.profileImage;
|
delete userForToken.profileImage;
|
||||||
delete userForToken.avatarImage;
|
delete userForToken.avatarImage;
|
||||||
|
|
||||||
const token = issueToken(userForToken);
|
const appSettings = await req.settingsService.getSettings();
|
||||||
|
const token = issueToken(userForToken, appSettings);
|
||||||
|
|
||||||
req.emailService
|
req.emailService
|
||||||
.buildAndSendEmail(
|
.buildAndSendEmail(
|
||||||
@@ -130,8 +136,8 @@ const loginController = async (req, res, next) => {
|
|||||||
delete userWithoutPassword.avatarImage;
|
delete userWithoutPassword.avatarImage;
|
||||||
|
|
||||||
// Happy path, return token
|
// Happy path, return token
|
||||||
const token = issueToken(userWithoutPassword);
|
const appSettings = req.settingsService.getSettings();
|
||||||
|
const token = await issueToken(userWithoutPassword, appSettings);
|
||||||
// reset avatar image
|
// reset avatar image
|
||||||
userWithoutPassword.avatarImage = user.avatarImage;
|
userWithoutPassword.avatarImage = user.avatarImage;
|
||||||
|
|
||||||
@@ -425,7 +431,9 @@ const resetPasswordController = async (req, res, next) => {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const user = await req.db.resetPassword(req, res);
|
const user = await req.db.resetPassword(req, res);
|
||||||
const token = issueToken(user._doc);
|
|
||||||
|
const appSettings = await req.settingsService.getSettings();
|
||||||
|
const token = issueToken(user._doc, appSettings);
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: successMessages.AUTH_RESET_PASSWORD,
|
msg: successMessages.AUTH_RESET_PASSWORD,
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ const createCheck = async (req, res, next) => {
|
|||||||
.status(200)
|
.status(200)
|
||||||
.json({ success: true, msg: successMessages.CHECK_CREATE, data: check });
|
.json({ success: true, msg: successMessages.CHECK_CREATE, data: check });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service === undefined ? (error.serivce = SERVICE_NAME) : null;
|
error.service === undefined ? (error.service = SERVICE_NAME) : null;
|
||||||
error.method === undefined ? (error.method = "createCheck") : null;
|
error.method === undefined ? (error.method = "createCheck") : null;
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
@@ -62,7 +62,7 @@ const getChecks = async (req, res, next) => {
|
|||||||
data: { checksCount, checks },
|
data: { checksCount, checks },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service === undefined ? (error.serivce = SERVICE_NAME) : null;
|
error.service === undefined ? (error.service = SERVICE_NAME) : null;
|
||||||
error.method === undefined ? (error.method = "getChecks") : null;
|
error.method === undefined ? (error.method = "getChecks") : null;
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
@@ -88,7 +88,7 @@ const getTeamChecks = async (req, res, next) => {
|
|||||||
data: checkData,
|
data: checkData,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service === undefined ? (error.serivce = SERVICE_NAME) : null;
|
error.service === undefined ? (error.service = SERVICE_NAME) : null;
|
||||||
error.method === undefined ? (error.method = "getTeamChecks") : null;
|
error.method === undefined ? (error.method = "getTeamChecks") : null;
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
@@ -114,7 +114,7 @@ const deleteChecks = async (req, res, next) => {
|
|||||||
data: { deletedCount },
|
data: { deletedCount },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service === undefined ? (error.serivce = SERVICE_NAME) : null;
|
error.service === undefined ? (error.service = SERVICE_NAME) : null;
|
||||||
error.method === undefined ? (error.method = "deleteChecks") : null;
|
error.method === undefined ? (error.method = "deleteChecks") : null;
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
const { successMessages } = require("../utils/messages");
|
||||||
|
const SERVICE_NAME = "SettingsController";
|
||||||
|
|
||||||
|
const getAppSettings = async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const settings = await req.db.getAppSettings();
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: successMessages.GET_APP_SETTINGS,
|
||||||
|
data: settings,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
error.service === undefined ? (error.service = SERVICE_NAME) : null;
|
||||||
|
error.method === undefined ? (error.method = "getAppSettings") : null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
getAppSettings,
|
||||||
|
};
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
const mongoose = require("mongoose");
|
const mongoose = require("mongoose");
|
||||||
const UserModel = require("../../models/User");
|
const UserModel = require("../../models/User");
|
||||||
|
const AppSettings = require("../../models/AppSettings");
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
// DB Connection
|
// DB Connection
|
||||||
@@ -7,7 +8,16 @@ const UserModel = require("../../models/User");
|
|||||||
|
|
||||||
const connect = async () => {
|
const connect = async () => {
|
||||||
try {
|
try {
|
||||||
await mongoose.connect(process.env.DB_CONNECTION_STRING);
|
const connectionString =
|
||||||
|
process.env.DB_CONNECTION_STRING || "mongodb://localhost:27017/uptime_db";
|
||||||
|
await mongoose.connect(connectionString);
|
||||||
|
// If there are no AppSettings, create one
|
||||||
|
let appSettings = await AppSettings.find();
|
||||||
|
if (appSettings.length === 0) {
|
||||||
|
appSettings = new AppSettings({});
|
||||||
|
await appSettings.save();
|
||||||
|
}
|
||||||
|
|
||||||
console.log("Connected to MongoDB");
|
console.log("Connected to MongoDB");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to connect to MongoDB");
|
console.error("Failed to connect to MongoDB");
|
||||||
@@ -115,12 +125,20 @@ const {
|
|||||||
deleteMaintenanceWindowByUserId,
|
deleteMaintenanceWindowByUserId,
|
||||||
} = require("./modules/maintenaceWindowModule");
|
} = require("./modules/maintenaceWindowModule");
|
||||||
|
|
||||||
|
//****************************************
|
||||||
|
// Notifications
|
||||||
|
//****************************************
|
||||||
const {
|
const {
|
||||||
createNotification,
|
createNotification,
|
||||||
getNotificationsByMonitorId,
|
getNotificationsByMonitorId,
|
||||||
deleteNotificationsByMonitorId,
|
deleteNotificationsByMonitorId,
|
||||||
} = require("./modules/notificationModule");
|
} = require("./modules/notificationModule");
|
||||||
|
|
||||||
|
//****************************************
|
||||||
|
// AppSettings
|
||||||
|
//****************************************
|
||||||
|
const { getAppSettings } = require("./modules/settingsModule");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
connect,
|
connect,
|
||||||
insertUser,
|
insertUser,
|
||||||
@@ -168,4 +186,5 @@ module.exports = {
|
|||||||
createNotification,
|
createNotification,
|
||||||
getNotificationsByMonitorId,
|
getNotificationsByMonitorId,
|
||||||
deleteNotificationsByMonitorId,
|
deleteNotificationsByMonitorId,
|
||||||
|
getAppSettings,
|
||||||
};
|
};
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
const AppSettings = require("../../../models/AppSettings");
|
||||||
|
const SERVICE_NAME = "SettingsModule";
|
||||||
|
|
||||||
|
const getAppSettings = async () => {
|
||||||
|
try {
|
||||||
|
const settings = AppSettings.findOne();
|
||||||
|
return settings;
|
||||||
|
} catch (error) {
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
error.method = "getSettings";
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
getAppSettings,
|
||||||
|
};
|
||||||
@@ -35,6 +35,7 @@ const insertUser = async (userData, imageFile) => {
|
|||||||
email: userData.email,
|
email: userData.email,
|
||||||
});
|
});
|
||||||
userData.teamId = team._id;
|
userData.teamId = team._id;
|
||||||
|
userData.checkTTL = 60 * 60 * 24 * 30;
|
||||||
await team.save();
|
await team.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+11
-5
@@ -5,7 +5,6 @@ const swaggerUi = require("swagger-ui-express");
|
|||||||
const express = require("express");
|
const express = require("express");
|
||||||
const helmet = require("helmet");
|
const helmet = require("helmet");
|
||||||
const cors = require("cors");
|
const cors = require("cors");
|
||||||
require("dotenv").config();
|
|
||||||
const logger = require("./utils/logger");
|
const logger = require("./utils/logger");
|
||||||
const { verifyJWT } = require("./middleware/verifyJWT");
|
const { verifyJWT } = require("./middleware/verifyJWT");
|
||||||
const { handleErrors } = require("./middleware/handleErrors");
|
const { handleErrors } = require("./middleware/handleErrors");
|
||||||
@@ -15,6 +14,7 @@ const inviteRouter = require("./routes/inviteRoute");
|
|||||||
const monitorRouter = require("./routes/monitorRoute");
|
const monitorRouter = require("./routes/monitorRoute");
|
||||||
const checkRouter = require("./routes/checkRoute");
|
const checkRouter = require("./routes/checkRoute");
|
||||||
const maintenanceWindowRouter = require("./routes/maintenanceWindowRoute");
|
const maintenanceWindowRouter = require("./routes/maintenanceWindowRoute");
|
||||||
|
const settingsRouter = require("./routes/settingsRoute");
|
||||||
|
|
||||||
const { connectDbAndRunServer } = require("./configs/db");
|
const { connectDbAndRunServer } = require("./configs/db");
|
||||||
const queueRouter = require("./routes/queueRoute");
|
const queueRouter = require("./routes/queueRoute");
|
||||||
@@ -22,6 +22,7 @@ const JobQueue = require("./service/jobQueue");
|
|||||||
const NetworkService = require("./service/networkService");
|
const NetworkService = require("./service/networkService");
|
||||||
const EmailService = require("./service/emailService");
|
const EmailService = require("./service/emailService");
|
||||||
const PageSpeedService = require("./service/pageSpeedService");
|
const PageSpeedService = require("./service/pageSpeedService");
|
||||||
|
const SettingsService = require("./service/settingsService");
|
||||||
const SERVICE_NAME = "Server";
|
const SERVICE_NAME = "Server";
|
||||||
|
|
||||||
let cleaningUp = false;
|
let cleaningUp = false;
|
||||||
@@ -47,9 +48,11 @@ const startApp = async () => {
|
|||||||
FakedDB: () => require("./db/FakeDb"),
|
FakedDB: () => require("./db/FakeDb"),
|
||||||
};
|
};
|
||||||
|
|
||||||
const db = DB_TYPE[process.env.DB_TYPE]
|
// const db = DB_TYPE[process.env.DB_TYPE]
|
||||||
? DB_TYPE[process.env.DB_TYPE]()
|
// ? DB_TYPE[process.env.DB_TYPE]()
|
||||||
: require("./db/FakeDb");
|
// : require("./db/FakeDb");
|
||||||
|
|
||||||
|
const db = DB_TYPE.MongoDB();
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
@@ -60,7 +63,6 @@ const startApp = async () => {
|
|||||||
);
|
);
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use(helmet());
|
app.use(helmet());
|
||||||
|
|
||||||
// **************************
|
// **************************
|
||||||
// Make DB accessible anywhere we have a Request object
|
// Make DB accessible anywhere we have a Request object
|
||||||
// By adding the DB to the request object, we can access it in any route
|
// By adding the DB to the request object, we can access it in any route
|
||||||
@@ -72,6 +74,7 @@ const startApp = async () => {
|
|||||||
req.jobQueue = jobQueue;
|
req.jobQueue = jobQueue;
|
||||||
req.emailService = emailService;
|
req.emailService = emailService;
|
||||||
req.pageSpeedService = pageSpeedService;
|
req.pageSpeedService = pageSpeedService;
|
||||||
|
req.settingsService = settingsService;
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -80,6 +83,7 @@ const startApp = async () => {
|
|||||||
|
|
||||||
//routes
|
//routes
|
||||||
app.use("/api/v1/auth", authRouter);
|
app.use("/api/v1/auth", authRouter);
|
||||||
|
app.use("/api/v1/settings", verifyJWT, settingsRouter);
|
||||||
app.use("/api/v1/invite", inviteRouter);
|
app.use("/api/v1/invite", inviteRouter);
|
||||||
app.use("/api/v1/monitors", verifyJWT, monitorRouter);
|
app.use("/api/v1/monitors", verifyJWT, monitorRouter);
|
||||||
app.use("/api/v1/checks", verifyJWT, checkRouter);
|
app.use("/api/v1/checks", verifyJWT, checkRouter);
|
||||||
@@ -126,6 +130,8 @@ const startApp = async () => {
|
|||||||
const networkService = new NetworkService(db, emailService);
|
const networkService = new NetworkService(db, emailService);
|
||||||
const jobQueue = await JobQueue.createJobQueue(db, networkService);
|
const jobQueue = await JobQueue.createJobQueue(db, networkService);
|
||||||
const pageSpeedService = new PageSpeedService();
|
const pageSpeedService = new PageSpeedService();
|
||||||
|
const settingsService = new SettingsService();
|
||||||
|
settingsService.loadSettings();
|
||||||
|
|
||||||
const cleanup = async () => {
|
const cleanup = async () => {
|
||||||
if (cleaningUp) {
|
if (cleaningUp) {
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
const mongoose = require("mongoose");
|
||||||
|
|
||||||
|
const AppSettingsSchema = mongoose.Schema(
|
||||||
|
{
|
||||||
|
apiBaseUrl: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
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",
|
||||||
|
},
|
||||||
|
redisHost: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
default: "127.0.0.1",
|
||||||
|
},
|
||||||
|
redisPort: {
|
||||||
|
type: Number,
|
||||||
|
default: "6379",
|
||||||
|
},
|
||||||
|
jwtTTL: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
default: "99d",
|
||||||
|
},
|
||||||
|
pagespeedApiKey: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
systemEmailHost: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
systemEmailPort: {
|
||||||
|
type: Number,
|
||||||
|
},
|
||||||
|
systemEmailAddress: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
systemEmailAddress: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
singleton: {
|
||||||
|
type: Boolean,
|
||||||
|
required: true,
|
||||||
|
unique: true,
|
||||||
|
default: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timestamps: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
module.exports = mongoose.model("AppSettings", AppSettingsSchema);
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
const router = require("express").Router();
|
||||||
|
const settingsController = require("../controllers/settingsController");
|
||||||
|
const { isAllowed } = require("../middleware/isAllowed");
|
||||||
|
const Monitor = require("../models/Monitor");
|
||||||
|
|
||||||
|
router.get("/", isAllowed(["superadmin"]), settingsController.getAppSettings);
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
const AppSettings = require("../models/AppSettings");
|
||||||
|
const SERVICE_NAME = "SettingsService";
|
||||||
|
|
||||||
|
class SettingsService {
|
||||||
|
constructor() {
|
||||||
|
this.settings = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
async loadSettings() {
|
||||||
|
if (!this.settings) {
|
||||||
|
try {
|
||||||
|
this.settings = await AppSettings.findOne();
|
||||||
|
if (!this.settings) {
|
||||||
|
throw new Error("Settings not found");
|
||||||
|
}
|
||||||
|
return this.settings;
|
||||||
|
} catch (error) {
|
||||||
|
error.service === undefined ? (error.service = SERVICE_NAME) : null;
|
||||||
|
error.method === undefined ? (error.method = "loadSettings") : null;
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getSettings() {
|
||||||
|
if (!this.settings) {
|
||||||
|
throw new Error("Settings have not been loaded");
|
||||||
|
}
|
||||||
|
return this.settings;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = SettingsService;
|
||||||
@@ -98,6 +98,9 @@ const successMessages = {
|
|||||||
|
|
||||||
//Ping Operations
|
//Ping Operations
|
||||||
PING_SUCCESS: "Success",
|
PING_SUCCESS: "Success",
|
||||||
|
|
||||||
|
// App Settings
|
||||||
|
GET_APP_SETTINGS: "Got app settings successfully",
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
Reference in new issue
Block a user