Skip to content

Commit 457d07e

Browse files
Update main.ts
Corrige estrutura de server.listen e inicialiazação do servidor
1 parent 3960624 commit 457d07e

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

src/main.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,17 @@ async function bootstrap() {
5959
app.set('view engine', 'hbs');
6060
app.set('views', join(ROOT_DIR, 'views'));
6161
app.use(express.static(join(ROOT_DIR, 'public')));
62-
6362
app.use('/store', express.static(join(ROOT_DIR, 'store')));
64-
6563
app.use('/', router);
6664

6765
app.use(
6866
(err: Error, req: Request, res: Response, next: NextFunction) => {
6967
if (err) {
7068
const webhook = configService.get<Webhook>('WEBHOOK');
7169

72-
if (webhook.EVENTS.ERRORS_WEBHOOK && webhook.EVENTS.ERRORS_WEBHOOK != '' && webhook.EVENTS.ERRORS) {
73-
const tzoffset = new Date().getTimezoneOffset() * 60000; //offset in milliseconds
70+
if (webhook.EVENTS.ERRORS_WEBHOOK && webhook.EVENTS.ERRORS_WEBHOOK !== '' && webhook.EVENTS) {
71+
const tzoffset = new Date().getTimezoneOffset() * 60000;
7472
const localISOTime = new Date(Date.now() - tzoffset).toISOString();
75-
const now = localISOTime;
7673
const globalApiKey = configService.get<Auth>('AUTHENTICATION').API_KEY.KEY;
7774
const serverUrl = configService.get<HttpServer>('SERVER').URL;
7875

@@ -86,17 +83,14 @@ async function bootstrap() {
8683
message: err['message'] || 'Internal Server Error',
8784
},
8885
},
89-
date_time: now,
86+
date_time: localISOTime,
9087
api_key: globalApiKey,
9188
server_url: serverUrl,
9289
};
9390

9491
logger.error(errorData);
95-
9692
const baseURL = webhook.EVENTS.ERRORS_WEBHOOK;
97-
const httpService = axios.create({ baseURL });
98-
99-
httpService.post('', errorData);
93+
axios.create({ baseURL }).post('', errorData);
10094
}
10195

10296
return res.status(err['status'] || 500).json({
@@ -112,28 +106,26 @@ async function bootstrap() {
112106
},
113107
(req: Request, res: Response, next: NextFunction) => {
114108
const { method, url } = req;
115-
116109
res.status(HttpStatus.NOT_FOUND).json({
117110
status: HttpStatus.NOT_FOUND,
118111
error: 'Not Found',
119112
response: {
120113
message: [`Cannot ${method.toUpperCase()} ${url}`],
121114
},
122115
});
123-
124116
next();
125117
},
126118
);
127119

128120
const httpServer = configService.get<HttpServer>('SERVER');
121+
const PORT = process.env.PORT || httpServer.PORT || 8080;
129122

130123
ServerUP.app = app;
131124
let server = ServerUP[httpServer.TYPE];
132125

133126
if (server === null) {
134127
logger.warn('SSL cert load failed — falling back to HTTP.');
135128
logger.info("Ensure 'SSL_CONF_PRIVKEY' and 'SSL_CONF_FULLCHAIN' env vars point to valid certificate files.");
136-
137129
httpServer.TYPE = 'http';
138130
server = ServerUP[httpServer.TYPE];
139131
}
@@ -142,16 +134,14 @@ async function bootstrap() {
142134

143135
if (process.env.SENTRY_DSN) {
144136
logger.info('Sentry - ON');
145-
146-
// Add this after all routes,
147-
// but before any and other error-handling middlewares are defined
148137
Sentry.setupExpressErrorHandler(app);
149138
}
150139

151-
server.listen(httpServer.PORT, () => logger.log(httpServer.TYPE.toUpperCase() + ' - ON: ' + httpServer.PORT));
140+
server.listen(PORT, () => {
141+
logger.log(`${httpServer.TYPE.toUpperCase()} - ON: ${PORT}`);
142+
});
152143

153144
initWA();
154-
155145
onUnexpectedError();
156146
}
157147

0 commit comments

Comments
 (0)