Use the NetworkService for making network requests.
This commit is contained in:
1 parent
92d9573fcf
commit
cc48f1d3aa
3 files changed
+101
-43
No files matched your search
+13
-1
@@ -38,6 +38,10 @@ import QueueController from "./controllers/queueController.js";
|
|||||||
import DistributedUptimeRoutes from "./routes/distributedUptimeRoute.js";
|
import DistributedUptimeRoutes from "./routes/distributedUptimeRoute.js";
|
||||||
import DistributedUptimeController from "./controllers/distributedUptimeController.js";
|
import DistributedUptimeController from "./controllers/distributedUptimeController.js";
|
||||||
|
|
||||||
|
import NotificationRoutes from "./routes/notificationRoute.js"; // Add this line
|
||||||
|
|
||||||
|
import NotificationController from "./controllers/notificationController.js";
|
||||||
|
|
||||||
//JobQueue service and dependencies
|
//JobQueue service and dependencies
|
||||||
import JobQueue from "./service/jobQueue.js";
|
import JobQueue from "./service/jobQueue.js";
|
||||||
import { Queue, Worker } from "bullmq";
|
import { Queue, Worker } from "bullmq";
|
||||||
@@ -174,7 +178,7 @@ const startApp = async () => {
|
|||||||
);
|
);
|
||||||
const networkService = new NetworkService(axios, ping, logger, http, Docker, net);
|
const networkService = new NetworkService(axios, ping, logger, http, Docker, net);
|
||||||
const statusService = new StatusService(db, logger);
|
const statusService = new StatusService(db, logger);
|
||||||
const notificationService = new NotificationService(emailService, db, logger);
|
const notificationService = new NotificationService(emailService, db, logger, networkService);
|
||||||
|
|
||||||
const jobQueue = new JobQueue(
|
const jobQueue = new JobQueue(
|
||||||
db,
|
db,
|
||||||
@@ -244,6 +248,10 @@ const startApp = async () => {
|
|||||||
ServiceRegistry.get(MongoDB.SERVICE_NAME)
|
ServiceRegistry.get(MongoDB.SERVICE_NAME)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const notificationController = new NotificationController(
|
||||||
|
ServiceRegistry.get(NotificationService.SERVICE_NAME)
|
||||||
|
);
|
||||||
|
|
||||||
const distributedUptimeController = new DistributedUptimeController();
|
const distributedUptimeController = new DistributedUptimeController();
|
||||||
|
|
||||||
//Create routes
|
//Create routes
|
||||||
@@ -260,6 +268,9 @@ const startApp = async () => {
|
|||||||
const distributedUptimeRoutes = new DistributedUptimeRoutes(
|
const distributedUptimeRoutes = new DistributedUptimeRoutes(
|
||||||
distributedUptimeController
|
distributedUptimeController
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const notificationRoutes = new NotificationRoutes(notificationController);
|
||||||
|
|
||||||
// Init job queue
|
// Init job queue
|
||||||
await jobQueue.initJobQueue();
|
await jobQueue.initJobQueue();
|
||||||
// Middleware
|
// Middleware
|
||||||
@@ -284,6 +295,7 @@ const startApp = async () => {
|
|||||||
app.use("/api/v1/queue", verifyJWT, queueRoutes.getRouter());
|
app.use("/api/v1/queue", verifyJWT, queueRoutes.getRouter());
|
||||||
app.use("/api/v1/distributed-uptime", distributedUptimeRoutes.getRouter());
|
app.use("/api/v1/distributed-uptime", distributedUptimeRoutes.getRouter());
|
||||||
app.use("/api/v1/status-page", statusPageRoutes.getRouter());
|
app.use("/api/v1/status-page", statusPageRoutes.getRouter());
|
||||||
|
app.use("/api/v1/notifications", notificationRoutes.getRouter()); // Add this line
|
||||||
app.use(handleErrors);
|
app.use(handleErrors);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -336,6 +336,52 @@ class NetworkService {
|
|||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async requestWebhook(platform, url, message) {
|
||||||
|
try {
|
||||||
|
const { response, responseTime, error } = await this.timeRequest(() =>
|
||||||
|
this.axios.post(url, message, {
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
const webhookResponse = {
|
||||||
|
type: 'webhook',
|
||||||
|
responseTime,
|
||||||
|
payload: response?.data
|
||||||
|
};
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
webhookResponse.status = false;
|
||||||
|
webhookResponse.code = error.response?.status || this.NETWORK_ERROR;
|
||||||
|
webhookResponse.message = `Failed to send ${platform} notification`;
|
||||||
|
this.logger.warn({
|
||||||
|
message: error.message,
|
||||||
|
service: this.SERVICE_NAME,
|
||||||
|
method: 'requestWebhook',
|
||||||
|
url,
|
||||||
|
platform,
|
||||||
|
error: error.message,
|
||||||
|
statusCode: error.response?.status,
|
||||||
|
responseData: error.response?.data,
|
||||||
|
requestPayload: message
|
||||||
|
});
|
||||||
|
return webhookResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
webhookResponse.status = true;
|
||||||
|
webhookResponse.code = response.status;
|
||||||
|
webhookResponse.message = `Successfully sent ${platform} notification`;
|
||||||
|
return webhookResponse;
|
||||||
|
} catch (error) {
|
||||||
|
error.service = this.SERVICE_NAME;
|
||||||
|
error.method = 'requestWebhook';
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the status of a job based on its type and returns the appropriate response.
|
* Gets the status of a job based on its type and returns the appropriate response.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
const SERVICE_NAME = "NotificationService";
|
const SERVICE_NAME = "NotificationService";
|
||||||
import axios from 'axios';
|
import NetworkService from "./networkService.js";
|
||||||
|
|
||||||
|
|
||||||
class NotificationService {
|
class NotificationService {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
@@ -10,50 +11,53 @@ class NotificationService {
|
|||||||
* @param {Object} db - The database instance for storing notification data.
|
* @param {Object} db - The database instance for storing notification data.
|
||||||
* @param {Object} logger - The logger instance for logging activities.
|
* @param {Object} logger - The logger instance for logging activities.
|
||||||
*/
|
*/
|
||||||
constructor(emailService, db, logger) {
|
constructor(emailService, db, logger, networkService) {
|
||||||
this.SERVICE_NAME = SERVICE_NAME;
|
this.SERVICE_NAME = SERVICE_NAME;
|
||||||
this.emailService = emailService;
|
this.emailService = emailService;
|
||||||
this.db = db;
|
this.db = db;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
|
this.networkService = networkService;
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendWebhookNotification(networkResponse, address, platform) {
|
async sendWebhookNotification(networkResponse, address, platform) {
|
||||||
const { monitor, status } = networkResponse;
|
const { monitor, status } = networkResponse;
|
||||||
let message;
|
let message;
|
||||||
let url = address;
|
let url = address;
|
||||||
|
|
||||||
if (platform === 'slack') {
|
if (platform === 'slack') {
|
||||||
message = { text: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}` };
|
message = { text: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}` };
|
||||||
} else if (platform === 'discord') {
|
} else if (platform === 'discord') {
|
||||||
message = { content: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}` };
|
message = { content: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}` };
|
||||||
} else if (platform === 'telegram') {
|
} else if (platform === 'telegram') {
|
||||||
const [botToken, chatId] = address.split('|').map(part => part?.trim());
|
const [botToken, chatId] = address.split('|').map(part => part?.trim());
|
||||||
if (!botToken || !chatId) {
|
if (!botToken || !chatId) {
|
||||||
console.error('Invalid Telegram address format');
|
return false;
|
||||||
return false;
|
}
|
||||||
}
|
message = {
|
||||||
message = {
|
chat_id: chatId,
|
||||||
chat_id: chatId,
|
text: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}`
|
||||||
text: `Monitor ${monitor.name} is ${status ? "up" : "down"}. URL: ${monitor.url}`
|
};
|
||||||
};
|
url = `https://api.telegram.org/bot${botToken}/sendMessage`;
|
||||||
url = `https://api.telegram.org/bot${botToken}/sendMessage`;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await axios.post(url, message, {
|
const response = await this.networkService.requestWebhook(platform, url, message);
|
||||||
headers: {
|
return response.status;
|
||||||
'Content-Type': 'application/json'
|
} catch (error) {
|
||||||
}
|
this.logger.error({
|
||||||
});
|
message: `Error sending ${platform} notification`,
|
||||||
return true;
|
service: this.SERVICE_NAME,
|
||||||
} catch (error) {
|
method: 'sendWebhookNotification',
|
||||||
|
error: error.message,
|
||||||
|
stack: error.stack,
|
||||||
|
url,
|
||||||
|
platform,
|
||||||
|
requestPayload: message
|
||||||
|
});
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
console.error(`Error sending ${platform} notification:`, error.toJSON());
|
|
||||||
console.error(`URL: ${url}`);
|
|
||||||
console.error(`Message:`, message);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -105,16 +109,12 @@ class NotificationService {
|
|||||||
for (const notification of notifications) {
|
for (const notification of notifications) {
|
||||||
if (notification.type === "email") {
|
if (notification.type === "email") {
|
||||||
this.sendEmail(networkResponse, notification.address);
|
this.sendEmail(networkResponse, notification.address);
|
||||||
} else if (notification.type === "discord") {
|
} else if (["discord", "slack", "telegram"].includes(notification.type)) {
|
||||||
this.sendDiscordNotification(networkResponse, notification.address);
|
this.sendWebhookNotification(networkResponse, notification.address, notification.type);
|
||||||
} else if (notification.type === "slack") {
|
|
||||||
this.sendSlackNotification(networkResponse, notification.address);
|
|
||||||
} else if (notification.type === "telegram") {
|
|
||||||
this.sendTelegramNotification(networkResponse, notification.address);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle other types of notifications here
|
// Handle other types of notifications here
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.warn({
|
this.logger.warn({
|
||||||
|
|||||||
Reference in new issue
Block a user