44 lines
2 KiB
JavaScript
Executable file
44 lines
2 KiB
JavaScript
Executable file
'use strict';
|
|
require('dotenv').config({ path: `${__dirname}/.env` });
|
|
|
|
const express = require('express');
|
|
const cookieParser = require('cookie-parser');
|
|
const path = require('path');
|
|
|
|
const authRoutes = require('./routes/auth');
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
// ── Middleware ────────────────────────────────────────────────────────────────
|
|
app.use(express.json());
|
|
app.use(cookieParser());
|
|
|
|
// Trust first proxy (Apache/Nginx) so rate-limiter uses real client IP
|
|
app.set('trust proxy', 1);
|
|
|
|
// ── API routes ────────────────────────────────────────────────────────────────
|
|
app.use('/api/auth', authRoutes);
|
|
|
|
// ── Health check ──────────────────────────────────────────────────────────────
|
|
app.get('/api/health', (_req, res) => res.json({ status: 'ok' }));
|
|
|
|
// ── Serve static front-end (disabled when Apache/Nginx handles static files) ──
|
|
if (process.env.SERVE_STATIC !== 'false') {
|
|
const staticDir = path.join(__dirname, '..');
|
|
app.use(express.static(staticDir, { index: 'index.html' }));
|
|
// SPA fallback for ?reset_token= and ?verify_token= query params
|
|
app.get('*', (_req, res) => {
|
|
res.sendFile(path.join(staticDir, 'index.html'));
|
|
});
|
|
}
|
|
|
|
// ── Global error handler ──────────────────────────────────────────────────────
|
|
app.use((err, _req, res, _next) => {
|
|
console.error('Unhandled error:', err);
|
|
res.status(500).json({ error: 'Internal server error.' });
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`SLA Calculator server running on port ${PORT} [${process.env.NODE_ENV || 'development'}]`);
|
|
});
|