|
1 | 1 | import { |
2 | | - CORTEX_CPP_PROCESS_DESTROY_URL, |
3 | 2 | CORTEX_JS_SYSTEM_URL, |
| 3 | + defaultCortexCppPort, |
4 | 4 | defaultCortexJsHost, |
5 | 5 | defaultCortexJsPort, |
6 | 6 | } from '@/infrastructure/constants/cortex'; |
7 | 7 | import { getApp } from './app'; |
| 8 | +import { fileManagerService } from './infrastructure/services/file-manager/file-manager.service'; |
8 | 9 | import { CortexUsecases } from './usecases/cortex/cortex.usecases'; |
9 | 10 |
|
| 11 | +let host: string; |
| 12 | +let port: number; |
| 13 | +let enginePort: number; |
| 14 | + |
10 | 15 | /** |
11 | 16 | * Start the API server |
12 | 17 | */ |
13 | | -export async function start(host?: string, port?: number) { |
14 | | - // getting port from env |
15 | | - const sHost = host || process.env.CORTEX_JS_HOST || defaultCortexJsHost; |
16 | | - const sPort = port || process.env.CORTEX_JS_PORT || defaultCortexJsPort; |
17 | | - const app = await getApp(sHost, Number(sPort)); |
| 18 | +export async function start( |
| 19 | + name?: string, |
| 20 | + address?: string, |
| 21 | + portNumber?: number, |
| 22 | + enginePortNumber?: number, |
| 23 | + dataFolder?: string, |
| 24 | +) { |
| 25 | + if (name) { |
| 26 | + const isProfileConfigExists = fileManagerService.profileConfigExists(name); |
| 27 | + if (!isProfileConfigExists) { |
| 28 | + await fileManagerService.writeConfigFile({ |
| 29 | + ...fileManagerService.defaultConfig(), |
| 30 | + apiServerHost: address || defaultCortexJsHost, |
| 31 | + apiServerPort: port || defaultCortexJsPort, |
| 32 | + cortexCppPort: Number(enginePort) || defaultCortexCppPort, |
| 33 | + }); |
| 34 | + } |
| 35 | + } |
| 36 | + const { |
| 37 | + apiServerHost: configApiServerHost, |
| 38 | + apiServerPort: configApiServerPort, |
| 39 | + cortexCppPort: configCortexCppPort, |
| 40 | + } = await fileManagerService.getConfig(); |
| 41 | + |
| 42 | + host = address || configApiServerHost || defaultCortexJsHost; |
| 43 | + port = portNumber || configApiServerPort || defaultCortexJsPort; |
| 44 | + if (host === 'localhost') { |
| 45 | + host = '127.0.0.1'; |
| 46 | + } |
| 47 | + enginePort = |
| 48 | + Number(enginePortNumber) || configCortexCppPort || defaultCortexCppPort; |
| 49 | + const dataFolderPath = dataFolder; |
| 50 | + |
| 51 | + return startServer(dataFolderPath); |
| 52 | +} |
| 53 | + |
| 54 | +async function startServer(dataFolderPath?: string) { |
| 55 | + const config = await fileManagerService.getConfig(); |
18 | 56 | try { |
19 | | - await app.listen(sPort, sHost); |
| 57 | + if (dataFolderPath) { |
| 58 | + await fileManagerService.writeConfigFile({ |
| 59 | + ...config, |
| 60 | + dataFolderPath, |
| 61 | + }); |
| 62 | + // load config again to create the data folder |
| 63 | + await fileManagerService.getConfig(dataFolderPath); |
| 64 | + } |
| 65 | + const app = await getApp(host, port); |
20 | 66 | const cortexUsecases = await app.resolve(CortexUsecases); |
21 | | - await cortexUsecases.startCortex(); |
22 | | - console.log(`Started server at http://${sHost}:${sPort}`); |
23 | | - console.log(`API Playground available at http://${sHost}:${sPort}/api`); |
24 | | - } catch { |
| 67 | + await cortexUsecases.startCortex().catch((e) => { |
| 68 | + throw e; |
| 69 | + }); |
| 70 | + const isServerOnline = await cortexUsecases.isAPIServerOnline(); |
| 71 | + if (isServerOnline) { |
| 72 | + console.log( |
| 73 | + `Server is already running at http://${host}:${port}. Please use 'cortex stop' to stop the server.`, |
| 74 | + ); |
| 75 | + } |
| 76 | + await app.listen(port, host); |
| 77 | + await fileManagerService.writeConfigFile({ |
| 78 | + ...config, |
| 79 | + apiServerHost: host, |
| 80 | + apiServerPort: port, |
| 81 | + dataFolderPath: dataFolderPath || config.dataFolderPath, |
| 82 | + cortexCppPort: enginePort, |
| 83 | + }); |
| 84 | + } catch (e) { |
| 85 | + console.error(e); |
| 86 | + // revert the data folder path if it was set |
| 87 | + await fileManagerService.writeConfigFile({ |
| 88 | + ...config, |
| 89 | + }); |
25 | 90 | console.error(`Failed to start server. Is port ${port} in use?`); |
26 | 91 | } |
27 | 92 | } |
28 | | - |
29 | 93 | /** |
30 | 94 | * Stop the API server |
31 | 95 | * @returns |
32 | 96 | */ |
33 | 97 | export async function stop(host?: string, port?: number) { |
34 | 98 | return fetch(CORTEX_JS_SYSTEM_URL(host, port), { |
35 | 99 | method: 'DELETE', |
36 | | - }) |
37 | | - .catch(() => {}) |
38 | | - .then(() => |
39 | | - fetch(CORTEX_CPP_PROCESS_DESTROY_URL(host, port), { |
40 | | - method: 'DELETE', |
41 | | - }), |
42 | | - ) |
43 | | - .catch(() => {}); |
| 100 | + }); |
44 | 101 | } |
0 commit comments