From ce4113142452d7d5429ea20d4f0bdb12da2a1e05 Mon Sep 17 00:00:00 2001 From: Francis Breit Date: Wed, 20 Sep 2023 01:40:59 -0300 Subject: [PATCH] Update main.ts --- src/main.ts | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/main.ts b/src/main.ts index 75dd95b3..e239f83a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,11 +1,12 @@ import 'express-async-errors'; +import axios from 'axios'; import compression from 'compression'; import cors from 'cors'; import express, { json, NextFunction, Request, Response, urlencoded } from 'express'; import { join } from 'path'; -import { configService, Cors, HttpServer, Rabbitmq } from './config/env.config'; +import { Auth, configService, Cors, HttpServer, Rabbitmq, Webhook } from './config/env.config'; import { onUnexpectedError } from './config/error.config'; import { Logger } from './config/logger.config'; import { ROOT_DIR } from './config/path.config'; @@ -47,11 +48,45 @@ function bootstrap() { app.set('views', join(ROOT_DIR, 'views')); app.use(express.static(join(ROOT_DIR, 'public'))); + app.use('/store', express.static(join(ROOT_DIR, 'store'))); + app.use('/', router); app.use( (err: Error, req: Request, res: Response, next: NextFunction) => { if (err) { + const webhook = configService.get('WEBHOOK'); + + if (webhook.EVENTS.ERRORS_WEBHOOK && webhook.EVENTS.ERRORS_WEBHOOK != '' && webhook.EVENTS.ERRORS) { + const tzoffset = new Date().getTimezoneOffset() * 60000; //offset in milliseconds + const localISOTime = new Date(Date.now() - tzoffset).toISOString(); + const now = localISOTime; + const globalApiKey = configService.get('AUTHENTICATION').API_KEY.KEY; + const serverUrl = configService.get('SERVER').URL; + + const errorData = { + event: 'error', + data: { + error: err['error'] || 'Internal Server Error', + message: err['message'] || 'Internal Server Error', + status: err['status'] || 500, + response: { + message: err['message'] || 'Internal Server Error', + }, + }, + date_time: now, + api_key: globalApiKey, + server_url: serverUrl, + }; + + logger.error(errorData); + + const baseURL = webhook.EVENTS.ERRORS_WEBHOOK; + const httpService = axios.create({ baseURL }); + + httpService.post('', errorData); + } + return res.status(err['status'] || 500).json({ status: err['status'] || 500, error: err['error'] || 'Internal Server Error', @@ -89,7 +124,7 @@ function bootstrap() { initIO(server); - if (configService.get('RABBITMQ').ENABLED) initAMQP(); + if (configService.get('RABBITMQ')?.ENABLED) initAMQP(); onUnexpectedError(); }