const express = require('express'); const cors = require('cors'); const wazeRoutes = require('./routes/waze'); const routingRoutes = require('./routes/routing'); const placesRoutes = require('./routes/places'); const app = express(); const PORT = process.env.PORT || 3000; // Standard middleware app.use(cors()); app.use(express.json()); app.set('trust proxy', 1); // Health check & discovery endpoint (Preserves legacy contract for MotorSignal TEST CONNECTION) app.get(['/', '/health', '/test'], (req, res) => { res.json({ status: 'ok', service: 'MotorSignal Waze Community Relay', version: '1.1.0', timestamp: new Date().toISOString(), uptimeSeconds: Math.floor(process.uptime()), features: [ 'police', 'speed_cameras', 'hazards', 'accidents', 'traffic', 'routing', 'places' ] }); }); // Mount Waze routes on legacy and modern endpoints app.use('/alerts', wazeRoutes); app.use('/api/alerts', wazeRoutes); app.use('/api/waze', wazeRoutes); // Mount routing and places endpoints app.use('/api/routes', routingRoutes); app.use('/api/places', placesRoutes); // Central error handler app.use((err, req, res, next) => { console.error('[Relay] Unhandled error:', err); res.status(500).json({ error: 'internalError', message: 'An internal server error occurred' }); }); if (require.main === module) { app.listen(PORT, '0.0.0.0', () => { console.log(`====================================================`); console.log(` 🏍️ MotorSignal API & Waze Alert Relay Server running`); console.log(` πŸ“‘ Listening on: http://0.0.0.0:${PORT}`); console.log(` πŸ₯ Healthcheck: http://localhost:${PORT}/health`); console.log(` 🚨 Alerts API: http://localhost:${PORT}/alerts?lat=37.77&lng=-122.42`); console.log(` πŸ—ΊοΈ Routing API: http://localhost:${PORT}/api/routes/calculate`); console.log(` πŸ“ Places API: http://localhost:${PORT}/api/places/search`); console.log(`====================================================`); }); } module.exports = app;