refactoring continuing

This commit is contained in:
Boki 2025-06-22 08:27:54 -04:00
parent 742e590382
commit a0a3b26177
20 changed files with 394 additions and 798 deletions

View file

@ -10,14 +10,17 @@ import { cors } from 'hono/cors';
// Library imports
import {
createServiceContainer,
initializeServices as initializeAwilixServices,
createServiceAdapter,
createDataIngestionServices,
disposeDataIngestionServices,
type IDataIngestionServices
type IDataIngestionServices,
type ServiceContainer
} from '@stock-bot/di';
import { getLogger, setLoggerConfig, shutdownLoggers } from '@stock-bot/logger';
import { Shutdown } from '@stock-bot/shutdown';
import { handlerRegistry } from '@stock-bot/types';
import { ProxyManager } from '@stock-bot/utils';
// Local imports
import { createRoutes } from './routes/create-routes';
@ -43,6 +46,7 @@ const logger = getLogger('data-ingestion');
const PORT = serviceConfig.port;
let server: ReturnType<typeof Bun.serve> | null = null;
let services: IDataIngestionServices | null = null;
let container: ServiceContainer | null = null;
let app: Hono | null = null;
// Initialize shutdown manager
@ -53,7 +57,36 @@ async function initializeServices() {
logger.info('Initializing data-ingestion service with improved DI...');
try {
// Create all services using the service factory
// Create Awilix container with proper config structure
logger.debug('Creating Awilix DI container...');
const awilixConfig = {
redis: {
host: config.database.dragonfly.host,
port: config.database.dragonfly.port,
db: config.database.dragonfly.db,
},
mongodb: {
uri: config.database.mongodb.uri,
database: config.database.mongodb.database,
},
postgres: {
host: config.database.postgres.host,
port: config.database.postgres.port,
database: config.database.postgres.database,
user: config.database.postgres.user,
password: config.database.postgres.password,
},
proxy: {
cachePrefix: 'proxy:',
ttl: 3600,
},
};
container = createServiceContainer(awilixConfig);
await initializeAwilixServices(container);
logger.info('Awilix container created and initialized');
// Create all services using the service factory (for backward compatibility)
logger.debug('Creating services using service factory...');
services = await createDataIngestionServices(config);
logger.info('All services created successfully');
@ -76,16 +109,20 @@ async function initializeServices() {
const routes = createRoutes(services);
app.route('/', routes);
// Initialize proxy manager
logger.debug('Initializing proxy manager...');
await ProxyManager.initialize();
logger.info('Proxy manager initialized');
// Initialize handlers with new DI pattern
logger.debug('Initializing data handlers with new DI pattern...');
// Initialize handlers with Awilix service container
logger.debug('Initializing data handlers with Awilix DI pattern...');
// Auto-register all handlers
await initializeAllHandlers(services);
// Create service adapter that includes proxy from Awilix container
const serviceContainerWithProxy = createServiceAdapter(services);
// Override the proxy service with the one from Awilix
Object.defineProperty(serviceContainerWithProxy, 'proxy', {
get: () => container!.resolve('proxyManager'),
enumerable: true,
configurable: true
});
// Auto-register all handlers with the enhanced service container
await initializeAllHandlers(serviceContainerWithProxy);
logger.info('Data handlers initialized with new DI pattern');