Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
101 lines (82 loc) · 2.38 KB
/
Copy pathserver.js
File metadata and controls
101 lines (82 loc) · 2.38 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
101
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const morgan = require('morgan');
const rateLimit = require('express-rate-limit');
// Import custom modules
const connectDB = require('./config/database');
const logger = require('./config/logger');
const errorHandler = require('./middleware/errorHandler');
const deviceRoutes = require('./routes/deviceRoutes');
const deviceService = require('./services/deviceService');
const app = express();
// Security middleware
app.use(helmet());
// Rate limiting
// const limiter = rateLimit({
// windowMs: 60 * 60 * 1000, // 60 minutes
// max: 500, // limit each IP to 500 requests per windowMs
// message: 'Too many requests from this IP, please try again later.'
// });
// app.use('/api', limiter);
// CORS
app.use(cors({
origin: process.env.CORS_ORIGIN || '*',
credentials: true
}));
// Logging
app.use(morgan('combined', {
stream: { write: message => logger.info(message.trim()) }
}));
// Body parsing
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true }));
// Static files
app.use(express.static('public'));
// Health check endpoint
app.get('/health', (req, res) => {
res.status(200).json({
status: 'OK',
timestamp: new Date().toISOString(),
uptime: process.uptime()
});
});
// API Routes
app.use('/api', deviceRoutes);
// Error handling middleware
app.use(errorHandler);
// 404 handler
app.use('*', (req, res) => {
res.status(404).json({ error: 'Route not found' });
});
// Initialize server
const PORT = process.env.PORT || 3000;
async function startServer() {
try {
// Connect to database
await connectDB();
// Start background services
deviceService.startPositionUpdater();
// Start server
app.listen(PORT, () => {
logger.info(`🚀 GoFasta API listening on port ${PORT}`);
console.log(`🚀 GoFasta API listening on port ${PORT}`);
});
} catch (error) {
logger.error('Failed to start server:', error);
process.exit(1);
}
}
// Graceful shutdown
process.on('SIGTERM', () => {
logger.info('SIGTERM received, shutting down gracefully');
deviceService.stopPositionUpdater();
process.exit(0);
});
process.on('SIGINT', () => {
logger.info('SIGINT received, shutting down gracefully');
deviceService.stopPositionUpdater();
process.exit(0);
});
startServer();