From 9b4f70f139fb401e7103cff5c3cd6ff2ff60808d Mon Sep 17 00:00:00 2001 From: marknguyen1302 Date: Mon, 5 Aug 2024 21:32:11 +0700 Subject: [PATCH] fix: force change localhost --- cortex-js/src/app.ts | 4 ++-- cortex-js/src/index.ts | 2 +- .../infrastructure/commanders/cortex-command.commander.ts | 5 ++++- .../services/file-manager/file-manager.service.ts | 7 +++++-- cortex-js/src/usecases/cortex/cortex.usecases.ts | 1 + 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/cortex-js/src/app.ts b/cortex-js/src/app.ts index 0ea26d577..58b87119b 100644 --- a/cortex-js/src/app.ts +++ b/cortex-js/src/app.ts @@ -4,7 +4,7 @@ import { AppModule } from './app.module'; import { fileManagerService } from './infrastructure/services/file-manager/file-manager.service'; import { ValidationPipe } from '@nestjs/common'; import { TelemetryUsecases } from './usecases/telemetry/telemetry.usecases'; -export const getApp = async () => { +export const getApp = async (host?: string, port?: number) => { const app = await NestFactory.create(AppModule, { snapshot: true, cors: true, @@ -64,7 +64,7 @@ export const getApp = async () => { 'System', 'Endpoints for stopping the Cortex API server, checking its status, and fetching system events.', ) - .addServer('http://127.0.0.1:1337') + .addServer(`http://${host || '127.0.0.1'}:${port || 1337}`) .build(); const document = SwaggerModule.createDocument(app, config); diff --git a/cortex-js/src/index.ts b/cortex-js/src/index.ts index 5ba80c22b..166a82941 100644 --- a/cortex-js/src/index.ts +++ b/cortex-js/src/index.ts @@ -13,7 +13,7 @@ import { cleanLogs } from './utils/log'; * Start the API server */ export async function start(host?: string, port?: number) { - const app = await getApp(); + const app = await getApp(host, port); // getting port from env const sHost = host || process.env.CORTEX_JS_HOST || defaultCortexJsHost; const sPort = port || process.env.CORTEX_JS_PORT || defaultCortexJsPort; diff --git a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts index 533143828..ddc24d2d2 100644 --- a/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts +++ b/cortex-js/src/infrastructure/commanders/cortex-command.commander.ts @@ -91,6 +91,9 @@ export class CortexCommand extends CommandRunner { this.host = options?.address || configApiServerHost || defaultCortexJsHost; this.port = options?.port || configApiServerPort || defaultCortexJsPort; + if(this.host === 'localhost') { + this.host = '127.0.0.1'; + } this.enginePort = Number(options?.enginePort) || configCortexCppPort || @@ -134,7 +137,7 @@ export class CortexCommand extends CommandRunner { await fileManagerService.getConfig(dataFolderPath); } if (attach) { - const app = await getApp(); + const app = await getApp(this.host, this.port); await app.listen(this.port, this.host); } else { await this.cortexUseCases.startServerDetached(this.host, this.port); diff --git a/cortex-js/src/infrastructure/services/file-manager/file-manager.service.ts b/cortex-js/src/infrastructure/services/file-manager/file-manager.service.ts index 5975d3b2a..071f95b12 100644 --- a/cortex-js/src/infrastructure/services/file-manager/file-manager.service.ts +++ b/cortex-js/src/infrastructure/services/file-manager/file-manager.service.ts @@ -36,7 +36,7 @@ export class FileManagerService { private benchmarkFoldername = 'benchmark'; private cortexEnginesFolderName = 'engines'; private cortexTelemetryFolderName = 'telemetry'; - private configProfile = 'default'; + private configProfile = process.env.CORTEX_PROFILE || 'default'; /** * Get cortex configs @@ -353,7 +353,7 @@ export class FileManagerService { config = configs?.[this.configProfile] ?? config; } catch {} return { - host: config.apiServerHost ?? 'localhost', + host: config.apiServerHost ?? '127.0.0.1', port: config.apiServerPort ?? 1337, }; } @@ -361,6 +361,9 @@ export class FileManagerService { public setConfigProfile(profile: string) { this.configProfile = profile; } + public getConfigProfile() { + return this.configProfile; + } public profileConfigExists(profile: string): boolean { const homeDir = os.homedir(); const configPath = join(homeDir, this.configFile); diff --git a/cortex-js/src/usecases/cortex/cortex.usecases.ts b/cortex-js/src/usecases/cortex/cortex.usecases.ts index bc8c27cb6..400c76163 100644 --- a/cortex-js/src/usecases/cortex/cortex.usecases.ts +++ b/cortex-js/src/usecases/cortex/cortex.usecases.ts @@ -151,6 +151,7 @@ export class CortexUsecases implements BeforeApplicationShutdown { env: { CORTEX_JS_HOST: host, CORTEX_JS_PORT: port.toString(), + CORTEX_PROFILE: fileManagerService.getConfigProfile(), }, }); server.disconnect(); // closes the IPC channel