Skip to content

Commit

Permalink
WIP towards using NP router. Create router in root shim.
Browse files Browse the repository at this point in the history
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 elastic#44620
  • Loading branch information
John Schulz committed Sep 6, 2019
1 parent a9fd48e commit 6aab980
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 35 deletions.
11 changes: 7 additions & 4 deletions x-pack/legacy/plugins/integrations_manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down Expand Up @@ -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,
Expand Down
38 changes: 7 additions & 31 deletions x-pack/legacy/plugins/integrations_manager/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {}
}

0 comments on commit 6aab980

Please sign in to comment.