-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
85 lines (80 loc) · 2.75 KB
/
server.js
File metadata and controls
85 lines (80 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//=================Gọi các module=======================
const dotenv = require('dotenv');
dotenv.config({ path: './config.env' });
dotenv.config({ path: './.env' });
const app = require('./app');
const prisma = require('./utils/db');
const { elastic, reindexAllDocuments } = require('./utils/elastic');
const { scheduleDeleteOldFiles } = require('./utils/schedule');
const https = require('https');
const http = require('http');
const fs = require('fs');
//======================================================
//Kết nối PostgreSQL
async function connectDB() {
try {
await prisma.$connect();
console.log('✅ Kết nối Prisma (PostgreSQL) thành công');
} catch (err) {
console.error('❌ Lỗi kết nối Prisma:', err);
process.exit(1);
}
}
connectDB();
//Kết nối Elasticsearch
async function connectElastic() {
try {
await elastic.ping();
console.log('✅ Kết nối Elasticsearch thành công');
} catch (err) {
console.error('❌ Lỗi kết nối Elasticsearch:', err);
process.exit(1);
}
}
connectElastic();
// Chạy reindex tất cả document lên Elasticsearch
reindexAllDocuments(prisma);
//===================
// Khởi động scheduled tasks
scheduleDeleteOldFiles();
console.log('✅ Scheduled tasks initialized');
//===================
//Chạy server
let server;
if(process.env.NODE_ENV === 'production'){
const options = {
key: fs.readFileSync('/etc/letsencrypt/live/api.nguyentronghieu.io.vn/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/api.nguyentronghieu.io.vn/fullchain.pem')
};
server = https.createServer(options, app).listen(443, '0.0.0.0', () => {
console.log('✅ HTTPS Server đang chạy trên cổng 443');
});
} else {
server = http.createServer(app).listen(process.env.PORT, '0.0.0.0', () => {
console.log(`HTTP Server đang chạy trên cổng ${process.env.PORT}...`);
});
}
//============
//==================Xử lý có lỗi khi chạy ứng dụng thì ngừng ngay server==================
process.on('unhandledRejection', (err) => {
console.log('🚨 LỖI KHÔNG XỬ LÝ:', err.name, err.message);
console.log('Đang tắt ứng dụng...');
server.close(async () => {
await prisma.$disconnect();
process.exit(1);
});
});
process.on('uncaughtException', (err) => {
console.log('🚨 NGOẠI LỆ KHÔNG ĐƯỢC BẮT:', err.name, err.message);
console.log('Đang tắt ứng dụng...');
process.exit(1);
});
// Graceful shutdown
process.on('SIGTERM', async () => {
console.log('💀 SIGTERM received. Shutting down gracefully');
server.close(async () => {
await prisma.$disconnect();
console.log('💥 Process terminated!');
});
});
//========================================================================================