2025-12-11 22:31:59 +00:00

72 lines
2.4 KiB
TypeScript

/**
* Express server entry point.
*/
import express from 'express';
import cors from 'cors';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { config } from 'dotenv';
import { apiRouter } from './api/index.js';
// Load environment variables from orchestrator root
const __dirname = dirname(fileURLToPath(import.meta.url));
config({ path: join(__dirname, '../../.env') });
const app = express();
const PORT = process.env.PORT || 3001;
// Middleware
app.use(cors());
app.use(express.json());
// Logging middleware
app.use((req, res, next) => {
const start = Date.now();
res.on('finish', () => {
const duration = Date.now() - start;
console.log(`${req.method} ${req.path} - ${res.statusCode} (${duration}ms)`);
});
next();
});
// API routes
app.use('/api', apiRouter);
// Serve static files for generated PDFs
const pdfDir = join(__dirname, '../../data/pdfs');
app.use('/pdfs', express.static(pdfDir));
// Health check
app.get('/health', (req, res) => {
res.json({ status: 'ok', timestamp: new Date().toISOString() });
});
// Serve client app in production
if (process.env.NODE_ENV === 'production') {
const clientDir = join(__dirname, '../../dist/client');
app.use(express.static(clientDir));
// SPA fallback
app.get('*', (req, res) => {
res.sendFile(join(clientDir, 'index.html'));
});
}
// Start server
app.listen(PORT, () => {
console.log(`
╔═══════════════════════════════════════════════════════════╗
║ ║
║ 🚀 Job Ops Orchestrator ║
║ ║
║ Server running at: http://localhost:${PORT}
║ ║
║ API: http://localhost:${PORT}/api ║
║ Health: http://localhost:${PORT}/health ║
║ PDFs: http://localhost:${PORT}/pdfs ║
║ ║
╚═══════════════════════════════════════════════════════════╝
`);
});