Update main.ts

Corrige estrutura de server.listen e inicialiazação do servidor
This commit is contained in:
victor-lucrabot 2025-06-25 20:03:53 -03:00 committed by GitHub
parent 39606240da
commit 457d07e3cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -59,9 +59,7 @@ async function bootstrap() {
app.set('view engine', 'hbs'); app.set('view engine', 'hbs');
app.set('views', join(ROOT_DIR, 'views')); app.set('views', join(ROOT_DIR, 'views'));
app.use(express.static(join(ROOT_DIR, 'public'))); app.use(express.static(join(ROOT_DIR, 'public')));
app.use('/store', express.static(join(ROOT_DIR, 'store'))); app.use('/store', express.static(join(ROOT_DIR, 'store')));
app.use('/', router); app.use('/', router);
app.use( app.use(
@ -69,10 +67,9 @@ async function bootstrap() {
if (err) { if (err) {
const webhook = configService.get<Webhook>('WEBHOOK'); const webhook = configService.get<Webhook>('WEBHOOK');
if (webhook.EVENTS.ERRORS_WEBHOOK && webhook.EVENTS.ERRORS_WEBHOOK != '' && webhook.EVENTS.ERRORS) { if (webhook.EVENTS.ERRORS_WEBHOOK && webhook.EVENTS.ERRORS_WEBHOOK !== '' && webhook.EVENTS) {
const tzoffset = new Date().getTimezoneOffset() * 60000; //offset in milliseconds const tzoffset = new Date().getTimezoneOffset() * 60000;
const localISOTime = new Date(Date.now() - tzoffset).toISOString(); const localISOTime = new Date(Date.now() - tzoffset).toISOString();
const now = localISOTime;
const globalApiKey = configService.get<Auth>('AUTHENTICATION').API_KEY.KEY; const globalApiKey = configService.get<Auth>('AUTHENTICATION').API_KEY.KEY;
const serverUrl = configService.get<HttpServer>('SERVER').URL; const serverUrl = configService.get<HttpServer>('SERVER').URL;
@ -86,17 +83,14 @@ async function bootstrap() {
message: err['message'] || 'Internal Server Error', message: err['message'] || 'Internal Server Error',
}, },
}, },
date_time: now, date_time: localISOTime,
api_key: globalApiKey, api_key: globalApiKey,
server_url: serverUrl, server_url: serverUrl,
}; };
logger.error(errorData); logger.error(errorData);
const baseURL = webhook.EVENTS.ERRORS_WEBHOOK; const baseURL = webhook.EVENTS.ERRORS_WEBHOOK;
const httpService = axios.create({ baseURL }); axios.create({ baseURL }).post('', errorData);
httpService.post('', errorData);
} }
return res.status(err['status'] || 500).json({ return res.status(err['status'] || 500).json({
@ -112,7 +106,6 @@ async function bootstrap() {
}, },
(req: Request, res: Response, next: NextFunction) => { (req: Request, res: Response, next: NextFunction) => {
const { method, url } = req; const { method, url } = req;
res.status(HttpStatus.NOT_FOUND).json({ res.status(HttpStatus.NOT_FOUND).json({
status: HttpStatus.NOT_FOUND, status: HttpStatus.NOT_FOUND,
error: 'Not Found', error: 'Not Found',
@ -120,12 +113,12 @@ async function bootstrap() {
message: [`Cannot ${method.toUpperCase()} ${url}`], message: [`Cannot ${method.toUpperCase()} ${url}`],
}, },
}); });
next(); next();
}, },
); );
const httpServer = configService.get<HttpServer>('SERVER'); const httpServer = configService.get<HttpServer>('SERVER');
const PORT = process.env.PORT || httpServer.PORT || 8080;
ServerUP.app = app; ServerUP.app = app;
let server = ServerUP[httpServer.TYPE]; let server = ServerUP[httpServer.TYPE];
@ -133,7 +126,6 @@ async function bootstrap() {
if (server === null) { if (server === null) {
logger.warn('SSL cert load failed — falling back to HTTP.'); logger.warn('SSL cert load failed — falling back to HTTP.');
logger.info("Ensure 'SSL_CONF_PRIVKEY' and 'SSL_CONF_FULLCHAIN' env vars point to valid certificate files."); logger.info("Ensure 'SSL_CONF_PRIVKEY' and 'SSL_CONF_FULLCHAIN' env vars point to valid certificate files.");
httpServer.TYPE = 'http'; httpServer.TYPE = 'http';
server = ServerUP[httpServer.TYPE]; server = ServerUP[httpServer.TYPE];
} }
@ -142,16 +134,14 @@ async function bootstrap() {
if (process.env.SENTRY_DSN) { if (process.env.SENTRY_DSN) {
logger.info('Sentry - ON'); logger.info('Sentry - ON');
// Add this after all routes,
// but before any and other error-handling middlewares are defined
Sentry.setupExpressErrorHandler(app); Sentry.setupExpressErrorHandler(app);
} }
server.listen(httpServer.PORT, () => logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT)); server.listen(PORT, () => {
logger.log(`${httpServer.TYPE.toUpperCase()} - ON: ${PORT}`);
});
initWA(); initWA();
onUnexpectedError(); onUnexpectedError();
} }