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
+160 -49

No files matched your search

@@ -31,6 +31,30 @@ export const createUptimeMonitor = createAsyncThunk(
} }
); );
export const checkEndpointResolution = createAsyncThunk(
"monitors/checkEnpoint",
async(data, thunkApi) => {
try{
const { authToken, monitorURL } = data;
const res = await networkService.checkEndpointResolution({
authToken: authToken,
monitorURL: monitorURL,
})
return res.data;
} catch (error) {
if (error.response && error.response.data) {
return thunkApi.rejectWithValue(error.response.data);
}
const payload = {
status: false,
msg: error.message ? error.message : "Unknown error",
};
return thunkApi.rejectWithValue(payload);
}
}
)
export const getUptimeMonitorById = createAsyncThunk( export const getUptimeMonitorById = createAsyncThunk(
"monitors/getMonitorById", "monitors/getMonitorById",
async (data, thunkApi) => { async (data, thunkApi) => {
@@ -271,6 +295,24 @@ const uptimeMonitorsSlice = createSlice({
: "Failed to create uptime monitor"; : "Failed to create uptime monitor";
}) })
// ***************************************************** // *****************************************************
// Resolve Endpoint
// *****************************************************
.addCase(checkEndpointResolution.pending, (state) => {
state.isLoading = true;
})
.addCase(checkEndpointResolution.fulfilled, (state, action) => {
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
})
.addCase(checkEndpointResolution.rejected, (state, action) => {
state.isLoading = false;
state.success = false;
state.msg = action.payload
? action.payload.msg
: "Failed to check endpoint resolution";
})
// *****************************************************
// Get Monitor By Id // Get Monitor By Id
// ***************************************************** // *****************************************************
.addCase(getUptimeMonitorById.pending, (state) => { .addCase(getUptimeMonitorById.pending, (state) => {
@@ -3,6 +3,7 @@ import { Box, Button, ButtonGroup, Stack, Typography } from "@mui/material";
import { useSelector, useDispatch } from "react-redux"; import { useSelector, useDispatch } from "react-redux";
import { monitorValidation } from "../../../Validation/validation"; import { monitorValidation } from "../../../Validation/validation";
import { createUptimeMonitor } from "../../../Features/UptimeMonitors/uptimeMonitorsSlice"; import { createUptimeMonitor } from "../../../Features/UptimeMonitors/uptimeMonitorsSlice";
import { checkEndpointResolution } from "../../../Features/UptimeMonitors/uptimeMonitorsSlice"
import { useNavigate, useParams } from "react-router-dom"; import { useNavigate, useParams } from "react-router-dom";
import { useTheme } from "@emotion/react"; import { useTheme } from "@emotion/react";
import { createToast } from "../../../Utils/toastUtils"; import { createToast } from "../../../Utils/toastUtils";
@@ -124,58 +125,55 @@ const CreateMonitor = () => {
const handleCreateMonitor = async (event) => { const handleCreateMonitor = async (event) => {
event.preventDefault(); event.preventDefault();
//obj to submit
let form = {
url:
//preprending protocol for url
monitor.type === "http"
? `http${https ? "s" : ""}://` + monitor.url
: monitor.url,
name: monitor.name === "" ? monitor.url : monitor.name,
type: monitor.type,
interval: monitor.interval * MS_PER_MINUTE,
};
const formattedUrl = monitor.type === "http" const { error } = monitorValidation.validate(form, {
? `http${https ? "s" : ""}://` + monitor.url abortEarly: false,
: monitor.url; });
try { if (error) {
// Check if the URL resolves by sending a request const newErrors = {};
const response = await fetch(formattedUrl, { method: 'GET', mode: 'no-cors' }); error.details.forEach((err) => {
if (!response) { newErrors[err.path[0]] = err.message;
throw new Error();
}
//obj to submit
let form = {
url:formattedUrl,
name: monitor.name === "" ? monitor.url : monitor.name,
type: monitor.type,
interval: monitor.interval * MS_PER_MINUTE,
};
const { error } = monitorValidation.validate(form, {
abortEarly: false,
}); });
setErrors(newErrors);
if (error) { createToast({ body: "Error validation data." });
const newErrors = {}; } else {
error.details.forEach((err) => { const checkEndpointAction = await dispatch(
newErrors[err.path[0]] = err.message; checkEndpointResolution({ authToken, monitorURL: form.url })
}); )
setErrors(newErrors); if (checkEndpointAction.meta.requestStatus === "rejected") {
createToast({ body: "Error validation data." }); createToast({ body: "The endpoint you entered doesn't resolve. Check the URL again." });
} else { setErrors({ url: "The entered URL is not reachable." });
form = { return;
...form, }
description: form.name,
teamId: user.teamId, form = {
userId: user._id, ...form,
notifications: monitor.notifications, description: form.name,
}; teamId: user.teamId,
const action = await dispatch( userId: user._id,
createUptimeMonitor({ authToken, monitor: form }) notifications: monitor.notifications,
); };
if (action.meta.requestStatus === "fulfilled") { const action = await dispatch(
createToast({ body: "Monitor created successfully!" }); createUptimeMonitor({ authToken, monitor: form })
navigate("/monitors"); );
} else { if (action.meta.requestStatus === "fulfilled") {
createToast({ body: "Failed to create monitor." }); createToast({ body: "Monitor created successfully!" });
} navigate("/monitors");
} else {
createToast({ body: "Failed to create monitor." });
} }
} catch (error) {
createToast({ body: "The endpoint you entered doesn't resolve. Check the URL again." });
setErrors({ url: "The entered URL is not reachable." });
} }
}; };
@@ -387,7 +385,7 @@ const CreateMonitor = () => {
onClick={handleCreateMonitor} onClick={handleCreateMonitor}
disabled={Object.keys(errors).length !== 0 && true} disabled={Object.keys(errors).length !== 0 && true}
> >
{monitor.type === "http" ? "Create Monitor" : "Check Endpoint"} Create Monitor
</Button> </Button>
</Stack> </Stack>
</Stack> </Stack>
+26
View File
@@ -89,6 +89,32 @@ class NetworkService {
}); });
} }
/**
*
* ************************************
* Check the endpoint resolution
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
* @param {Object} config.monitorURL - The monitor url to be sent in the request body.
* @returns {Promise<AxiosResponse>} The response from the axios POST request.
*/
async checkEndpointResolution(config) {
const { authToken, monitorURL } = config;
const params = new URLSearchParams();
if (monitorURL) params.append("monitorURL", monitorURL);
return this.axiosInstance.get(`/monitors/check-endpoint/url?${params.toString()}`, {
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
}
})
}
/** /**
* *
* ************************************ * ************************************
+39
View File
@@ -20,6 +20,7 @@ const jwt = require("jsonwebtoken");
const { getTokenFromHeaders } = require("../utils/utils"); const { getTokenFromHeaders } = require("../utils/utils");
const logger = require("../utils/logger"); const logger = require("../utils/logger");
const { handleError, handleValidationError } = require("./controllerUtils"); const { handleError, handleValidationError } = require("./controllerUtils");
const dns = require('dns');
/** /**
* Returns all monitors * 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. * Deletes a monitor by its ID and also deletes associated checks, alerts, and notifications.
* @async * @async
@@ -475,6 +513,7 @@ module.exports = {
getMonitorsAndSummaryByTeamId, getMonitorsAndSummaryByTeamId,
getMonitorsByTeamId, getMonitorsByTeamId,
createMonitor, createMonitor,
checkEndpointResolution,
deleteMonitor, deleteMonitor,
deleteAllMonitors, deleteAllMonitors,
editMonitor, editMonitor,
+6
View File
@@ -18,6 +18,12 @@ router.post(
monitorController.createMonitor monitorController.createMonitor
); );
router.get(
"/check-endpoint/url",
isAllowed(["admin", "superadmin"]),
monitorController.checkEndpointResolution
)
router.delete( router.delete(
"/:monitorId", "/:monitorId",
isAllowed(["admin", "superadmin"]), isAllowed(["admin", "superadmin"]),