added disabled functioality
This commit is contained in:
parent
fabf42dc7f
commit
d8ae0cb3c5
16 changed files with 147 additions and 75 deletions
|
|
@ -16,6 +16,7 @@ import { asFunction, asValue, createContainer, InjectionMode, type AwilixContain
|
|||
// Configuration types
|
||||
export interface AppConfig {
|
||||
redis: {
|
||||
enabled?: boolean;
|
||||
host: string;
|
||||
port: number;
|
||||
password?: string;
|
||||
|
|
@ -23,10 +24,12 @@ export interface AppConfig {
|
|||
db?: number;
|
||||
};
|
||||
mongodb: {
|
||||
enabled?: boolean;
|
||||
uri: string;
|
||||
database: string;
|
||||
};
|
||||
postgres: {
|
||||
enabled?: boolean;
|
||||
host: string;
|
||||
port: number;
|
||||
database: string;
|
||||
|
|
@ -34,6 +37,7 @@ export interface AppConfig {
|
|||
password: string;
|
||||
};
|
||||
questdb?: {
|
||||
enabled?: boolean;
|
||||
host: string;
|
||||
httpPort?: number;
|
||||
pgPort?: number;
|
||||
|
|
@ -59,7 +63,7 @@ export function createServiceContainer(config: AppConfig): AwilixContainer {
|
|||
});
|
||||
|
||||
// Register configuration values
|
||||
container.register({
|
||||
const registrations: any = {
|
||||
// Configuration
|
||||
config: asValue(config),
|
||||
redisConfig: asValue(config.redis),
|
||||
|
|
@ -69,9 +73,11 @@ export function createServiceContainer(config: AppConfig): AwilixContainer {
|
|||
|
||||
// Core services with dependency injection
|
||||
logger: asFunction(() => getLogger('app')).singleton(),
|
||||
|
||||
// Cache with injected config and logger
|
||||
cache: asFunction(({ redisConfig, logger }) =>
|
||||
};
|
||||
|
||||
// Conditionally register cache/dragonfly
|
||||
if (config.redis?.enabled !== false) {
|
||||
registrations.cache = asFunction(({ redisConfig, logger }) =>
|
||||
createCache({
|
||||
redisConfig,
|
||||
logger,
|
||||
|
|
@ -79,23 +85,37 @@ export function createServiceContainer(config: AppConfig): AwilixContainer {
|
|||
ttl: 3600,
|
||||
enableMetrics: true,
|
||||
})
|
||||
).singleton(),
|
||||
|
||||
// Proxy manager with injected cache and logger
|
||||
proxyManager: asFunction(({ cache, config, logger }) => {
|
||||
const manager = new ProxyManager(
|
||||
cache,
|
||||
config.proxy || {},
|
||||
logger
|
||||
);
|
||||
// Note: initialization happens in initializeServices function
|
||||
return manager;
|
||||
}).singleton(), // MongoDB client with injected dependencies
|
||||
mongoClient: asFunction(({ mongoConfig, logger }) => {
|
||||
).singleton();
|
||||
} else {
|
||||
registrations.cache = asValue(null);
|
||||
}
|
||||
|
||||
// Proxy manager depends on cache
|
||||
registrations.proxyManager = asFunction(({ cache, config, logger }) => {
|
||||
if (!cache) {
|
||||
logger.warn('Cache is disabled, ProxyManager will have limited functionality');
|
||||
return null;
|
||||
}
|
||||
const manager = new ProxyManager(
|
||||
cache,
|
||||
config.proxy || {},
|
||||
logger
|
||||
);
|
||||
return manager;
|
||||
}).singleton();
|
||||
|
||||
// Conditionally register MongoDB client
|
||||
if (config.mongodb?.enabled !== false) {
|
||||
registrations.mongoClient = asFunction(({ mongoConfig, logger }) => {
|
||||
return new MongoDBClient(mongoConfig, logger);
|
||||
}).singleton(),
|
||||
|
||||
postgresClient: asFunction(({ postgresConfig, logger }) => {
|
||||
}).singleton();
|
||||
} else {
|
||||
registrations.mongoClient = asValue(null);
|
||||
}
|
||||
|
||||
// Conditionally register PostgreSQL client
|
||||
if (config.postgres?.enabled !== false) {
|
||||
registrations.postgresClient = asFunction(({ postgresConfig, logger }) => {
|
||||
return createPostgreSQLClient(
|
||||
{
|
||||
host: postgresConfig.host,
|
||||
|
|
@ -106,9 +126,14 @@ export function createServiceContainer(config: AppConfig): AwilixContainer {
|
|||
},
|
||||
logger
|
||||
);
|
||||
}).singleton(),
|
||||
|
||||
questdbClient: asFunction(({ questdbConfig, logger }) => {
|
||||
}).singleton();
|
||||
} else {
|
||||
registrations.postgresClient = asValue(null);
|
||||
}
|
||||
|
||||
// Conditionally register QuestDB client
|
||||
if (config.questdb?.enabled !== false) {
|
||||
registrations.questdbClient = asFunction(({ questdbConfig, logger }) => {
|
||||
console.log('Creating QuestDB client with config:', questdbConfig);
|
||||
return createQuestDBClient(
|
||||
{
|
||||
|
|
@ -123,32 +148,35 @@ export function createServiceContainer(config: AppConfig): AwilixContainer {
|
|||
},
|
||||
logger
|
||||
);
|
||||
}).singleton(),
|
||||
|
||||
// Queue manager - placeholder
|
||||
queueManager: asFunction(() => {
|
||||
// TODO: Create queue manager when decoupled
|
||||
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,
|
||||
browser: cradle.browser,
|
||||
mongodb: cradle.mongoClient,
|
||||
postgres: cradle.postgresClient,
|
||||
questdb: cradle.questdbClient,
|
||||
queue: cradle.queueManager,
|
||||
} as IServiceContainer)).singleton(),
|
||||
});
|
||||
}).singleton();
|
||||
} else {
|
||||
registrations.questdbClient = asValue(null);
|
||||
}
|
||||
|
||||
// Queue manager - placeholder
|
||||
registrations.queueManager = asFunction(() => {
|
||||
// TODO: Create queue manager when decoupled
|
||||
return null;
|
||||
}).singleton();
|
||||
|
||||
// Browser automation
|
||||
registrations.browser = asFunction(({ config, logger }) => {
|
||||
return new Browser(logger, config.browser);
|
||||
}).singleton();
|
||||
|
||||
// Build the IServiceContainer for handlers
|
||||
registrations.serviceContainer = asFunction((cradle) => ({
|
||||
logger: cradle.logger,
|
||||
cache: cradle.cache,
|
||||
proxy: cradle.proxyManager,
|
||||
browser: cradle.browser,
|
||||
mongodb: cradle.mongoClient,
|
||||
postgres: cradle.postgresClient,
|
||||
questdb: cradle.questdbClient,
|
||||
queue: cradle.queueManager,
|
||||
} as IServiceContainer)).singleton();
|
||||
|
||||
container.register(registrations);
|
||||
return container;
|
||||
}
|
||||
|
||||
|
|
@ -157,40 +185,53 @@ export function createServiceContainer(config: AppConfig): AwilixContainer {
|
|||
*/
|
||||
export async function initializeServices(container: AwilixContainer): Promise<void> {
|
||||
const logger = container.resolve('logger');
|
||||
const config = container.resolve('config');
|
||||
|
||||
try {
|
||||
// Wait for cache to be ready first
|
||||
// Wait for cache to be ready first (if enabled)
|
||||
const cache = container.resolve('cache');
|
||||
if (cache && typeof cache.waitForReady === 'function') {
|
||||
await cache.waitForReady(10000);
|
||||
logger.info('Cache is ready');
|
||||
} else if (config.redis?.enabled === false) {
|
||||
logger.info('Cache is disabled');
|
||||
}
|
||||
|
||||
// Initialize proxy manager
|
||||
// Initialize proxy manager (depends on cache)
|
||||
const proxyManager = container.resolve('proxyManager');
|
||||
if (proxyManager && typeof proxyManager.initialize === 'function') {
|
||||
await proxyManager.initialize();
|
||||
logger.info('Proxy manager initialized');
|
||||
} else {
|
||||
logger.info('Proxy manager is disabled (requires cache)');
|
||||
}
|
||||
|
||||
// Connect database clients
|
||||
// Connect MongoDB client (if enabled)
|
||||
const mongoClient = container.resolve('mongoClient');
|
||||
if (mongoClient && typeof mongoClient.connect === 'function') {
|
||||
await mongoClient.connect();
|
||||
logger.info('MongoDB connected');
|
||||
} else if (config.mongodb?.enabled === false) {
|
||||
logger.info('MongoDB is disabled');
|
||||
}
|
||||
|
||||
// Connect PostgreSQL client (if enabled)
|
||||
const postgresClient = container.resolve('postgresClient');
|
||||
if (postgresClient && typeof postgresClient.connect === 'function') {
|
||||
await postgresClient.connect();
|
||||
logger.info('PostgreSQL connected');
|
||||
} else if (config.postgres?.enabled === false) {
|
||||
logger.info('PostgreSQL is disabled');
|
||||
}
|
||||
|
||||
// const questdbClient = container.resolve('questdbClient');
|
||||
// if (questdbClient && typeof questdbClient.connect === 'function') {
|
||||
// await questdbClient.connect();
|
||||
// logger.info('QuestDB connected');
|
||||
// }
|
||||
// Connect QuestDB client (if enabled)
|
||||
const questdbClient = container.resolve('questdbClient');
|
||||
if (questdbClient && typeof questdbClient.connect === 'function') {
|
||||
await questdbClient.connect();
|
||||
logger.info('QuestDB connected');
|
||||
} else if (config.questdb?.enabled === false) {
|
||||
logger.info('QuestDB is disabled');
|
||||
}
|
||||
|
||||
// Initialize browser if configured
|
||||
const browser = container.resolve('browser');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue