Skip to content
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

LP plugins can leverage new http server during migration #36120

Closed

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ export interface CoreStart

| Property | Type | Description |
| --- | --- | --- |
| [http](./kibana-plugin-server.corestart.http.md) | <code>HttpServiceStart</code> | |
| [plugins](./kibana-plugin-server.corestart.plugins.md) | <code>PluginsServiceStart</code> | |

Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
<b>Signature:</b>

```typescript
export declare type HttpServiceSetup = HttpServerSetup;
export declare type HttpServiceSetup = HttpServerSetup & {
shouldListen: () => boolean;
};
```

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@

[Home](./index) &gt; [kibana-plugin-server](./kibana-plugin-server.md) &gt; [HttpServiceStart](./kibana-plugin-server.httpservicestart.md)

## HttpServiceStart interface
## HttpServiceStart type


<b>Signature:</b>

```typescript
export interface HttpServiceStart
export declare type HttpServiceStart = void;
```

## Properties

| Property | Type | Description |
| --- | --- | --- |
| [isListening](./kibana-plugin-server.httpservicestart.islistening.md) | <code>() =&gt; boolean</code> | Indicates if http server is listening on a port |

2 changes: 1 addition & 1 deletion docs/development/core/server/kibana-plugin-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
| [CoreSetup](./kibana-plugin-server.coresetup.md) | |
| [CoreStart](./kibana-plugin-server.corestart.md) | |
| [ElasticsearchServiceSetup](./kibana-plugin-server.elasticsearchservicesetup.md) | |
| [HttpServiceStart](./kibana-plugin-server.httpservicestart.md) | |
| [Logger](./kibana-plugin-server.logger.md) | Logger exposes all the necessary methods to log any type of information and this is the interface used by the logging consumers including plugins. |
| [LoggerFactory](./kibana-plugin-server.loggerfactory.md) | The single purpose of <code>LoggerFactory</code> interface is to define a way to retrieve a context-based logger instance. |
| [LogMeta](./kibana-plugin-server.logmeta.md) | Contextual metadata |
Expand All @@ -42,6 +41,7 @@
| [ElasticsearchClientConfig](./kibana-plugin-server.elasticsearchclientconfig.md) | |
| [Headers](./kibana-plugin-server.headers.md) | |
| [HttpServiceSetup](./kibana-plugin-server.httpservicesetup.md) | |
| [HttpServiceStart](./kibana-plugin-server.httpservicestart.md) | |
| [OnRequestHandler](./kibana-plugin-server.onrequesthandler.md) | |
| [PluginInitializer](./kibana-plugin-server.plugininitializer.md) | The <code>plugin</code> export at the root of a plugin's <code>server</code> directory should conform to this interface. |
| [PluginName](./kibana-plugin-server.pluginname.md) | Dedicated type for plugin name/id that is supposed to make Map/Set/Arrays that use it as a key or value more obvious. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function runKibanaServer({ procs, config, options }) {
...process.env,
},
cwd: installDir || KIBANA_ROOT,
wait: /Server running/,
wait: /The core is running/,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ describe('Server logging configuration', function () {
'--logging.json', 'false'
]);

