/** * Backend API: Layered architecture for scalability and maintainability. * * ## Architecture Layers * * ### Controllers (routes/) * - Handle HTTP requests and responses * - Validate input and format output * - Call services for business logic * * ### Services (services/) * - Implement business logic * - Coordinate between controllers and repositories * - Handle validation and transformation * * ### Repositories (repositories/) * - Data access layer * - Interact with databases or external APIs * - Return domain models * * ### Models (models/) * - Data structures and types * - Validation schemas * * ### Middleware (middleware/) * - Request validation * - Authentication/authorization * - Rate limiting * - Error handling * * ## Adding New Endpoints * * 1. Define the model in `models/` * 2. Implement repository logic in `repositories/` * 3. Implement service logic in `services/` * 4. Create controller in `routes/` * 5. Register route in `index.ts` */ import express from 'express' import cors from 'cors' // --------------------------------------------------------------------------- // Configuration // --------------------------------------------------------------------------- const PORT = Number(process.env.PORT) || 8080 const CORS_ORIGIN = process.env.CORS_ORIGIN || 'http://localhost:3000' // --------------------------------------------------------------------------- // Express App Setup // --------------------------------------------------------------------------- const app = express() app.use(cors({ origin: CORS_ORIGIN })) app.use(express.json()) // --------------------------------------------------------------------------- // Agent Routes // --------------------------------------------------------------------------- import { handleAgentRequest, handleAgentStreamRequest } from './routes/agentRoutes.js' /** POST /api/agent - Run AI agent */ app.post('/api/agent', handleAgentRequest) /** POST /api/agent/stream - Stream AI agent response */ app.post('/api/agent/stream', handleAgentStreamRequest) // --------------------------------------------------------------------------- // Health Check Endpoint // --------------------------------------------------------------------------- /** GET /health - Health check for Docker / orchestration */ app.get('/health', (req, res) => { res.status(200).json({ ok: true, timestamp: Date.now() }) }) // --------------------------------------------------------------------------- // Error Handling Middleware // --------------------------------------------------------------------------- /** Global error handler for consistent error responses */ app.use((err: Error, req: express.Request, res: express.Response, next: express.NextFunction) => { console.error('Error:', err) res.status(500).json({ error: err.message ?? 'Internal server error' }) }) // --------------------------------------------------------------------------- // Start Server // --------------------------------------------------------------------------- app.listen(PORT, '0.0.0.0', () => { console.log(`Backend listening on port ${PORT} (CORS: ${CORS_ORIGIN})`) }) // --------------------------------------------------------------------------- // Export for testing // --------------------------------------------------------------------------- export default app