From 6aab980fb7e75f9b00d3e9a6feba4e7b21ba59e2 Mon Sep 17 00:00:00 2001 From: John Schulz Date: Fri, 6 Sep 2019 14:02:06 -0400 Subject: [PATCH] WIP towards using NP router. Create router in root shim. The server plugin isn't ready to be full NP yet (i.e. "ManagerPlugin implements Plugin", etc) but we don't need lots of features that would provide anyway (e.g. defining contexts, etc). If we change the plugin to only accept a router instance, we can get get NP router and pull the elasticsearch & savedobject clients (when SO client merges) off the new `context` value in the handlers. The big thing I want to wait for is the support for `router.route` that's coming in https://github.com/elastic/kibana/issues/44620 --- .../plugins/integrations_manager/index.ts | 11 ++++-- .../integrations_manager/server/plugin.ts | 38 ++++--------------- 2 files changed, 14 insertions(+), 35 deletions(-) diff --git a/x-pack/legacy/plugins/integrations_manager/index.ts b/x-pack/legacy/plugins/integrations_manager/index.ts index 02afc133928dafb..39ee722c0e61b51 100644 --- a/x-pack/legacy/plugins/integrations_manager/index.ts +++ b/x-pack/legacy/plugins/integrations_manager/index.ts @@ -9,8 +9,9 @@ import { LegacyPluginInitializer, LegacyPluginOptions } from 'src/legacy/types'; import KbnServer, { Server } from 'src/legacy/server/kbn_server'; import { Feature } from '../xpack_main/server/lib/feature_registry'; import { PLUGIN } from './common/constants'; +import { API_ROOT } from './common/routes'; import manifest from './kibana.json'; -import { CoreSetup, Plugin as ServerPlugin, PluginInitializerContext } from './server/plugin'; +import { CoreSetup, Plugin as ServerPlugin } from './server/plugin'; import { mappings, savedObjectSchemas } from './server/saved_objects'; const ROOT = `plugins/${PLUGIN.ID}`; @@ -74,10 +75,12 @@ const pluginOptions: LegacyPluginOptions = { // `kbnServer.server` is the same hapi instance // `kbnServer.newPlatform` has important values const kbnServer = (server as unknown) as KbnServer; - const initializerContext: PluginInitializerContext = {}; - const coreSetup: CoreSetup = kbnServer.newPlatform.setup.core; - new ServerPlugin(initializerContext).setup(coreSetup); + const kbnCore = kbnServer.newPlatform.setup.core; + const router = kbnCore.http.createRouter(API_ROOT); + const coreSetup: CoreSetup = { router }; + + new ServerPlugin().setup(coreSetup); }, postInit: undefined, isEnabled: false, diff --git a/x-pack/legacy/plugins/integrations_manager/server/plugin.ts b/x-pack/legacy/plugins/integrations_manager/server/plugin.ts index f713a5be46cfbe8..3d09d3e0c6e3b1a 100644 --- a/x-pack/legacy/plugins/integrations_manager/server/plugin.ts +++ b/x-pack/legacy/plugins/integrations_manager/server/plugin.ts @@ -4,19 +4,12 @@ * you may not use this file except in compliance with the Elastic License. */ -import { - ClusterClient, - CoreStart, - ElasticsearchServiceSetup, - HttpServiceSetup, -} from 'src/core/server'; -import { PLUGIN } from '../common/constants'; +import { IRouter } from 'src/core/server'; import { fetchList } from './registry'; import { routes } from './routes'; export interface CoreSetup { - elasticsearch: ElasticsearchServiceSetup; - http: HttpServiceSetup; + router: IRouter; } // eslint-disable-next-line @typescript-eslint/no-empty-interface @@ -29,34 +22,17 @@ export interface PluginContext { } export class Plugin { - constructor(initializerContext: PluginInitializerContext) {} + constructor() {} public setup(core: CoreSetup) { - const { http, elasticsearch } = core; - const { server } = http; - const pluginContext: PluginContext = { - esClient: elasticsearch.createClient(PLUGIN.ID), - }; - - // make pluginContext entries available to handlers via h.context - // https://github.com/hapijs/hapi/blob/master/API.md#route.options.bind - // aligns closely with approach proposed in handler RFC - // https://github.com/epixa/kibana/blob/rfc-handlers/rfcs/text/0003_handler_interface.md - const routesWithContext = routes.map(function injectRouteContext(route) { - // merge route.options.bind, defined or otherwise, into pluginContext - // routes can add extra values or override pluginContext values (e.g. spies, etc) - if (!route.options) route.options = {}; - route.options.bind = Object.assign({}, pluginContext, route.options.bind); - - return route; - }); + const { router } = core; - // map routes to handlers - server.route(routesWithContext); + router.route(routes); // the JS API for other consumers return { getList: fetchList, }; } - public start(core: CoreStart) {} + public start() {} + public stop() {} }