@@ -14,68 +14,77 @@ import { HttpStatus, router } from './whatsapp/routers/index.router';
1414import { waMonitor } from './whatsapp/whatsapp.module' ;
1515
1616function initWA ( ) {
17- waMonitor . loadInstance ( ) ;
17+ waMonitor . loadInstance ( ) ;
1818}
1919
2020function bootstrap ( ) {
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 ( err ) ;
52- }
53-
54- next ( ) ;
55- } ,
56- ( req : Request , res : Response , next : NextFunction ) => {
57- const { method, url } = req ;
58-
59- res . status ( HttpStatus . NOT_FOUND ) . json ( {
60- status : HttpStatus . NOT_FOUND ,
61- message : `Cannot ${ method . toUpperCase ( ) } ${ url } ` ,
62- error : 'Not Found' ,
63- } ) ;
64-
65- next ( ) ;
66- } ,
67- ) ;
68-
69- const httpServer = configService . get < HttpServer > ( 'SERVER' ) ;
70-
71- ServerUP . app = app ;
72- const server = ServerUP [ httpServer . TYPE ] ;
73-
74- server . listen ( httpServer . PORT , ( ) => logger . log ( httpServer . TYPE . toUpperCase ( ) + ' - ON: ' + httpServer . PORT ) ) ;
75-
76- initWA ( ) ;
77-
78- 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 ( ) ;
7988}
8089
8190bootstrap ( ) ;
0 commit comments