refactored db's and browser

This commit is contained in:
Boki 2025-06-22 08:45:14 -04:00
parent a0a3b26177
commit 89cbfb7848
12 changed files with 111 additions and 39 deletions

View file

@ -7,6 +7,10 @@ import { createContainer, asFunction, asValue, InjectionMode, type AwilixContain
import { createCache, type CacheProvider } from '@stock-bot/cache';
import { ProxyManager } from '@stock-bot/proxy';
import { getLogger } from '@stock-bot/logger';
import { createMongoDBClient } from '@stock-bot/mongodb';
import { createPostgreSQLClient } from '@stock-bot/postgres';
import { createQuestDBClient } from '@stock-bot/questdb';
import { Browser } from '@stock-bot/browser';
import type { IServiceContainer } from '@stock-bot/handlers';
// Configuration types
@ -37,6 +41,10 @@ export interface AppConfig {
cachePrefix?: string;
ttl?: number;
};
browser?: {
headless?: boolean;
timeout?: number;
};
}
/**
@ -87,20 +95,45 @@ export function createServiceContainer(config: AppConfig): AwilixContainer {
return null;
}).singleton(),
// Database clients - placeholders for now
mongoClient: asFunction(() => {
// TODO: Create MongoDB client
return null;
// MongoDB client with injected logger
mongoClient: asFunction(({ mongoConfig, logger }) => {
// Parse MongoDB URI to extract host and port
const url = new URL(mongoConfig.uri);
return createMongoDBClient(
{
uri: mongoConfig.uri,
host: url.hostname,
port: parseInt(url.port || '27017'),
database: mongoConfig.database,
},
logger
);
}).singleton(),
postgresClient: asFunction(() => {
// TODO: Create PostgreSQL client
return null;
postgresClient: asFunction(({ postgresConfig, logger }) => {
return createPostgreSQLClient(
{
host: postgresConfig.host,
port: postgresConfig.port,
database: postgresConfig.database,
username: postgresConfig.user,
password: postgresConfig.password,
},
logger
);
}).singleton(),
questdbClient: asFunction(() => {
// TODO: Create QuestDB client
return null;
questdbClient: asFunction(({ questdbConfig, logger }) => {
return createQuestDBClient(
{
host: questdbConfig.host,
httpPort: 9000,
pgPort: questdbConfig.port || 8812,
influxPort: 9009,
database: 'questdb',
},
logger
);
}).singleton(),
// Queue manager - placeholder
@ -109,12 +142,18 @@ export function createServiceContainer(config: AppConfig): AwilixContainer {
return null;
}).singleton(),
// Browser automation
browser: asFunction(({ config, logger }) => {
return new Browser(logger, config.browser);
}).singleton(),
// Build the IServiceContainer for handlers
serviceContainer: asFunction((cradle) => ({
logger: cradle.logger,
cache: cradle.cache,
proxy: cradle.proxyManager,
http: cradle.httpClient,
browser: cradle.browser,
mongodb: cradle.mongoClient,
postgres: cradle.postgresClient,
questdb: cradle.questdbClient,
@ -146,8 +185,31 @@ export async function initializeServices(container: AwilixContainer): Promise<vo
logger.info('Proxy manager initialized');
}
// Initialize other async services as needed
// ...
// Connect database clients
const mongoClient = container.resolve('mongoClient');
if (mongoClient && typeof mongoClient.connect === 'function') {
await mongoClient.connect();
logger.info('MongoDB connected');
}
const postgresClient = container.resolve('postgresClient');
if (postgresClient && typeof postgresClient.connect === 'function') {
await postgresClient.connect();
logger.info('PostgreSQL connected');
}
const questdbClient = container.resolve('questdbClient');
if (questdbClient && typeof questdbClient.connect === 'function') {
await questdbClient.connect();
logger.info('QuestDB connected');
}
// Initialize browser if configured
const browser = container.resolve('browser');
if (browser && typeof browser.initialize === 'function') {
await browser.initialize();
logger.info('Browser initialized');
}
logger.info('All services initialized successfully');
} catch (error) {
@ -163,6 +225,7 @@ export interface ServiceCradle {
cache: CacheProvider;
proxyManager: ProxyManager;
httpClient: any;
browser: any;
mongoClient: any;
postgresClient: any;
questdbClient: any;

View file

@ -14,6 +14,7 @@ export abstract class BaseHandler implements IHandler {
readonly queue;
readonly http;
readonly proxy;
readonly browser;
readonly mongodb;
readonly postgres;
readonly questdb;
@ -27,6 +28,7 @@ export abstract class BaseHandler implements IHandler {
this.queue = services.queue;
this.http = services.http;
this.proxy = services.proxy;
this.browser = services.browser;
this.mongodb = services.mongodb;
this.postgres = services.postgres;
this.questdb = services.questdb;

View file

@ -16,6 +16,7 @@ export interface IServiceContainer {
readonly queue: any; // Queue manager (BullMQ)
readonly http: any; // HTTP client with proxy support
readonly proxy: ProxyManager; // Proxy manager service
readonly browser?: any; // Browser automation (Playwright)
// Database clients
readonly mongodb: any; // MongoDB client