-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (60 loc) · 2 KB
/
Copy pathindex.js
File metadata and controls
77 lines (60 loc) · 2 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
import express from 'express';
import indexRoutes from './routes/index.routes.js';
import indexMiddleware from './middlewares/index.middleware.js';
import { connectDB, prisma } from './config/db.js';
import { initWebSocket } from './services/websocket.service.js';
import dotenv from 'dotenv';
import admin from 'firebase-admin';
import { createRequire } from 'module';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const require = createRequire(import.meta.url);
const serviceAccount = require('./config/firebaseServiceAccountKey.json');
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(express.json());
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: process.env.FIREBASE_DATABASE_URL
});
// Routes
app.use('/', indexRoutes);
app.get('/', (req, res) => {
res.send('Welcome to the Express server!');
});
//static file for profile pictures
app.use(
'/uploads/profile-pictures',
express.static(path.join(__dirname, 'uploads/profile-pictures'))
);
async function startServer() {
try {
// Connect to database
await connectDB();
// Start the server
const server = app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
initWebSocket(server);
// Graceful shutdown
const shutdown = async () => {
console.log('Shutting down gracefully...');
await prisma.$disconnect();
console.log('Database disconnected.');
server.close(() => {
console.log('Server closed.');
process.exit(0);
});
};
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
} catch (error) {
console.error('Error starting server:', error);
process.exit(1);
}
}
startServer();