-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial server route; add core & route services (#27)
Signed-off-by: Tyler Ohlsen <[email protected]>
- Loading branch information
Showing
15 changed files
with
133 additions
and
90 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
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
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
This file was deleted.
Oops, something went wrong.
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
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { CoreStart, HttpFetchError } from '../../../src/core/public'; | ||
import { SEARCH_PATH } from '../common'; | ||
|
||
export interface RouteService { | ||
searchIndex: (indexName: string, body: {}) => Promise<any | HttpFetchError>; | ||
} | ||
|
||
export function configureRoutes(core: CoreStart): RouteService { | ||
return { | ||
searchIndex: async (indexName: string, body: {}) => { | ||
try { | ||
const response = await core.http.post<{ respString: string }>( | ||
`${SEARCH_PATH}/${indexName}`, | ||
{ | ||
body: JSON.stringify(body), | ||
} | ||
); | ||
return response; | ||
} catch (e: any) { | ||
return e as HttpFetchError; | ||
} | ||
}, | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { createGetterSetter } from '../../../src/plugins/opensearch_dashboards_utils/public'; | ||
import { CoreStart } from '../../../src/core/public'; | ||
import { RouteService } from './route_service'; | ||
|
||
export const [getCore, setCore] = createGetterSetter<CoreStart>('Core'); | ||
|
||
export const [getRouteService, setRouteService] = createGetterSetter< | ||
RouteService | ||
>(''); |
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
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
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { schema } from '@osd/config-schema'; | ||
import { SearchRequest } from '@opensearch-project/opensearch/api/types'; | ||
import { | ||
IRouter, | ||
IOpenSearchDashboardsResponse, | ||
} from '../../../../src/core/server'; | ||
import { SEARCH_PATH } from '../../common'; | ||
|
||
export function registerOpenSearchRoutes(router: IRouter): void { | ||
router.post( | ||
{ | ||
path: `${SEARCH_PATH}/{index_name}`, | ||
validate: { | ||
params: schema.object({ | ||
index_name: schema.string(), | ||
}), | ||
body: schema.any(), | ||
}, | ||
}, | ||
async (context, req, res): Promise<IOpenSearchDashboardsResponse<any>> => { | ||
const client = context.core.opensearch.client.asCurrentUser; | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
const { index_name } = req.params; | ||
const body = req.body; | ||
|
||
const params = { | ||
index: index_name, | ||
body, | ||
} as SearchRequest; | ||
|
||
try { | ||
const response = await client.search(params); | ||
return res.ok({ body: response }); | ||
} catch (err: any) { | ||
return res.customError({ | ||
statusCode: err.statusCode || 500, | ||
body: { | ||
message: err.message, | ||
attributes: { | ||
error: err.body?.error || err.message, | ||
}, | ||
}, | ||
}); | ||
} | ||
} | ||
); | ||
} |