From e01ece3c36f7f374af9839ef5d6908c950d1fd33 Mon Sep 17 00:00:00 2001 From: Ronit Agarwala Date: Tue, 21 Nov 2023 23:09:16 -0500 Subject: [PATCH] Change user provided api file name. Add code to run user defined api calls from sandbox for now. --- index.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/index.ts b/index.ts index 21e6043..e027293 100644 --- a/index.ts +++ b/index.ts @@ -6,6 +6,9 @@ * SPDX-License-Identifier: Apache-2.0 */ +import { exists } from './paths' +import { join } from 'path' +import { pathToFileURL } from 'url' import { launch } from './run.js' import type { LocalOpenSearch } from './run.js' import { populate } from './data.js' @@ -59,6 +62,7 @@ export const deploy = { } let local: LocalOpenSearch +const openSearchApiFile = 'openSearchApi.js' export const sandbox = { async start({ @@ -70,8 +74,30 @@ export const sandbox = { }, }) { local = await launch({}) + + //Load api call file and run all api calls to cluster + const api_path = join(cwd, openSearchApiFile) + let result + if (await exists(api_path)) { + console.log(`Found ${openSearchApiFile} file, deploying ML model`) + result = (await import(pathToFileURL(api_path).toString())).default + + //result should be a function that returns a promise + if (typeof result === 'function') { + result = result({ node: local.url }) + } + + //wait for the returned promise to resolve and thus all api calls to complete + if (result instanceof Promise) { + await result + } + } else { + console.log(`No ${openSearchApiFile} file found.`) + } + await populate(cwd, { node: local.url }) }, + async end() { await local.stop() },