From 28d0960a9111914f7188b5d53e1690bf42eb6604 Mon Sep 17 00:00:00 2001 From: pgayvallet Date: Wed, 24 Apr 2024 09:17:48 +0200 Subject: [PATCH] Fix `maps` plugin async route registration --- .../server/data_indexing/indexing_routes.ts | 5 ++-- x-pack/plugins/maps/server/mvt/mvt_routes.ts | 8 +++---- x-pack/plugins/maps/server/routes.ts | 24 ++++++++++++++----- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/x-pack/plugins/maps/server/data_indexing/indexing_routes.ts b/x-pack/plugins/maps/server/data_indexing/indexing_routes.ts index 94fbb94165514..e7cb10427a297 100644 --- a/x-pack/plugins/maps/server/data_indexing/indexing_routes.ts +++ b/x-pack/plugins/maps/server/data_indexing/indexing_routes.ts @@ -26,11 +26,11 @@ import { getMatchingIndexes } from './get_indexes_matching_pattern'; export function initIndexingRoutes({ router, logger, - dataPlugin, + getDataPlugin, }: { router: IRouter; logger: Logger; - dataPlugin: DataPluginStart; + getDataPlugin: () => Promise; securityPlugin?: SecurityPluginStart; }) { router.versioned @@ -58,6 +58,7 @@ export function initIndexingRoutes({ async (context, request, response) => { const coreContext = await context.core; const { index, mappings } = request.body; + const dataPlugin = await getDataPlugin(); const indexPatternsService = await dataPlugin.indexPatterns.dataViewsServiceFactory( coreContext.savedObjects.client, coreContext.elasticsearch.client.asCurrentUser, diff --git a/x-pack/plugins/maps/server/mvt/mvt_routes.ts b/x-pack/plugins/maps/server/mvt/mvt_routes.ts index 1cd53f02f83c6..f768eb93dd9a3 100644 --- a/x-pack/plugins/maps/server/mvt/mvt_routes.ts +++ b/x-pack/plugins/maps/server/mvt/mvt_routes.ts @@ -27,11 +27,11 @@ const CACHE_TIMEOUT_SECONDS = 60 * 60; export function initMVTRoutes({ router, logger, - core, + getCore, }: { router: IRouter; logger: Logger; - core: CoreStart; + getCore: () => Promise; }) { router.versioned .get({ @@ -93,7 +93,7 @@ export function initMVTRoutes({ abortController: makeAbortController(request), body: tileRequest.body, context, - core, + core: await getCore(), executionContext: makeExecutionContext({ type: 'server', name: APP_ID, @@ -173,7 +173,7 @@ export function initMVTRoutes({ abortController: makeAbortController(request), body: tileRequest.body, context, - core, + core: await getCore(), executionContext: makeExecutionContext({ type: 'server', name: APP_ID, diff --git a/x-pack/plugins/maps/server/routes.ts b/x-pack/plugins/maps/server/routes.ts index 07bf8fefd9d14..32f7a9e6c18ea 100644 --- a/x-pack/plugins/maps/server/routes.ts +++ b/x-pack/plugins/maps/server/routes.ts @@ -8,7 +8,7 @@ import { schema } from '@kbn/config-schema'; import fs from 'fs'; import path from 'path'; -import { CoreSetup, CoreStart, IRouter, Logger } from '@kbn/core/server'; +import { CoreSetup, IRouter, Logger } from '@kbn/core/server'; import { DataRequestHandlerContext } from '@kbn/data-plugin/server'; import { INDEX_SETTINGS_API_PATH, FONTS_API_PATH } from '../common/constants'; import { getIndexPatternSettings } from './lib/get_index_pattern_settings'; @@ -16,10 +16,8 @@ import { initMVTRoutes } from './mvt/mvt_routes'; import { initIndexingRoutes } from './data_indexing/indexing_routes'; import { StartDeps } from './types'; -export async function initRoutes(coreSetup: CoreSetup, logger: Logger): Promise { +export function initRoutes(coreSetup: CoreSetup, logger: Logger) { const router: IRouter = coreSetup.http.createRouter(); - const [coreStart, { data: dataPlugin }]: [CoreStart, StartDeps] = - (await coreSetup.getStartServices()) as unknown as [CoreStart, StartDeps]; router.versioned .get({ @@ -109,6 +107,20 @@ export async function initRoutes(coreSetup: CoreSetup, logger: Logger): Promise< } ); - initMVTRoutes({ router, logger, core: coreStart }); - initIndexingRoutes({ router, logger, dataPlugin }); + initMVTRoutes({ + router, + logger, + getCore: async () => { + const [core] = await coreSetup.getStartServices(); + return core; + }, + }); + initIndexingRoutes({ + router, + logger, + getDataPlugin: async () => { + const [, { data }] = await coreSetup.getStartServices(); + return data; + }, + }); }