Check the monitor URL resolution before adding it

This commit is contained in:
om-3004 committed 2024-10-15 17:20:54 +05:30
1 parent 4a1b290416
commit ebf6a0ceb7
5 files changed
+161 -50

No files matched your search

+39
View File
@@ -20,6 +20,7 @@ const jwt = require("jsonwebtoken");
const { getTokenFromHeaders } = require("../utils/utils");
const logger = require("../utils/logger");
const { handleError, handleValidationError } = require("./controllerUtils");
const dns = require('dns');
/**
* Returns all monitors
@@ -262,6 +263,43 @@ const createMonitor = async (req, res, next) => {
}
};
/**
* Checks if the endpoint can be resolved and adds it to the job queue.
* @async
* @param {Object} req - The Express request object.
* @property {Object} req.query - The query parameters of the request.
* @param {Object} res - The Express response object.
* @param {function} next - The next middleware function.
* @returns {Object} The response object with a success status, a message, and the resolution result.
* @throws {Error} If there is an error during the process, especially if there is a validation error (422).
*/
const checkEndpointResolution = async (req, res, next) => {
try {
let { monitorURL } = req.query;
// Remove the protocol (http:// or https://) if present
monitorURL = monitorURL.replace(/^https?:\/\//, '');
// Remove the trailing slash if it exists
monitorURL = monitorURL.endsWith('/') ? monitorURL.slice(0, -1) : monitorURL;
dns.setServers(['8.8.8.8', '1.1.1.1']);// Google, Cloudfare
dns.resolve(monitorURL, (error) => {
console.log("Inside .resolve");
if (error) {
next(handleError(error, SERVICE_NAME, "checkEndpointResolution"));
return ;
}
return res.status(200).json({
success: true,
msg: `URL resolved successfully`,
});
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "checkEndpointResolution"));
}
}
/**
* Deletes a monitor by its ID and also deletes associated checks, alerts, and notifications.
* @async
@@ -475,6 +513,7 @@ module.exports = {
getMonitorsAndSummaryByTeamId,
getMonitorsByTeamId,
createMonitor,
checkEndpointResolution,
deleteMonitor,
deleteAllMonitors,
editMonitor,