Modernization Project Complete. Updated to latest versions of frameworks.
This commit is contained in:
@@ -1,48 +1,86 @@
|
||||
/**
|
||||
* @ai-summary Express app configuration with feature registration
|
||||
* @ai-summary Fastify app configuration with feature registration
|
||||
* @ai-context Each feature capsule registers its routes independently
|
||||
*/
|
||||
import express from 'express';
|
||||
import cors from 'cors';
|
||||
import helmet from 'helmet';
|
||||
import { errorHandler } from './core/middleware/error.middleware';
|
||||
import { requestLogger } from './core/middleware/logging.middleware';
|
||||
import Fastify, { FastifyInstance } from 'fastify';
|
||||
import cors from '@fastify/cors';
|
||||
import helmet from '@fastify/helmet';
|
||||
|
||||
export const app = express();
|
||||
// Core plugins
|
||||
import authPlugin from './core/plugins/auth.plugin';
|
||||
import loggingPlugin from './core/plugins/logging.plugin';
|
||||
import errorPlugin from './core/plugins/error.plugin';
|
||||
|
||||
// Core middleware
|
||||
app.use(helmet());
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
app.use(requestLogger);
|
||||
// Fastify feature routes
|
||||
import { vehiclesRoutes } from './features/vehicles/api/vehicles.routes';
|
||||
import { fuelLogsRoutes } from './features/fuel-logs/api/fuel-logs.routes';
|
||||
import { stationsRoutes } from './features/stations/api/stations.routes';
|
||||
|
||||
// Health check
|
||||
app.get('/health', (_req, res) => {
|
||||
res.json({
|
||||
status: 'healthy',
|
||||
timestamp: new Date().toISOString(),
|
||||
environment: process.env.NODE_ENV,
|
||||
features: ['vehicles', 'fuel-logs', 'stations', 'maintenance']
|
||||
async function buildApp(): Promise<FastifyInstance> {
|
||||
const app = Fastify({
|
||||
logger: false, // Use custom logging plugin instead
|
||||
});
|
||||
});
|
||||
|
||||
// Import all feature route registrations
|
||||
import { registerVehiclesRoutes } from './features/vehicles';
|
||||
import { registerFuelLogsRoutes } from './features/fuel-logs';
|
||||
import { registerStationsRoutes } from './features/stations';
|
||||
// Core middleware plugins
|
||||
await app.register(helmet);
|
||||
await app.register(cors);
|
||||
await app.register(loggingPlugin);
|
||||
await app.register(errorPlugin);
|
||||
|
||||
// Authentication plugin
|
||||
await app.register(authPlugin);
|
||||
|
||||
// Register all feature routes
|
||||
app.use(registerVehiclesRoutes());
|
||||
app.use(registerFuelLogsRoutes());
|
||||
app.use(registerStationsRoutes());
|
||||
// Health check
|
||||
app.get('/health', async (_request, reply) => {
|
||||
return reply.code(200).send({
|
||||
status: 'healthy',
|
||||
timestamp: new Date().toISOString(),
|
||||
environment: process.env.NODE_ENV,
|
||||
features: ['vehicles', 'fuel-logs', 'stations', 'maintenance']
|
||||
});
|
||||
});
|
||||
|
||||
// 404 handler
|
||||
app.use((_req, res) => {
|
||||
res.status(404).json({ error: 'Route not found' });
|
||||
});
|
||||
// Register Fastify feature routes
|
||||
await app.register(vehiclesRoutes, { prefix: '/api' });
|
||||
await app.register(fuelLogsRoutes, { prefix: '/api' });
|
||||
await app.register(stationsRoutes, { prefix: '/api' });
|
||||
|
||||
// Error handling (must be last)
|
||||
app.use(errorHandler);
|
||||
// Maintenance feature placeholder (not yet implemented)
|
||||
await app.register(async (fastify) => {
|
||||
// Maintenance routes - basic placeholder for future implementation
|
||||
fastify.get('/api/maintenance*', async (_request, reply) => {
|
||||
return reply.code(501).send({
|
||||
error: 'Not Implemented',
|
||||
message: 'Maintenance feature not yet implemented'
|
||||
});
|
||||
});
|
||||
|
||||
export default app;
|
||||
fastify.post('/api/maintenance*', async (_request, reply) => {
|
||||
return reply.code(501).send({
|
||||
error: 'Not Implemented',
|
||||
message: 'Maintenance feature not yet implemented'
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// 404 handler
|
||||
app.setNotFoundHandler(async (_request, reply) => {
|
||||
return reply.code(404).send({ error: 'Route not found' });
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
export { buildApp };
|
||||
|
||||
// For compatibility with existing server.ts
|
||||
let appInstance: FastifyInstance | null = null;
|
||||
|
||||
export async function getApp(): Promise<FastifyInstance> {
|
||||
if (!appInstance) {
|
||||
appInstance = await buildApp();
|
||||
}
|
||||
return appInstance;
|
||||
}
|
||||
|
||||
export default buildApp;
|
||||
Reference in New Issue
Block a user