diff --git a/cortex-js/src/index.ts b/cortex-js/src/index.ts index c383a5417..34a1cef2a 100644 --- a/cortex-js/src/index.ts +++ b/cortex-js/src/index.ts @@ -1,31 +1,95 @@ import { - CORTEX_CPP_PROCESS_DESTROY_URL, CORTEX_JS_SYSTEM_URL, + defaultCortexCppPort, defaultCortexJsHost, defaultCortexJsPort, } from '@/infrastructure/constants/cortex'; import { getApp } from './app'; +import { fileManagerService } from './infrastructure/services/file-manager/file-manager.service'; import { CortexUsecases } from './usecases/cortex/cortex.usecases'; +let host: string; +let port: number; +let enginePort: number; + /** * Start the API server */ -export async function start(host?: string, port?: number) { - // getting port from env - const sHost = host || process.env.CORTEX_JS_HOST || defaultCortexJsHost; - const sPort = port || process.env.CORTEX_JS_PORT || defaultCortexJsPort; - const app = await getApp(sHost, Number(sPort)); +export async function start( + name?: string, + address?: string, + portNumber?: number, + enginePortNumber?: number, + dataFolder?: string, +) { + if (name) { + const isProfileConfigExists = fileManagerService.profileConfigExists(name); + if (!isProfileConfigExists) { + await fileManagerService.writeConfigFile({ + ...fileManagerService.defaultConfig(), + apiServerHost: address || defaultCortexJsHost, + apiServerPort: port || defaultCortexJsPort, + cortexCppPort: Number(enginePort) || defaultCortexCppPort, + }); + } + } + const { + apiServerHost: configApiServerHost, + apiServerPort: configApiServerPort, + cortexCppPort: configCortexCppPort, + } = await fileManagerService.getConfig(); + + host = address || configApiServerHost || defaultCortexJsHost; + port = portNumber || configApiServerPort || defaultCortexJsPort; + if (host === 'localhost') { + host = '127.0.0.1'; + } + enginePort = + Number(enginePortNumber) || configCortexCppPort || defaultCortexCppPort; + const dataFolderPath = dataFolder; + + return startServer(dataFolderPath); +} + +async function startServer(dataFolderPath?: string) { + const config = await fileManagerService.getConfig(); try { - await app.listen(sPort, sHost); + if (dataFolderPath) { + await fileManagerService.writeConfigFile({ + ...config, + dataFolderPath, + }); + // load config again to create the data folder + await fileManagerService.getConfig(dataFolderPath); + } + const app = await getApp(host, port); const cortexUsecases = await app.resolve(CortexUsecases); - await cortexUsecases.startCortex(); - console.log(`Started server at http://${sHost}:${sPort}`); - console.log(`API Playground available at http://${sHost}:${sPort}/api`); - } catch { + await cortexUsecases.startCortex().catch((e) => { + throw e; + }); + const isServerOnline = await cortexUsecases.isAPIServerOnline(); + if (isServerOnline) { + console.log( + `Server is already running at http://${host}:${port}. Please use 'cortex stop' to stop the server.`, + ); + } + await app.listen(port, host); + await fileManagerService.writeConfigFile({ + ...config, + apiServerHost: host, + apiServerPort: port, + dataFolderPath: dataFolderPath || config.dataFolderPath, + cortexCppPort: enginePort, + }); + } catch (e) { + console.error(e); + // revert the data folder path if it was set + await fileManagerService.writeConfigFile({ + ...config, + }); console.error(`Failed to start server. Is port ${port} in use?`); } } - /** * Stop the API server * @returns @@ -33,12 +97,5 @@ export async function start(host?: string, port?: number) { export async function stop(host?: string, port?: number) { return fetch(CORTEX_JS_SYSTEM_URL(host, port), { method: 'DELETE', - }) - .catch(() => {}) - .then(() => - fetch(CORTEX_CPP_PROCESS_DESTROY_URL(host, port), { - method: 'DELETE', - }), - ) - .catch(() => {}); + }); } diff --git a/cortex-js/src/utils/logs.ts b/cortex-js/src/utils/logs.ts index 3ee06be92..1802d6d2b 100644 --- a/cortex-js/src/utils/logs.ts +++ b/cortex-js/src/utils/logs.ts @@ -74,7 +74,7 @@ export async function cleanLogs( // Schedule the next execution with doubled delays timeout = setTimeout( - () => this.cleanLogs(maxFileSizeBytes, daysToKeep), + () => cleanLogs(maxFileSizeBytes, daysToKeep), logCleaningInterval, ); }