watchFileUntil(logPath, /Server running at/, 2 * minute)
watchFileUntil(logPath, /The core is running/, 2 * minute)
.then(() => {
// once the server is running, archive the log file and issue SIGHUP
fs.renameSync(logPath, logPathArchived);
Expand Down
11 changes: 2 additions & 9 deletions src/core/server/http/http_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { HttpService } from './http_service';

const createSetupContractMock = () => {
const setupContract = {
shouldListen: jest.fn(),
options: {} as ServerOptions,
registerAuth: jest.fn(),
registerOnRequest: jest.fn(),
Expand All @@ -31,17 +32,10 @@ const createSetupContractMock = () => {
// we can mock some hapi server method when we need it
server: {} as Server,
};
setupContract.shouldListen.mockReturnValue(true);
return setupContract;
};

const createStartContractMock = () => {
const startContract = {
isListening: jest.fn(),
};
startContract.isListening.mockReturnValue(true);
return startContract;
};

type HttpServiceContract = PublicMethodsOf<HttpService>;
const createHttpServiceMock = () => {
const mocked: jest.Mocked<HttpServiceContract> = {
Expand All @@ -50,7 +44,6 @@ const createHttpServiceMock = () => {
stop: jest.fn(),
};
mocked.setup.mockResolvedValue(createSetupContractMock());
mocked.start.mockResolvedValue(createStartContractMock());
return mocked;
};

Expand Down
8 changes: 7 additions & 1 deletion src/core/server/http/http_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,13 @@ test('returns http server contract on setup', async () => {

const service = new HttpService({ configService, env, logger });

expect(await service.setup()).toBe(httpServer);
const setupContract = await service.setup();
expect(setupContract).toMatchObject(httpServer);
expect(setupContract).toMatchObject(
expect.objectContaining({
shouldListen: expect.any(Function),
})
);
});

test('does not start http server if process is dev cluster master', async () => {
Expand Down
35 changes: 19 additions & 16 deletions src/core/server/http/http_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ import { HttpServer, HttpServerSetup } from './http_server';
import { HttpsRedirectServer } from './https_redirect_server';

/** @public */
export type HttpServiceSetup = HttpServerSetup;
export type HttpServiceSetup = HttpServerSetup & {
/** Indicates if http server has configured to listen on a port */
shouldListen: () => boolean;
};

/** @public */
export interface HttpServiceStart {
/** Indicates if http server is listening on a port */
isListening: () => boolean;
}
export type HttpServiceStart = void;

/** @internal */
export class HttpService implements CoreService<HttpServiceSetup, HttpServiceStart> {
Expand Down Expand Up @@ -67,17 +68,15 @@ export class HttpService implements CoreService<HttpServiceSetup, HttpServiceSta

const config = await this.config$.pipe(first()).toPromise();

return this.httpServer.setup(config);
return {
...this.httpServer.setup(config),
shouldListen: () => this.shouldListen(config),
};
}

public async start() {
const config = await this.config$.pipe(first()).toPromise();

// We shouldn't set up http service in two cases:`
// 1. If `server.autoListen` is explicitly set to `false`.
// 2. When the process is run as dev cluster master in which case cluster manager
// will fork a dedicated process where http service will be set up instead.
if (!this.coreContext.env.isDevClusterMaster && config.autoListen) {
if (this.shouldListen(config)) {
// If a redirect port is specified, we start an HTTP server at this port and
// redirect all requests to the SSL port.
if (config.ssl.enabled && config.ssl.redirectHttpFromPort !== undefined) {
Expand All @@ -86,10 +85,6 @@ export class HttpService implements CoreService<HttpServiceSetup, HttpServiceSta

await this.httpServer.start(config);
}

return {
isListening: () => this.httpServer.isListening(),
};
}

public async stop() {
Expand All @@ -103,4 +98,12 @@ export class HttpService implements CoreService<HttpServiceSetup, HttpServiceSta
await this.httpServer.stop();
await this.httpsRedirectServer.stop();
}

private shouldListen(config: HttpConfig) {
// We shouldn't start http service in two cases:`
// 1. If `server.autoListen` is explicitly set to `false`.
// 2. When the process is run as dev cluster master in which case cluster manager
// will fork a dedicated process where http service will be set up instead.
return !this.coreContext.env.isDevClusterMaster && config.autoListen;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { Router } from '../../../../../../server';

// eslint-disable-next-line import/no-default-export
export default function(kibana) {
return new kibana.Plugin({
id: 'dummy-legacy',

init(server) {
const router = new Router('');
router.get({ path: '/hi', validate: false }, async (req, res) =>
res.ok({ content: 'from-new-platform' })
);
server.newPlatform.setup.core.http.registerRouter(router);
},
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "dummy-legacy",
"version": "kibana"
}
24 changes: 24 additions & 0 deletions src/core/server/http/integration_tests/http_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { parse } from 'url';
import request from 'request';
import * as kbnTestServer from '../../../../test_utils/kbn_server';
import { Router } from '../router';

import { url as authUrl } from './__fixtures__/plugins/dummy_security/server/plugin';
import { url as onReqUrl } from './__fixtures__/plugins/dummy_on_request/server/plugin';

Expand Down Expand Up @@ -233,5 +234,28 @@ describe('http service', () => {
await kbnTestServer.request.get(root, legacyUrl).expect(200, reqBasePath);
});
});

describe('#registerRouter()', () => {
let root: ReturnType<typeof kbnTestServer.createRoot>;
beforeEach(async () => {
const plugin = path.resolve(__dirname, './__fixtures__/plugins/dummy_legacy');

root = kbnTestServer.createRoot(
{
plugins: { paths: [plugin] },
},
{
dev: true,
}
);
await root.setup();
await root.start();
}, 30000);
afterEach(async () => await root.shutdown());

it('Should support registering a route from legacy platform', async () => {
await kbnTestServer.request.get(root, '/hi').expect(200, { content: 'from-new-platform' });
});
});
});
});
8 changes: 4 additions & 4 deletions src/core/server/index.test.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*/

import { httpServiceMock } from './http/http_service.mock';
export const httpService = httpServiceMock.create();
export const mockHttpService = httpServiceMock.create();
jest.doMock('./http/http_service', () => ({
HttpService: jest.fn(() => httpService),
HttpService: jest.fn(() => mockHttpService),
}));

export const mockPluginsService = { setup: jest.fn(), start: jest.fn(), stop: jest.fn() };
Expand All @@ -29,9 +29,9 @@ jest.doMock('./plugins/plugins_service', () => ({
}));

import { elasticsearchServiceMock } from './elasticsearch/elasticsearch_service.mock';
export const elasticsearchService = elasticsearchServiceMock.create();
export const mockElasticsearchService = elasticsearchServiceMock.create();
jest.doMock('./elasticsearch/elasticsearch_service', () => ({
ElasticsearchService: jest.fn(() => elasticsearchService),
ElasticsearchService: jest.fn(() => mockElasticsearchService),
}));

export const mockLegacyService = { setup: jest.fn(), start: jest.fn(), stop: jest.fn() };
Expand Down
1 change: 0 additions & 1 deletion src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export interface CoreSetup {
}

export interface CoreStart {
http: HttpServiceStart;
plugins: PluginsServiceStart;
}

Expand Down
Loading