removed configs from all libs and will inject them within the services themselves
This commit is contained in:
parent
fd28162811
commit
6cc5b339bc
32 changed files with 366 additions and 349 deletions
27
apps/web-api/src/clients.ts
Normal file
27
apps/web-api/src/clients.ts
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import { PostgreSQLClient } from '@stock-bot/postgres-client';
|
||||
import { MongoDBClient } from '@stock-bot/mongodb-client';
|
||||
|
||||
let postgresClient: PostgreSQLClient | null = null;
|
||||
let mongodbClient: MongoDBClient | null = null;
|
||||
|
||||
export function setPostgreSQLClient(client: PostgreSQLClient): void {
|
||||
postgresClient = client;
|
||||
}
|
||||
|
||||
export function getPostgreSQLClient(): PostgreSQLClient {
|
||||
if (!postgresClient) {
|
||||
throw new Error('PostgreSQL client not initialized. Call setPostgreSQLClient first.');
|
||||
}
|
||||
return postgresClient;
|
||||
}
|
||||
|
||||
export function setMongoDBClient(client: MongoDBClient): void {
|
||||
mongodbClient = client;
|
||||
}
|
||||
|
||||
export function getMongoDBClient(): MongoDBClient {
|
||||
if (!mongodbClient) {
|
||||
throw new Error('MongoDB client not initialized. Call setMongoDBClient first.');
|
||||
}
|
||||
return mongodbClient;
|
||||
}
|
||||
|
|
@ -3,17 +3,30 @@
|
|||
*/
|
||||
import { Hono } from 'hono';
|
||||
import { cors } from 'hono/cors';
|
||||
import { initializeConfig, getServiceConfig } from '@stock-bot/config-new';
|
||||
import { getLogger, shutdownLoggers } from '@stock-bot/logger';
|
||||
import { connectMongoDB, disconnectMongoDB } from '@stock-bot/mongodb-client';
|
||||
import { connectPostgreSQL, disconnectPostgreSQL } from '@stock-bot/postgres-client';
|
||||
import { initializeConfig, getServiceConfig, getLoggingConfig } from '@stock-bot/config-new';
|
||||
import { getLogger, shutdownLoggers, setLoggerConfig } from '@stock-bot/logger';
|
||||
import { createAndConnectMongoDBClient, MongoDBClient } from '@stock-bot/mongodb-client';
|
||||
import { createAndConnectPostgreSQLClient, PostgreSQLClient } from '@stock-bot/postgres-client';
|
||||
import { Shutdown } from '@stock-bot/shutdown';
|
||||
import { setPostgreSQLClient, setMongoDBClient } from './clients';
|
||||
// Import routes
|
||||
import { exchangeRoutes } from './routes/exchange.routes';
|
||||
import { healthRoutes } from './routes/health.routes';
|
||||
|
||||
// Initialize configuration
|
||||
await initializeConfig();
|
||||
const serviceConfig = getServiceConfig();
|
||||
|
||||
// Initialize logger with config
|
||||
const loggingConfig = getLoggingConfig();
|
||||
if (loggingConfig) {
|
||||
setLoggerConfig({
|
||||
logLevel: loggingConfig.level,
|
||||
logConsole: true,
|
||||
logFile: false,
|
||||
environment: serviceConfig.environment,
|
||||
});
|
||||
}
|
||||
|
||||
const app = new Hono();
|
||||
|
||||
|
|
@ -29,9 +42,10 @@ app.use(
|
|||
);
|
||||
|
||||
const logger = getLogger('web-api');
|
||||
const serviceConfig = getServiceConfig();
|
||||
const PORT = serviceConfig.port;
|
||||
let server: ReturnType<typeof Bun.serve> | null = null;
|
||||
let postgresClient: PostgreSQLClient | null = null;
|
||||
let mongoClient: MongoDBClient | null = null;
|
||||
|
||||
// Initialize shutdown manager
|
||||
const shutdown = Shutdown.getInstance({ timeout: 15000 });
|
||||
|
|
@ -61,12 +75,37 @@ async function initializeServices() {
|
|||
try {
|
||||
// Initialize MongoDB client
|
||||
logger.info('Connecting to MongoDB...');
|
||||
await connectMongoDB();
|
||||
const mongoConfig = serviceConfig.database.mongodb;
|
||||
mongoClient = await createAndConnectMongoDBClient({
|
||||
uri: mongoConfig.uri,
|
||||
database: mongoConfig.database,
|
||||
host: 'localhost',
|
||||
port: 27017,
|
||||
timeouts: {
|
||||
connectTimeout: mongoConfig.connectionTimeout || 30000,
|
||||
socketTimeout: 30000,
|
||||
serverSelectionTimeout: mongoConfig.serverSelectionTimeout || 5000,
|
||||
},
|
||||
});
|
||||
setMongoDBClient(mongoClient);
|
||||
logger.info('MongoDB connected');
|
||||
|
||||
// Initialize PostgreSQL client
|
||||
logger.info('Connecting to PostgreSQL...');
|
||||
await connectPostgreSQL();
|
||||
const pgConfig = serviceConfig.database.postgres;
|
||||
postgresClient = await createAndConnectPostgreSQLClient({
|
||||
host: pgConfig.host,
|
||||
port: pgConfig.port,
|
||||
database: pgConfig.database,
|
||||
username: pgConfig.username,
|
||||
password: pgConfig.password,
|
||||
poolSettings: {
|
||||
min: 2,
|
||||
max: pgConfig.maxConnections || 10,
|
||||
idleTimeoutMillis: 30000,
|
||||
},
|
||||
});
|
||||
setPostgreSQLClient(postgresClient);
|
||||
logger.info('PostgreSQL connected');
|
||||
|
||||
logger.info('All services initialized successfully');
|
||||
|
|
@ -105,8 +144,12 @@ shutdown.onShutdown(async () => {
|
|||
shutdown.onShutdown(async () => {
|
||||
logger.info('Disconnecting from databases...');
|
||||
try {
|
||||
await disconnectMongoDB();
|
||||
await disconnectPostgreSQL();
|
||||
if (mongoClient) {
|
||||
await mongoClient.disconnect();
|
||||
}
|
||||
if (postgresClient) {
|
||||
await postgresClient.disconnect();
|
||||
}
|
||||
logger.info('Database connections closed');
|
||||
} catch (error) {
|
||||
logger.error('Error closing database connections', { error });
|
||||
|
|
|
|||
|
|
@ -3,8 +3,7 @@
|
|||
*/
|
||||
import { Hono } from 'hono';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import { getMongoDBClient } from '@stock-bot/mongodb-client';
|
||||
import { getPostgreSQLClient } from '@stock-bot/postgres-client';
|
||||
import { getPostgreSQLClient, getMongoDBClient } from '../clients';
|
||||
|
||||
const logger = getLogger('health-routes');
|
||||
export const healthRoutes = new Hono();
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import { getLogger } from '@stock-bot/logger';
|
||||
import { getPostgreSQLClient } from '@stock-bot/postgres-client';
|
||||
import { getMongoDBClient } from '@stock-bot/mongodb-client';
|
||||
import { getPostgreSQLClient, getMongoDBClient } from '../clients';
|
||||
import {
|
||||
Exchange,
|
||||
ExchangeWithMappings,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue