unified config
This commit is contained in:
parent
e7c0fe2798
commit
3877902ff4
13 changed files with 856 additions and 476 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import { createContainer, InjectionMode, asFunction, type AwilixContainer } from 'awilix';
|
||||
import type { AppConfig as StockBotAppConfig } from '@stock-bot/config';
|
||||
import type { AppConfig as StockBotAppConfig, UnifiedAppConfig } from '@stock-bot/config';
|
||||
import { appConfigSchema, type AppConfig } from '../config/schemas';
|
||||
import { toUnifiedConfig } from '@stock-bot/config';
|
||||
import {
|
||||
registerCoreServices,
|
||||
registerCacheServices,
|
||||
|
|
@ -12,6 +13,7 @@ import type { ServiceDefinitions, ContainerBuildOptions } from './types';
|
|||
|
||||
export class ServiceContainerBuilder {
|
||||
private config: Partial<AppConfig> = {};
|
||||
private unifiedConfig: UnifiedAppConfig | null = null;
|
||||
private options: ContainerBuildOptions = {
|
||||
enableCache: true,
|
||||
enableQueue: true,
|
||||
|
|
@ -24,8 +26,10 @@ export class ServiceContainerBuilder {
|
|||
initializationTimeout: 30000,
|
||||
};
|
||||
|
||||
withConfig(config: AppConfig | StockBotAppConfig): this {
|
||||
this.config = this.transformStockBotConfig(config);
|
||||
withConfig(config: AppConfig | StockBotAppConfig | UnifiedAppConfig): this {
|
||||
// Convert to unified config format
|
||||
this.unifiedConfig = toUnifiedConfig(config);
|
||||
this.config = this.transformStockBotConfig(this.unifiedConfig);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
@ -72,6 +76,19 @@ export class ServiceContainerBuilder {
|
|||
}
|
||||
|
||||
private applyServiceOptions(config: Partial<AppConfig>): AppConfig {
|
||||
// Ensure questdb config has the right field names for DI
|
||||
const questdbConfig = config.questdb ? {
|
||||
...config.questdb,
|
||||
influxPort: (config.questdb as any).influxPort || (config.questdb as any).ilpPort || 9009,
|
||||
} : {
|
||||
enabled: true,
|
||||
host: 'localhost',
|
||||
httpPort: 9000,
|
||||
pgPort: 8812,
|
||||
influxPort: 9009,
|
||||
database: 'questdb',
|
||||
};
|
||||
|
||||
return {
|
||||
redis: config.redis || {
|
||||
enabled: this.options.enableCache ?? true,
|
||||
|
|
@ -92,14 +109,7 @@ export class ServiceContainerBuilder {
|
|||
user: 'postgres',
|
||||
password: 'postgres',
|
||||
},
|
||||
questdb: this.options.enableQuestDB ? (config.questdb || {
|
||||
enabled: true,
|
||||
host: 'localhost',
|
||||
httpPort: 9000,
|
||||
pgPort: 8812,
|
||||
influxPort: 9009,
|
||||
database: 'questdb',
|
||||
}) : undefined,
|
||||
questdb: this.options.enableQuestDB ? questdbConfig : undefined,
|
||||
proxy: this.options.enableProxy ? (config.proxy || { enabled: false, cachePrefix: 'proxy:', ttl: 3600 }) : undefined,
|
||||
browser: this.options.enableBrowser ? (config.browser || { headless: true, timeout: 30000 }) : undefined,
|
||||
queue: this.options.enableQueue ? (config.queue || {
|
||||
|
|
@ -115,6 +125,7 @@ export class ServiceContainerBuilder {
|
|||
removeOnFail: 50,
|
||||
}
|
||||
}) : undefined,
|
||||
service: config.service,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -143,53 +154,27 @@ export class ServiceContainerBuilder {
|
|||
});
|
||||
}
|
||||
|
||||
private transformStockBotConfig(config: AppConfig | StockBotAppConfig): Partial<AppConfig> {
|
||||
// If it's already in the new format (has redis AND postgres at top level), return as is
|
||||
if ('redis' in config && 'postgres' in config && 'mongodb' in config) {
|
||||
return config as AppConfig;
|
||||
}
|
||||
private transformStockBotConfig(config: UnifiedAppConfig): Partial<AppConfig> {
|
||||
// Unified config already has flat structure, just extract what we need
|
||||
// Handle questdb field name mapping
|
||||
const questdb = config.questdb ? {
|
||||
enabled: config.questdb.enabled || true,
|
||||
host: config.questdb.host || 'localhost',
|
||||
httpPort: config.questdb.httpPort || 9000,
|
||||
pgPort: config.questdb.pgPort || 8812,
|
||||
influxPort: (config.questdb as any).influxPort || (config.questdb as any).ilpPort || 9009,
|
||||
database: config.questdb.database || 'questdb',
|
||||
} : undefined;
|
||||
|
||||
// Transform from StockBotAppConfig format
|
||||
const stockBotConfig = config as StockBotAppConfig;
|
||||
return {
|
||||
redis: stockBotConfig.database?.dragonfly ? {
|
||||
enabled: true,
|
||||
host: stockBotConfig.database.dragonfly.host || 'localhost',
|
||||
port: stockBotConfig.database.dragonfly.port || 6379,
|
||||
password: stockBotConfig.database.dragonfly.password,
|
||||
db: stockBotConfig.database.dragonfly.db || 0,
|
||||
} : undefined,
|
||||
mongodb: stockBotConfig.database?.mongodb ? {
|
||||
enabled: stockBotConfig.database.mongodb.enabled ?? true,
|
||||
uri: stockBotConfig.database.mongodb.uri,
|
||||
database: stockBotConfig.database.mongodb.database,
|
||||
} : undefined,
|
||||
postgres: stockBotConfig.database?.postgres ? {
|
||||
enabled: stockBotConfig.database.postgres.enabled ?? true,
|
||||
host: stockBotConfig.database.postgres.host,
|
||||
port: stockBotConfig.database.postgres.port,
|
||||
database: stockBotConfig.database.postgres.database,
|
||||
user: stockBotConfig.database.postgres.user,
|
||||
password: stockBotConfig.database.postgres.password,
|
||||
} : undefined,
|
||||
questdb: stockBotConfig.database?.questdb ? {
|
||||
enabled: true,
|
||||
host: stockBotConfig.database.questdb.host || 'localhost',
|
||||
httpPort: stockBotConfig.database.questdb.httpPort || 9000,
|
||||
pgPort: stockBotConfig.database.questdb.pgPort || 8812,
|
||||
influxPort: stockBotConfig.database.questdb.ilpPort || 9009,
|
||||
database: stockBotConfig.database.questdb.database || 'questdb',
|
||||
} : undefined,
|
||||
queue: stockBotConfig.queue,
|
||||
browser: stockBotConfig.browser,
|
||||
proxy: stockBotConfig.proxy ? {
|
||||
...{
|
||||
enabled: false,
|
||||
cachePrefix: 'proxy:',
|
||||
ttl: 3600,
|
||||
},
|
||||
...stockBotConfig.proxy
|
||||
} : undefined,
|
||||
redis: config.redis,
|
||||
mongodb: config.mongodb,
|
||||
postgres: config.postgres,
|
||||
questdb,
|
||||
queue: config.queue,
|
||||
browser: config.browser,
|
||||
proxy: config.proxy,
|
||||
service: config.service,
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue