From 0861595d39c158da3606032d25b3f57d1356e2a3 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Tue, 24 Dec 2024 11:58:56 -0800 Subject: [PATCH] Refactor routers to be class based --- Server/routes/authRoute.js | 74 +++++++++++------ Server/routes/checkRoute.js | 52 ++++++++---- Server/routes/inviteRoute.js | 29 +++++-- Server/routes/maintenanceWindowRoute.js | 46 ++++++----- Server/routes/monitorRoute.js | 104 +++++++++++++++--------- Server/routes/queueRoute.js | 34 ++++---- Server/routes/settingsRoute.js | 25 ++++-- Server/routes/statusPageRoute.js | 31 ++++--- 8 files changed, 255 insertions(+), 140 deletions(-) diff --git a/Server/routes/authRoute.js b/Server/routes/authRoute.js index e19f855db..cebe9a211 100644 --- a/Server/routes/authRoute.js +++ b/Server/routes/authRoute.js @@ -5,34 +5,56 @@ import { isAllowed } from "../middleware/isAllowed.js"; import multer from "multer"; import User from "../db/models/User.js"; -const router = Router(); const upload = multer(); -import { - registerUser, - loginUser, - refreshAuthToken, - editUser, - requestRecovery, - validateRecovery, - resetPassword, - checkSuperadminExists, - getAllUsers, - deleteUser, -} from "../controllers/authController.js"; +class AuthRoutes { + constructor(authController) { + this.router = Router(); + this.authController = authController; + this.initRoutes(); + } -//Auth routes -router.post("/register", upload.single("profileImage"), registerUser); -router.post("/login", loginUser); -router.post("/refresh", refreshAuthToken); -router.put("/user/:userId", upload.single("profileImage"), verifyJWT, editUser); -router.get("/users/superadmin", checkSuperadminExists); -router.get("/users", verifyJWT, isAllowed(["admin", "superadmin"]), getAllUsers); -router.delete("/user/:userId", verifyJWT, verifyOwnership(User, "userId"), deleteUser); + initRoutes() { + this.router.post( + "/register", + upload.single("profileImage"), + this.authController.registerUser + ); + this.router.post("/login", this.authController.loginUser); + this.router.post("/refresh", this.authController.refreshAuthToken); -//Recovery routes -router.post("/recovery/request", requestRecovery); -router.post("/recovery/validate", validateRecovery); -router.post("/recovery/reset/", resetPassword); + this.router.put( + "/user/:userId", + upload.single("profileImage"), + 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; diff --git a/Server/routes/checkRoute.js b/Server/routes/checkRoute.js index a57b7410d..289e0bc2f 100644 --- a/Server/routes/checkRoute.js +++ b/Server/routes/checkRoute.js @@ -1,26 +1,46 @@ import { Router } from "express"; -import { - createCheck, - getChecks, - deleteChecks, - getTeamChecks, - deleteChecksByTeamId, - updateChecksTTL, -} from "../controllers/checkController.js"; import { verifyOwnership } from "../middleware/verifyOwnership.js"; import { isAllowed } from "../middleware/isAllowed.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); -router.post("/:monitorId", verifyOwnership(Monitor, "monitorId"), createCheck); -router.delete("/:monitorId", verifyOwnership(Monitor, "monitorId"), deleteChecks); + initRoutes() { + this.router.get("/:monitorId", this.checkController.getChecks); + 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; diff --git a/Server/routes/inviteRoute.js b/Server/routes/inviteRoute.js index 36d30de5a..c66c573f1 100644 --- a/Server/routes/inviteRoute.js +++ b/Server/routes/inviteRoute.js @@ -1,14 +1,27 @@ import { Router } from "express"; import { verifyJWT } from "../middleware/verifyJWT.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); -router.post("/verify", inviteVerifyController); + initRoutes() { + 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; diff --git a/Server/routes/maintenanceWindowRoute.js b/Server/routes/maintenanceWindowRoute.js index e21e4b9ec..2665c2bb8 100644 --- a/Server/routes/maintenanceWindowRoute.js +++ b/Server/routes/maintenanceWindowRoute.js @@ -1,31 +1,37 @@ import { Router } from "express"; -import { - createMaintenanceWindows, - getMaintenanceWindowById, - getMaintenanceWindowsByTeamId, - getMaintenanceWindowsByMonitorId, - deleteMaintenanceWindow, - editMaintenanceWindow, -} from "../controllers/maintenanceWindowController.js"; import { verifyOwnership } from "../middleware/verifyOwnership.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( - "/monitor/:monitorId", - verifyOwnership(Monitor, "monitorId"), - getMaintenanceWindowsByMonitorId -); + this.router.get( + "/team/", + this.maintenanceWindowController.getMaintenanceWindowsByTeamId + ); -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; diff --git a/Server/routes/monitorRoute.js b/Server/routes/monitorRoute.js index 056d81ed6..c3257372b 100644 --- a/Server/routes/monitorRoute.js +++ b/Server/routes/monitorRoute.js @@ -1,51 +1,79 @@ 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 { fetchMonitorCertificate } from "../controllers/controllerUtils.js"; -const router = Router(); +class MonitorRoutes { + constructor(monitorController) { + this.router = Router(); + this.monitorController = monitorController; + this.initRoutes(); + } -router.get("/", getAllMonitors); -router.get("/uptime", getAllMonitorsWithUptimeStats); -router.get("/stats/:monitorId", getMonitorStatsById); -router.get("/certificate/:monitorId", (req, res, next) => { - getMonitorCertificate(req, res, next, fetchMonitorCertificate); -}); -router.get("/:monitorId", getMonitorById); -router.get("/team/summary/:teamId", getMonitorsAndSummaryByTeamId); -router.get("/team/:teamId", getMonitorsByTeamId); + initRoutes() { + this.router.get("/", this.monitorController.getAllMonitors); + this.router.get("/uptime", this.monitorController.getAllMonitorsWithUptimeStats); + this.router.get("/stats/:monitorId", this.monitorController.getMonitorStatsById); + this.router.get("/certificate/:monitorId", (req, res, next) => { + this.monitorController.getMonitorCertificate( + req, + res, + 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( - "/resolution/url", - isAllowed(["admin", "superadmin"]), - checkEndpointResolution -); + this.router.get( + "/resolution/url", + isAllowed(["admin", "superadmin"]), + 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; diff --git a/Server/routes/queueRoute.js b/Server/routes/queueRoute.js index addd9dab8..af0e9d08e 100644 --- a/Server/routes/queueRoute.js +++ b/Server/routes/queueRoute.js @@ -1,21 +1,21 @@ import { Router } from "express"; -import { - getMetrics, - getJobs, - addJob, - obliterateQueue, -} from "../controllers/queueController.js"; -const router = Router(); -router.get("/metrics", getMetrics); +class QueueRoutes { + 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 -router.get("/jobs", getJobs); + getRouter() { + return this.router; + } +} -// Add Job -router.post("/jobs", addJob); - -// Obliterate Queue -router.post("/obliterate", obliterateQueue); - -export default router; +export default QueueRoutes; diff --git a/Server/routes/settingsRoute.js b/Server/routes/settingsRoute.js index 1b8dc19ad..8da262427 100644 --- a/Server/routes/settingsRoute.js +++ b/Server/routes/settingsRoute.js @@ -1,10 +1,25 @@ import { Router } from "express"; -import { getAppSettings, updateAppSettings } from "../controllers/settingsController.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); -router.put("/", isAllowed(["superadmin"]), updateAppSettings); + initRoutes() { + this.router.get("/", this.settingsController.getAppSettings); + this.router.put( + "/", + isAllowed(["superadmin"]), + this.settingsController.updateAppSettings + ); + } -export default router; + getRouter() { + return this.router; + } +} + +export default SettingsRoutes; diff --git a/Server/routes/statusPageRoute.js b/Server/routes/statusPageRoute.js index 6300723f4..ff2a2018a 100644 --- a/Server/routes/statusPageRoute.js +++ b/Server/routes/statusPageRoute.js @@ -1,11 +1,22 @@ -import express from "express"; -import { verifyJWT } from "../middleware/verifyJWT.js"; -import { - createStatusPage, - getStatusPageByUrl, -} from "../controllers/statusPageController.js"; -const router = express.Router(); +import { Router } from "express"; -router.get("/:url", getStatusPageByUrl); -router.post("/:url", verifyJWT, createStatusPage); -export default router; +import { verifyJWT } from "../middleware/verifyJWT.js"; + +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;