/** * REST API Server * Provides RESTful API for Brazil SWIFT Operations Platform */ import express, { Express, Request, Response, NextFunction } from 'express'; import cors from 'cors'; import { getLogger } from '@brazil-swift-ops/utils'; import { evaluateTransaction } from '@brazil-swift-ops/rules-engine'; import type { Transaction } from '@brazil-swift-ops/types'; const app: Express = express(); const logger = getLogger(); const PORT = process.env.PORT || 3000; // Middleware app.use(cors()); app.use(express.json()); app.use((req: Request, res: Response, next: NextFunction) => { const correlationId = `req-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`; logger.setCorrelationId(correlationId); req.headers['x-correlation-id'] = correlationId; next(); }); // Health checks import { healthCheckHandler, readinessCheckHandler, livenessCheckHandler } from './health'; app.get('/health', healthCheckHandler); app.get('/health/ready', readinessCheckHandler); app.get('/health/live', livenessCheckHandler); // Evaluate transaction app.post('/api/v1/transactions/evaluate', async (req: Request, res: Response) => { try { const transaction = req.body as Transaction; const result = evaluateTransaction(transaction); logger.info('Transaction evaluated', { transactionId: transaction.id }); res.json({ success: true, data: result, }); } catch (error) { logger.error('Error evaluating transaction', error as Error); res.status(500).json({ success: false, error: 'Failed to evaluate transaction', }); } }); // Get transaction by ID app.get('/api/v1/transactions/:id', (req: Request, res: Response) => { // TODO: Implement database lookup res.status(501).json({ success: false, error: 'Not implemented - database persistence required', }); }); // List transactions app.get('/api/v1/transactions', (req: Request, res: Response) => { // TODO: Implement database lookup with pagination res.status(501).json({ success: false, error: 'Not implemented - database persistence required', }); }); // Error handler app.use((err: Error, req: Request, res: Response, next: NextFunction) => { logger.error('Unhandled error', err); res.status(500).json({ success: false, error: 'Internal server error', }); }); // Start server (only if running directly, not when imported) if (typeof require !== 'undefined' && require.main === module) { app.listen(PORT, () => { logger.info(`API server started on port ${PORT}`); }); } export default app;