updated di

This commit is contained in:
Boki 2025-06-21 20:07:43 -04:00
parent 3227388d25
commit c5a114d544
9 changed files with 545 additions and 306 deletions

View file

@ -1,27 +1,68 @@
/**
* Routes creation with improved DI pattern
*/
import { Hono } from 'hono';
import type { ServiceContainer } from '@stock-bot/di';
import type { IDataIngestionServices } from '@stock-bot/di';
import { exchangeRoutes } from './exchange.routes';
import { healthRoutes } from './health.routes';
import { queueRoutes } from './queue.routes';
/**
* Creates all routes with access to the service container
* Creates all routes with access to type-safe services
*/
export function createRoutes(container: ServiceContainer): Hono {
export function createRoutes(services: IDataIngestionServices): Hono {
const app = new Hono();
// Mount routes that don't need container
// Mount routes that don't need services
app.route('/health', healthRoutes);
// TODO: Update these routes to use container when needed
// Mount routes that need services (will be updated to use services)
app.route('/api/exchanges', exchangeRoutes);
app.route('/api/queue', queueRoutes);
// Store container in app context for handlers that need it
// Store services in app context for handlers that need it
app.use('*', async (c, next) => {
c.set('container', container);
c.set('services', services);
await next();
});
// Add a new endpoint to test the improved DI
app.get('/api/di-test', async (c) => {
try {
const services = c.get('services') as IDataIngestionServices;
// Test MongoDB connection
const mongoStats = services.mongodb.getPoolMetrics?.() || { status: 'connected' };
// Test PostgreSQL connection
const pgConnected = services.postgres.connected;
// Test cache
const cacheReady = services.cache.isReady();
// Test queue
const queueStats = services.queue.getGlobalStats();
return c.json({
success: true,
message: 'Improved DI pattern is working!',
services: {
mongodb: mongoStats,
postgres: { connected: pgConnected },
cache: { ready: cacheReady },
queue: queueStats
},
timestamp: new Date().toISOString()
});
} catch (error) {
services.logger.error('DI test endpoint failed', { error });
return c.json({
success: false,
error: error instanceof Error ? error.message : String(error)
}, 500);
}
});
return app;
}