11import 'express-async-errors' ;
22
3- // import * as Sentry from '@sentry/node';
43import compression from 'compression' ;
54import cors from 'cors' ;
65import express , { json , NextFunction , Request , Response , urlencoded } from 'express' ;
@@ -15,94 +14,77 @@ import { HttpStatus, router } from './whatsapp/routers/index.router';
1514import { waMonitor } from './whatsapp/whatsapp.module' ;
1615
1716function initWA ( ) {
18- waMonitor . loadInstance ( ) ;
17+ waMonitor . loadInstance ( ) ;
1918}
2019
2120function bootstrap ( ) {
22- const logger = new Logger ( 'SERVER' ) ;
23- const app = express ( ) ;
24-
25- // Sentry.init({
26- // dsn: '',
27- // integrations: [
28- // // enable HTTP calls tracing
29- // new Sentry.Integrations.Http({ tracing: true }),
30- // // enable Express.js middleware tracing
31- // new Sentry.Integrations.Express({ app }),
32- // // Automatically instrument Node.js libraries and frameworks
33- // ...Sentry.autoDiscoverNodePerformanceMonitoringIntegrations(),
34- // ],
35-
36- // // Set tracesSampleRate to 1.0 to capture 100%
37- // // of transactions for performance monitoring.
38- // // We recommend adjusting this value in production
39- // tracesSampleRate: 1.0,
40- // });
41-
42- // app.use(Sentry.Handlers.requestHandler());
43-
44- // app.use(Sentry.Handlers.tracingHandler());
45-
46- app . use (
47- cors ( {
48- origin ( requestOrigin , callback ) {
49- const { ORIGIN } = configService . get < Cors > ( 'CORS' ) ;
50- ! requestOrigin ? ( requestOrigin = '*' ) : undefined ;
51- if ( ORIGIN . indexOf ( requestOrigin ) !== - 1 ) {
52- return callback ( null , true ) ;
53- }
54- return callback ( new Error ( 'Not allowed by CORS' ) ) ;
55- } ,
56- methods : [ ...configService . get < Cors > ( 'CORS' ) . METHODS ] ,
57- credentials : configService . get < Cors > ( 'CORS' ) . CREDENTIALS ,
58- } ) ,
59- urlencoded ( { extended : true , limit : '136mb' } ) ,
60- json ( { limit : '136mb' } ) ,
61- compression ( ) ,
62- ) ;
63-
64- app . set ( 'view engine' , 'hbs' ) ;
65- app . set ( 'views' , join ( ROOT_DIR , 'views' ) ) ;
66- app . use ( express . static ( join ( ROOT_DIR , 'public' ) ) ) ;
67-
68- app . use ( '/' , router ) ;
69-
70- // app.use(Sentry.Handlers.errorHandler());
71-
72- // app.use(function onError(err, req, res, next) {
73- // res.statusCode = 500;
74- // res.end(res.sentry + '\n');
75- // });
76-
77- app . use (
78- ( err : Error , req : Request , res : Response ) => {
79- if ( err ) {
80- return res . status ( err [ 'status' ] || 500 ) . json ( err ) ;
81- }
82- } ,
83- ( req : Request , res : Response , next : NextFunction ) => {
84- const { method, url } = req ;
85-
86- res . status ( HttpStatus . NOT_FOUND ) . json ( {
87- status : HttpStatus . NOT_FOUND ,
88- message : `Cannot ${ method . toUpperCase ( ) } ${ url } ` ,
89- error : 'Not Found' ,
90- } ) ;
91-
92- next ( ) ;
93- } ,
94- ) ;
95-
96- const httpServer = configService . get < HttpServer > ( 'SERVER' ) ;
97-
98- ServerUP . app = app ;
99- const server = ServerUP [ httpServer . TYPE ] ;
100-
101- server . listen ( httpServer . PORT , ( ) => logger . log ( httpServer . TYPE . toUpperCase ( ) + ' - ON: ' + httpServer . PORT ) ) ;
102-
103- initWA ( ) ;
104-
105- onUnexpectedError ( ) ;
21+ const logger = new Logger ( 'SERVER' ) ;
22+ const app = express ( ) ;
23+
24+ app . use (
25+ cors ( {
26+ origin ( requestOrigin , callback ) {
27+ const { ORIGIN } = configService . get < Cors > ( 'CORS' ) ;
28+ ! requestOrigin ? ( requestOrigin = '*' ) : undefined ;
29+ if ( ORIGIN . indexOf ( requestOrigin ) !== - 1 ) {
30+ return callback ( null , true ) ;
31+ }
32+ return callback ( new Error ( 'Not allowed by CORS' ) ) ;
33+ } ,
34+ methods : [ ...configService . get < Cors > ( 'CORS' ) . METHODS ] ,
35+ credentials : configService . get < Cors > ( 'CORS' ) . CREDENTIALS ,
36+ } ) ,
37+ urlencoded ( { extended : true , limit : '136mb' } ) ,
38+ json ( { limit : '136mb' } ) ,
39+ compression ( ) ,
40+ ) ;
41+
42+ app . set ( 'view engine' , 'hbs' ) ;
43+ app . set ( 'views' , join ( ROOT_DIR , 'views' ) ) ;
44+ app . use ( express . static ( join ( ROOT_DIR , 'public' ) ) ) ;
45+
46+ app . use ( '/' , router ) ;
47+
48+ app . use (
49+ ( err : Error , req : Request , res : Response , next : NextFunction ) => {
50+ if ( err ) {
51+ return res . status ( err [ 'status' ] || 500 ) . json ( {
52+ status : 'ERROR' ,
53+ error : err [ 'error' ] || 'Internal Server Error' ,
54+ response : {
55+ message : err [ 'message' ] || 'Internal Server Error' ,
56+ } ,
57+ }
58+ ) ;
59+ }
60+
61+ next ( ) ;
62+ } ,
63+ ( req : Request , res : Response , next : NextFunction ) => {
64+ const { method, url } = req ;
65+
66+ res . status ( HttpStatus . NOT_FOUND ) . json ( {
67+ status : HttpStatus . NOT_FOUND ,
68+ error : 'Not Found' ,
69+ response : {
70+ message : `Cannot ${ method . toUpperCase ( ) } ${ url } ` ,
71+ } ,
72+ } ) ;
73+
74+ next ( ) ;
75+ } ,
76+ ) ;
77+
78+ const httpServer = configService . get < HttpServer > ( 'SERVER' ) ;
79+
80+ ServerUP . app = app ;
81+ const server = ServerUP [ httpServer . TYPE ] ;
82+
83+ server . listen ( httpServer . PORT , ( ) => logger . log ( httpServer . TYPE . toUpperCase ( ) + ' - ON: ' + httpServer . PORT ) ) ;
84+
85+ initWA ( ) ;
86+
87+ onUnexpectedError ( ) ;
10688}
10789
10890bootstrap ( ) ;
0 commit comments