-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[Uptime] Shim server for new platform #44938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0967c89
96b5568
edfd343
7ad44f3
5fdf8c0
a8518f4
c4e1a96
5e7a6e6
787038a
3a01810
453b6c5
d918cb7
9267c03
7af1a87
49bc115
e79059f
c53d6a6
70add2f
890d349
e13e5d0
c654473
fca8229
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,3 +5,4 @@ | |
| */ | ||
|
|
||
| export { initServerWithKibana, KibanaServer } from './kibana.index'; | ||
| export { plugin } from './plugin'; | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,21 +5,24 @@ | |
| */ | ||
|
|
||
| import { GraphQLSchema } from 'graphql'; | ||
| import { Server } from 'hapi'; | ||
| import { Request, ResponseToolkit } from 'hapi'; | ||
| import { runHttpQuery } from 'apollo-server-core'; | ||
| import { UptimeCorePlugins, UptimeCoreSetup } from './adapter_types'; | ||
| import { | ||
| UMBackendFrameworkAdapter, | ||
| UMFrameworkRequest, | ||
| UMFrameworkResponse, | ||
| UMFrameworkRouteOptions, | ||
| UMHapiGraphQLPluginOptions, | ||
| } from './adapter_types'; | ||
| import { uptimeGraphQLHapiPlugin } from './apollo_framework_adapter'; | ||
| import { DEFAULT_GRAPHQL_PATH } from '../../../graphql'; | ||
|
|
||
| export class UMKibanaBackendFrameworkAdapter implements UMBackendFrameworkAdapter { | ||
| private server: Server; | ||
|
|
||
| constructor(hapiServer: Server) { | ||
| this.server = hapiServer; | ||
| constructor( | ||
| private readonly server: UptimeCoreSetup, | ||
| private readonly plugins: UptimeCorePlugins | ||
| ) { | ||
| this.server = server; | ||
| this.plugins = plugins; | ||
| } | ||
|
|
||
| public registerRoute< | ||
|
|
@@ -30,24 +33,53 @@ export class UMKibanaBackendFrameworkAdapter implements UMBackendFrameworkAdapte | |
| } | ||
|
|
||
| public registerGraphQLEndpoint(routePath: string, schema: GraphQLSchema): void { | ||
| this.server.register<UMHapiGraphQLPluginOptions>({ | ||
| options: { | ||
| graphQLOptions: (req: any) => ({ | ||
| context: { req }, | ||
| schema, | ||
| }), | ||
| path: routePath, | ||
| route: { | ||
| tags: ['access:uptime'], | ||
| }, | ||
| const options = { | ||
| graphQLOptions: (req: any) => ({ | ||
| context: { req }, | ||
| schema, | ||
| }), | ||
| path: routePath, | ||
| route: { | ||
| tags: ['access:uptime'], | ||
| }, | ||
| }; | ||
| this.server.route({ | ||
| options: options.route, | ||
| handler: async (request: Request, h: ResponseToolkit) => { | ||
| try { | ||
| const { method } = request; | ||
| const query = | ||
| method === 'post' | ||
| ? (request.payload as Record<string, any>) | ||
| : (request.query as Record<string, any>); | ||
|
|
||
| const graphQLResponse = await runHttpQuery([request], { | ||
| method: method.toUpperCase(), | ||
| options: options.graphQLOptions, | ||
| query, | ||
| }); | ||
|
|
||
| return h.response(graphQLResponse).type('application/json'); | ||
| } catch (error) { | ||
| if (error.isGraphQLError === true) { | ||
| return h | ||
| .response(error.message) | ||
| .code(error.statusCode) | ||
| .type('application/json'); | ||
| } | ||
| return h.response(error).type('application/json'); | ||
| } | ||
| }, | ||
| plugin: uptimeGraphQLHapiPlugin, | ||
| method: ['get', 'post'], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a useful example for other teams for how to replace the hapi plugin. When you switch to using the NP router in your shim (from |
||
| path: options.path || DEFAULT_GRAPHQL_PATH, | ||
| vhost: undefined, | ||
| }); | ||
| } | ||
|
|
||
| public getSavedObjectsClient() { | ||
| const { SavedObjectsClient, getSavedObjectsRepository } = this.server.savedObjects; | ||
| const { callWithInternalUser } = this.server.plugins.elasticsearch.getCluster('admin'); | ||
| const { elasticsearch, savedObjects } = this.plugins; | ||
| const { SavedObjectsClient, getSavedObjectsRepository } = savedObjects; | ||
| const { callWithInternalUser } = elasticsearch.getCluster('admin'); | ||
| const internalRepository = getSavedObjectsRepository(callWithInternalUser); | ||
| return new SavedObjectsClient(internalRepository); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| import { PluginInitializerContext } from 'src/core/server'; | ||
| import { initServerWithKibana } from './kibana.index'; | ||
| import { UptimeCoreSetup, UptimeCorePlugins } from './lib/adapters/framework'; | ||
|
|
||
| export function plugin(initializerContext: PluginInitializerContext) { | ||
| return new Plugin(); | ||
| } | ||
|
|
||
| export class Plugin { | ||
| public setup(core: UptimeCoreSetup, plugins: UptimeCorePlugins) { | ||
| initServerWithKibana(core, plugins); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
optional: you can also use the NP elasticsearch client by taking it from
server.newPlatform.setup.core.elasticsearch