Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 93a28de

Browse files
committed
chore: support more configurations via direct js import
1 parent 1d822da commit 93a28de

File tree

2 files changed

+78
-21
lines changed

2 files changed

+78
-21
lines changed

cortex-js/src/index.ts

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,101 @@
11
import {
2-
CORTEX_CPP_PROCESS_DESTROY_URL,
32
CORTEX_JS_SYSTEM_URL,
3+
defaultCortexCppPort,
44
defaultCortexJsHost,
55
defaultCortexJsPort,
66
} from '@/infrastructure/constants/cortex';
77
import { getApp } from './app';
8+
import { fileManagerService } from './infrastructure/services/file-manager/file-manager.service';
89
import { CortexUsecases } from './usecases/cortex/cortex.usecases';
910

11+
let host: string;
12+
let port: number;
13+
let enginePort: number;
14+
1015
/**
1116
* Start the API server
1217
*/
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();
1856
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);
2066
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+
});
2590
console.error(`Failed to start server. Is port ${port} in use?`);
2691
}
2792
}
28-
2993
/**
3094
* Stop the API server
3195
* @returns
3296
*/
3397
export async function stop(host?: string, port?: number) {
3498
return fetch(CORTEX_JS_SYSTEM_URL(host, port), {
3599
method: 'DELETE',
36-
})
37-
.catch(() => {})
38-
.then(() =>
39-
fetch(CORTEX_CPP_PROCESS_DESTROY_URL(host, port), {
40-
method: 'DELETE',
41-
}),
42-
)
43-
.catch(() => {});
100+
});
44101
}

cortex-js/src/utils/logs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export async function cleanLogs(
7474

7575
// Schedule the next execution with doubled delays
7676
timeout = setTimeout(
77-
() => this.cleanLogs(maxFileSizeBytes, daysToKeep),
77+
() => cleanLogs(maxFileSizeBytes, daysToKeep),
7878
logCleaningInterval,
7979
);
8080
}

0 commit comments

Comments
 (0)