Refactor routers to be class based

This commit is contained in:
Alex Holliday committed 2024-12-24 11:58:56 -08:00
1 parent cc40bd6006
commit 0861595d39
8 files changed
+255 -140

No files matched your search

+48 -26
View File
@@ -5,34 +5,56 @@ import { isAllowed } from "../middleware/isAllowed.js";
import multer from "multer"; import multer from "multer";
import User from "../db/models/User.js"; import User from "../db/models/User.js";
const router = Router();
const upload = multer(); const upload = multer();
import { class AuthRoutes {
registerUser, constructor(authController) {
loginUser, this.router = Router();
refreshAuthToken, this.authController = authController;
editUser, this.initRoutes();
requestRecovery, }
validateRecovery,
resetPassword,
checkSuperadminExists,
getAllUsers,
deleteUser,
} from "../controllers/authController.js";
//Auth routes initRoutes() {
router.post("/register", upload.single("profileImage"), registerUser); this.router.post(
router.post("/login", loginUser); "/register",
router.post("/refresh", refreshAuthToken); upload.single("profileImage"),
router.put("/user/:userId", upload.single("profileImage"), verifyJWT, editUser); this.authController.registerUser
router.get("/users/superadmin", checkSuperadminExists); );
router.get("/users", verifyJWT, isAllowed(["admin", "superadmin"]), getAllUsers); this.router.post("/login", this.authController.loginUser);
router.delete("/user/:userId", verifyJWT, verifyOwnership(User, "userId"), deleteUser); this.router.post("/refresh", this.authController.refreshAuthToken);
//Recovery routes this.router.put(
router.post("/recovery/request", requestRecovery); "/user/:userId",
router.post("/recovery/validate", validateRecovery); upload.single("profileImage"),
router.post("/recovery/reset/", resetPassword); verifyJWT,
this.authController.editUser
);
export default router; this.router.get("/users/superadmin", this.authController.checkSuperadminExists);
this.router.get(
"/users",
verifyJWT,
isAllowed(["admin", "superadmin"]),
this.authController.getAllUsers
);
this.router.delete(
"/user/:userId",
verifyJWT,
verifyOwnership(User, "userId"),
this.authController.deleteUser
);
// Recovery routes
this.router.post("/recovery/request", this.authController.requestRecovery);
this.router.post("/recovery/validate", this.authController.validateRecovery);
this.router.post("/recovery/reset/", this.authController.resetPassword);
}
getRouter() {
return this.router;
}
}
export default AuthRoutes;
+36 -16
View File
@@ -1,26 +1,46 @@
import { Router } from "express"; import { Router } from "express";
import {
createCheck,
getChecks,
deleteChecks,
getTeamChecks,
deleteChecksByTeamId,
updateChecksTTL,
} from "../controllers/checkController.js";
import { verifyOwnership } from "../middleware/verifyOwnership.js"; import { verifyOwnership } from "../middleware/verifyOwnership.js";
import { isAllowed } from "../middleware/isAllowed.js"; import { isAllowed } from "../middleware/isAllowed.js";
import Monitor from "../db/models/Monitor.js"; import Monitor from "../db/models/Monitor.js";
const router = Router(); class CheckRoutes {
constructor(checkController) {
this.router = Router();
this.checkController = checkController;
this.initRoutes();
}
router.get("/:monitorId", getChecks); initRoutes() {
router.post("/:monitorId", verifyOwnership(Monitor, "monitorId"), createCheck); this.router.get("/:monitorId", this.checkController.getChecks);
router.delete("/:monitorId", verifyOwnership(Monitor, "monitorId"), deleteChecks); this.router.post(
"/:monitorId",
verifyOwnership(Monitor, "monitorId"),
this.checkController.createCheck
);
this.router.delete(
"/:monitorId",
verifyOwnership(Monitor, "monitorId"),
this.checkController.deleteChecks
);
router.get("/team/:teamId", getTeamChecks); this.router.get("/team/:teamId", this.checkController.getTeamChecks);
router.delete("/team/:teamId", isAllowed(["admin", "superadmin"]), deleteChecksByTeamId); this.router.delete(
"/team/:teamId",
isAllowed(["admin", "superadmin"]),
this.checkController.deleteChecksByTeamId
);
router.put("/team/ttl", isAllowed(["admin", "superadmin"]), updateChecksTTL); this.router.put(
"/team/ttl",
isAllowed(["admin", "superadmin"]),
this.checkController.updateChecksTTL
);
}
export default router; getRouter() {
return this.router;
}
}
export default CheckRoutes;
+21 -8
View File
@@ -1,14 +1,27 @@
import { Router } from "express"; import { Router } from "express";
import { verifyJWT } from "../middleware/verifyJWT.js"; import { verifyJWT } from "../middleware/verifyJWT.js";
import { isAllowed } from "../middleware/isAllowed.js"; import { isAllowed } from "../middleware/isAllowed.js";
import {
issueInvitation,
inviteVerifyController,
} from "../controllers/inviteController.js";
const router = Router(); class InviteRoutes {
constructor(inviteController) {
this.router = Router();
this.inviteController = inviteController;
this.initRoutes();
}
router.post("/", isAllowed(["admin", "superadmin"]), verifyJWT, issueInvitation); initRoutes() {
router.post("/verify", inviteVerifyController); this.router.post(
"/",
isAllowed(["admin", "superadmin"]),
verifyJWT,
this.inviteController.issueInvitation
);
this.router.post("/verify", this.inviteController.inviteVerifyController);
}
export default router; getRouter() {
return this.router;
}
}
export default InviteRoutes;
+26 -20
View File
@@ -1,31 +1,37 @@
import { Router } from "express"; import { Router } from "express";
import {
createMaintenanceWindows,
getMaintenanceWindowById,
getMaintenanceWindowsByTeamId,
getMaintenanceWindowsByMonitorId,
deleteMaintenanceWindow,
editMaintenanceWindow,
} from "../controllers/maintenanceWindowController.js";
import { verifyOwnership } from "../middleware/verifyOwnership.js"; import { verifyOwnership } from "../middleware/verifyOwnership.js";
import Monitor from "../db/models/Monitor.js"; import Monitor from "../db/models/Monitor.js";
const router = Router(); class MaintenanceWindowRoutes {
constructor(maintenanceWindowController) {
this.router = Router();
this.maintenanceWindowController = maintenanceWindowController;
this.initRoutes();
}
initRoutes() {
this.router.post("/", this.maintenanceWindowController.createMaintenanceWindows);
router.post("/", createMaintenanceWindows); this.router.get(
"/monitor/:monitorId",
verifyOwnership(Monitor, "monitorId"),
this.maintenanceWindowController.getMaintenanceWindowsByMonitorId
);
router.get( this.router.get(
"/monitor/:monitorId", "/team/",
verifyOwnership(Monitor, "monitorId"), this.maintenanceWindowController.getMaintenanceWindowsByTeamId
getMaintenanceWindowsByMonitorId );
);
router.get("/team/", getMaintenanceWindowsByTeamId); this.router.get("/:id", this.maintenanceWindowController.getMaintenanceWindowById);
router.get("/:id", getMaintenanceWindowById); this.router.put("/:id", this.maintenanceWindowController.editMaintenanceWindow);
router.put("/:id", editMaintenanceWindow); this.router.delete("/:id", this.maintenanceWindowController.deleteMaintenanceWindow);
}
router.delete("/:id", deleteMaintenanceWindow); getRouter() {
return this.router;
}
}
export default router; export default MaintenanceWindowRoutes;
+66 -38
View File
@@ -1,51 +1,79 @@
import { Router } from "express"; import { Router } from "express";
import {
getAllMonitors,
getAllMonitorsWithUptimeStats,
getMonitorStatsById,
getMonitorCertificate,
getMonitorById,
getMonitorsAndSummaryByTeamId,
getMonitorsByTeamId,
createMonitor,
checkEndpointResolution,
deleteMonitor,
deleteAllMonitors,
editMonitor,
pauseMonitor,
addDemoMonitors,
} from "../controllers/monitorController.js";
import { isAllowed } from "../middleware/isAllowed.js"; import { isAllowed } from "../middleware/isAllowed.js";
import { fetchMonitorCertificate } from "../controllers/controllerUtils.js"; import { fetchMonitorCertificate } from "../controllers/controllerUtils.js";
const router = Router(); class MonitorRoutes {
constructor(monitorController) {
this.router = Router();
this.monitorController = monitorController;
this.initRoutes();
}
router.get("/", getAllMonitors); initRoutes() {
router.get("/uptime", getAllMonitorsWithUptimeStats); this.router.get("/", this.monitorController.getAllMonitors);
router.get("/stats/:monitorId", getMonitorStatsById); this.router.get("/uptime", this.monitorController.getAllMonitorsWithUptimeStats);
router.get("/certificate/:monitorId", (req, res, next) => { this.router.get("/stats/:monitorId", this.monitorController.getMonitorStatsById);
getMonitorCertificate(req, res, next, fetchMonitorCertificate); this.router.get("/certificate/:monitorId", (req, res, next) => {
}); this.monitorController.getMonitorCertificate(
router.get("/:monitorId", getMonitorById); req,
router.get("/team/summary/:teamId", getMonitorsAndSummaryByTeamId); res,
router.get("/team/:teamId", getMonitorsByTeamId); next,
fetchMonitorCertificate
);
});
this.router.get("/:monitorId", this.monitorController.getMonitorById);
this.router.get(
"/team/summary/:teamId",
this.monitorController.getMonitorsAndSummaryByTeamId
);
this.router.get("/team/:teamId", this.monitorController.getMonitorsByTeamId);
router.get( this.router.get(
"/resolution/url", "/resolution/url",
isAllowed(["admin", "superadmin"]), isAllowed(["admin", "superadmin"]),
checkEndpointResolution this.monitorController.checkEndpointResolution
); );
router.delete("/:monitorId", isAllowed(["admin", "superadmin"]), deleteMonitor); this.router.delete(
"/:monitorId",
isAllowed(["admin", "superadmin"]),
this.monitorController.deleteMonitor
);
router.post("/", isAllowed(["admin", "superadmin"]), createMonitor); this.router.post(
"/",
isAllowed(["admin", "superadmin"]),
this.monitorController.createMonitor
);
router.put("/:monitorId", isAllowed(["admin", "superadmin"]), editMonitor); this.router.put(
"/:monitorId",
isAllowed(["admin", "superadmin"]),
this.monitorController.editMonitor
);
router.delete("/", isAllowed(["superadmin"]), deleteAllMonitors); this.router.delete(
"/",
isAllowed(["superadmin"]),
this.monitorController.deleteAllMonitors
);
router.post("/pause/:monitorId", isAllowed(["admin", "superadmin"]), pauseMonitor); this.router.post(
"/pause/:monitorId",
isAllowed(["admin", "superadmin"]),
this.monitorController.pauseMonitor
);
router.post("/demo", isAllowed(["admin", "superadmin"]), addDemoMonitors); this.router.post(
"/demo",
isAllowed(["admin", "superadmin"]),
this.monitorController.addDemoMonitors
);
}
export default router; getRouter() {
return this.router;
}
}
export default MonitorRoutes;
+17 -17
View File
@@ -1,21 +1,21 @@
import { Router } from "express"; import { Router } from "express";
import {
getMetrics,
getJobs,
addJob,
obliterateQueue,
} from "../controllers/queueController.js";
const router = Router(); class QueueRoutes {
router.get("/metrics", getMetrics); constructor(queueController) {
this.router = Router();
this.queueController = queueController;
this.initRoutes();
}
initRoutes() {
this.router.get("/metrics", this.queueController.getMetrics);
this.router.get("/jobs", this.queueController.getJobs);
this.router.post("/jobs", this.queueController.addJob);
this.router.post("/obliterate", this.queueController.obliterateQueue);
}
// Get Jobs getRouter() {
router.get("/jobs", getJobs); return this.router;
}
}
// Add Job export default QueueRoutes;
router.post("/jobs", addJob);
// Obliterate Queue
router.post("/obliterate", obliterateQueue);
export default router;
+20 -5
View File
@@ -1,10 +1,25 @@
import { Router } from "express"; import { Router } from "express";
import { getAppSettings, updateAppSettings } from "../controllers/settingsController.js";
import { isAllowed } from "../middleware/isAllowed.js"; import { isAllowed } from "../middleware/isAllowed.js";
const router = Router(); class SettingsRoutes {
constructor(settingsController) {
this.router = Router();
this.settingsController = settingsController;
this.initRoutes();
}
router.get("/", getAppSettings); initRoutes() {
router.put("/", isAllowed(["superadmin"]), updateAppSettings); this.router.get("/", this.settingsController.getAppSettings);
this.router.put(
"/",
isAllowed(["superadmin"]),
this.settingsController.updateAppSettings
);
}
export default router; getRouter() {
return this.router;
}
}
export default SettingsRoutes;
+21 -10
View File
@@ -1,11 +1,22 @@
import express from "express"; import { Router } from "express";
import { verifyJWT } from "../middleware/verifyJWT.js";
import {
createStatusPage,
getStatusPageByUrl,
} from "../controllers/statusPageController.js";
const router = express.Router();
router.get("/:url", getStatusPageByUrl); import { verifyJWT } from "../middleware/verifyJWT.js";
router.post("/:url", verifyJWT, createStatusPage);
export default router; class StatusPageRoutes {
constructor(statusPageController) {
this.router = Router();
this.statusPageController = statusPageController;
this.initRoutes();
}
initRoutes() {
this.router.get("/:url", this.statusPageController.getStatusPageByUrl);
this.router.post("/:url", verifyJWT, this.statusPageController.createStatusPage);
}
getRouter() {
return this.router;
}
}
export default StatusPageRoutes;