oneuptime/Ingestor/API/IncomingRequest.ts

78 lines
2.3 KiB
TypeScript
Raw Normal View History

2023-07-29 14:11:10 +00:00
import Express, {
ExpressRequest,
ExpressResponse,
ExpressRouter,
NextFunction,
} from 'CommonServer/Utils/Express';
import Response from 'CommonServer/Utils/Response';
import BadDataException from 'Common/Types/Exception/BadDataException';
2023-07-30 15:44:08 +00:00
import ProbeMonitorResponseService from 'CommonServer/Utils/Probe/ProbeMonitorResponse';
2023-07-29 14:11:10 +00:00
import Dictionary from 'Common/Types/Dictionary';
import { JSONObject } from 'Common/Types/JSON';
2023-07-29 15:21:47 +00:00
import ObjectID from 'Common/Types/ObjectID';
import IncomingMonitorRequest from 'Common/Types/Monitor/IncomingMonitor/IncomingMonitorRequest';
2023-07-30 15:44:08 +00:00
import OneUptimeDate from 'Common/Types/Date';
2023-07-29 14:11:10 +00:00
const router: ExpressRouter = Express.getRouter();
2023-07-30 21:03:17 +00:00
const processIncomingRequest: Function = async (
req: ExpressRequest,
2023-07-30 15:51:21 +00:00
res: ExpressResponse,
2023-07-30 21:03:17 +00:00
next: NextFunction
): Promise<void> => {
2023-07-30 15:51:21 +00:00
try {
const requestHeaders: Dictionary<string> =
req.headers as Dictionary<string>;
const requestBody: string | JSONObject = req.body as
| string
| JSONObject;
2023-07-31 12:17:20 +00:00
const monitorIdAsString: string | undefined = req.params['id'];
2023-07-30 15:51:21 +00:00
if (!monitorIdAsString) {
throw new BadDataException('Monitor Id is required');
}
const monitorId: ObjectID = ObjectID.fromString(monitorIdAsString);
const incomingRequest: IncomingMonitorRequest = {
monitorId: monitorId,
requestHeaders: requestHeaders,
requestBody: requestBody,
incomingRequestReceivedAt: OneUptimeDate.getCurrentDate(),
onlyCheckForIncomingRequestReceivedAt: false,
};
// process probe response here.
2023-07-31 12:33:34 +00:00
await ProbeMonitorResponseService.processProbeResponse(incomingRequest);
2023-07-30 15:51:21 +00:00
2023-07-31 12:33:34 +00:00
return Response.sendEmptyResponse(req, res);
2023-07-30 15:51:21 +00:00
} catch (err) {
return next(err);
}
2023-07-30 21:03:17 +00:00
};
2023-07-30 15:51:21 +00:00
2023-07-29 14:11:10 +00:00
router.post(
2023-07-31 12:17:20 +00:00
'/incoming-request/:id',
2023-07-29 14:11:10 +00:00
async (
req: ExpressRequest,
res: ExpressResponse,
next: NextFunction
): Promise<void> => {
2023-07-30 15:51:21 +00:00
await processIncomingRequest(req, res, next);
2023-07-29 14:11:10 +00:00
}
);
2023-07-30 15:51:21 +00:00
router.get(
2023-07-31 12:17:20 +00:00
'/incoming-request/:id',
2023-07-30 15:51:21 +00:00
async (
req: ExpressRequest,
res: ExpressResponse,
next: NextFunction
): Promise<void> => {
await processIncomingRequest(req, res, next);
}
);
2023-07-29 14:11:10 +00:00
export default router;