removed configuration from index files and moved to di container
This commit is contained in:
parent
a3459f5865
commit
80c1dcb6cb
6 changed files with 261 additions and 94 deletions
|
|
@ -14,6 +14,7 @@ import { PostgreSQLClient } from '@stock-bot/postgres';
|
|||
import { ProxyManager } from '@stock-bot/proxy';
|
||||
import { QuestDBClient } from '@stock-bot/questdb';
|
||||
import { type QueueManager } from '@stock-bot/queue';
|
||||
import type { AppConfig as StockBotAppConfig } from '@stock-bot/config';
|
||||
|
||||
// Configuration schema with validation
|
||||
const appConfigSchema = z.object({
|
||||
|
|
@ -299,3 +300,85 @@ export async function initializeServices(container: AwilixContainer): Promise<vo
|
|||
// Export typed container
|
||||
export type ServiceContainer = AwilixContainer<ServiceDefinitions>;
|
||||
export type ServiceCradle = ServiceDefinitions;
|
||||
|
||||
/**
|
||||
* Service-specific options for container creation
|
||||
*/
|
||||
export interface ServiceContainerOptions {
|
||||
enableQuestDB?: boolean;
|
||||
enableMongoDB?: boolean;
|
||||
enablePostgres?: boolean;
|
||||
enableCache?: boolean;
|
||||
enableQueue?: boolean;
|
||||
enableBrowser?: boolean;
|
||||
enableProxy?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create service container directly from AppConfig
|
||||
* This eliminates the need for manual config mapping in each service
|
||||
*/
|
||||
export function createServiceContainerFromConfig(
|
||||
appConfig: StockBotAppConfig,
|
||||
options: ServiceContainerOptions = {}
|
||||
): AwilixContainer<ServiceDefinitions> {
|
||||
// Apply defaults for options
|
||||
const {
|
||||
enableQuestDB = true,
|
||||
enableMongoDB = true,
|
||||
enablePostgres = true,
|
||||
enableCache = true,
|
||||
enableQueue = true,
|
||||
enableBrowser = true,
|
||||
enableProxy = true,
|
||||
} = options;
|
||||
|
||||
// Build the config object expected by createServiceContainer
|
||||
const containerConfig = {
|
||||
redis: {
|
||||
enabled: enableCache && appConfig.database?.dragonfly ? true : false,
|
||||
host: appConfig.database?.dragonfly?.host || 'localhost',
|
||||
port: appConfig.database?.dragonfly?.port || 6379,
|
||||
password: appConfig.database?.dragonfly?.password,
|
||||
db: appConfig.database?.dragonfly?.db || 0,
|
||||
},
|
||||
mongodb: {
|
||||
enabled: enableMongoDB && appConfig.database?.mongodb ? true : false,
|
||||
uri: appConfig.database?.mongodb?.uri ||
|
||||
`mongodb://${appConfig.database?.mongodb?.user || ''}:${appConfig.database?.mongodb?.password || ''}@${appConfig.database?.mongodb?.host || 'localhost'}:${appConfig.database?.mongodb?.port || 27017}/${appConfig.database?.mongodb?.database || 'test'}?authSource=${appConfig.database?.mongodb?.authSource || 'admin'}`,
|
||||
database: appConfig.database?.mongodb?.database || 'test',
|
||||
},
|
||||
postgres: {
|
||||
enabled: enablePostgres && appConfig.database?.postgres ? true : false,
|
||||
host: appConfig.database?.postgres?.host || 'localhost',
|
||||
port: appConfig.database?.postgres?.port || 5432,
|
||||
database: appConfig.database?.postgres?.database || 'test',
|
||||
user: appConfig.database?.postgres?.user || 'test',
|
||||
password: appConfig.database?.postgres?.password || 'test',
|
||||
},
|
||||
questdb: enableQuestDB && appConfig.database?.questdb ? {
|
||||
enabled: true,
|
||||
host: appConfig.database.questdb.host || 'localhost',
|
||||
httpPort: appConfig.database.questdb.httpPort || 9000,
|
||||
pgPort: appConfig.database.questdb.pgPort || 8812,
|
||||
influxPort: appConfig.database.questdb.ilpPort || 9009,
|
||||
database: appConfig.database.questdb.database || 'questdb',
|
||||
} : {
|
||||
enabled: false,
|
||||
host: 'localhost',
|
||||
httpPort: 9000,
|
||||
pgPort: 8812,
|
||||
influxPort: 9009,
|
||||
},
|
||||
proxy: enableProxy ? {
|
||||
cachePrefix: 'proxy:',
|
||||
ttl: 3600,
|
||||
} : undefined,
|
||||
browser: enableBrowser ? {
|
||||
headless: true,
|
||||
timeout: 30000,
|
||||
} : undefined,
|
||||
};
|
||||
|
||||
return createServiceContainer(containerConfig);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,8 +6,10 @@ export * from './types';
|
|||
// Awilix container exports
|
||||
export {
|
||||
createServiceContainer,
|
||||
createServiceContainerFromConfig,
|
||||
initializeServices,
|
||||
type AppConfig,
|
||||
type ServiceCradle,
|
||||
type ServiceContainer,
|
||||
type ServiceContainerOptions,
|
||||
} from './awilix-container';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue