-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: support more configurations via direct js import (#997)
- Loading branch information
Showing
2 changed files
with
78 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,101 @@ | ||
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 | ||
*/ | ||
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(() => {}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters