-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
100 lines (83 loc) · 2.96 KB
/
server.js
File metadata and controls
100 lines (83 loc) · 2.96 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import express from 'express';
import prisma from './prisma/lib/prisma.js';
import https from 'https';
import fs from 'fs';
import { initSocket } from './socket/chatSocket.js';
import cors from 'cors';
import cron from 'node-cron';
import { deleteOldEntries } from './utils/cleanOldEntries.js';
import path from 'path';
import { fileURLToPath } from 'url';
import userRoutes from './routes/signupRoutes.js';
import chatRoutes from './routes/chatRoutes.js';
import newsRoutes from './routes/newsRoutes.js';
import loginRoutes from './routes/loginRoutes.js';
import passForgotRoutes from './routes/passForgotRoutes.js';
import accountRoutes from './routes/accountRoutes.js';
import pushTokenRoutes from './routes/pushTokenRoutes.js';
import feedbackRoutes from './routes/feedbackRoutes.js';
const app = express();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Lokaler Pfad zu den SSL-Zertifikaten
const options = {
key: fs.readFileSync('./certsSSL/private.key'), // PRIVATE KEY!
cert: fs.readFileSync('./certsSSL/certificate.crt'), // DOMAIN-CERTIFICATE
ca: fs.readFileSync('./certsSSL/GlobalSign.crt') // CHAIN/INTERMEDIATE CERTIFICATE
};
// Database connection
prisma.$connect()
.then(() => {
console.log('✅ Database connection established');
})
.catch((error) => {
console.error('❌ Prisma database connection error:', error);
process.exit(1);
});
//app.use(cors());
app.use(cors({
origin: '*',
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization'],
}));
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ extended: true, limit: '50mb' }));
// Deploy static files from the "public" folder
app.use(express.static(path.join(__dirname, 'public')));
// API routes
app.use('/api/users', userRoutes);
app.use('/api/login', loginRoutes);
app.use('/api/chat', chatRoutes);
app.use('/api/news', newsRoutes);
app.use('/api/forgot-password', passForgotRoutes);
app.use('/api', accountRoutes);
app.use('/api/push-token', pushTokenRoutes);
app.use("/api", feedbackRoutes);
// Fallback for invalid routes
app.use((req, res, next) => {
res.status(404).json({ error: 'Route not found' });
});
// Error handling
app.use((err, req, res, next) => {
console.error('❌ Error in the server:', err);
res.status(500).json({ error: 'Server error' });
});
// Cronjob for automatic cleanup
cron.schedule('0 0 * * *', () => {
console.log(`⏰ Start automatic cleanup at ${new Date().toISOString()}`);
deleteOldEntries();
});
const PORT = process.env.PORT || ****;
// HTTPs servers start on port **** with HTTPS
const server = https.createServer(options, app);
// Initialize WebSocket
initSocket(server);
// Start server
server.listen(PORT, '0.0.0.0', () => {
console.log(`HTTPS Server läuft auf https://*********.com:****`);
});
// Disconnect Prisma at server end
process.on('SIGINT', async () => {
await prisma.$disconnect();
process.exit(0);
});