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

New Platform and Legacy platform servers integration #39047

Merged
merged 12 commits into from
Jun 19, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

## HttpServiceStart.isListening property

Indicates if http server is listening on a port
Indicates if http server is listening on a given port

<b>Signature:</b>

```typescript
isListening: () => boolean;
isListening: (port: number) => boolean;
```
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ export interface HttpServiceStart

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

This file was deleted.

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

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

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/,
mshustov marked this conversation as resolved.
Show resolved Hide resolved
});
}

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
4 changes: 2 additions & 2 deletions src/core/server/http/http_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ test('creates and sets up second http server', async () => {
const cfg = { port: 2345 };
await serverSetup.createNewServer(cfg);
const server = await service.start();
expect(server.isListening()).toBeTruthy();
expect(server.isListening(9222)).toBeTruthy();
mshustov marked this conversation as resolved.
Show resolved Hide resolved
mshustov marked this conversation as resolved.
Show resolved Hide resolved
expect(server.isListening(cfg.port)).toBeTruthy();

try {
Expand All @@ -112,7 +112,7 @@ test('creates and sets up second http server', async () => {
expect(err.message).toBe('port must be defined');
}
await service.stop();
expect(server.isListening()).toBeFalsy();
expect(server.isListening(9222)).toBeFalsy();
expect(server.isListening(cfg.port)).toBeFalsy();
});

Expand Down
29 changes: 19 additions & 10 deletions src/core/server/http/http_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export interface HttpServiceSetup extends HttpServerSetup {
}
/** @public */
export interface HttpServiceStart {
/** Indicates if http server is listening on a port */
isListening: () => boolean;
/** Indicates if http server is listening on a given port */
isListening: (port: number) => boolean;
}

/** @internal */
Expand Down Expand Up @@ -78,19 +78,16 @@ export class HttpService implements CoreService<HttpServiceSetup, HttpServiceSta
const httpSetup = (this.httpServer.setup(config) || {}) as HttpServiceSetup;
const setup = {
...httpSetup,
...{ createNewServer: this.createServer.bind(this) },
...{
mshustov marked this conversation as resolved.
Show resolved Hide resolved
createNewServer: this.createServer.bind(this),
},
};
return setup;
}

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 @@ -102,14 +99,26 @@ export class HttpService implements CoreService<HttpServiceSetup, HttpServiceSta
}

return {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: do we consume this return value anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the core doesn't use it anymore. @toddself are you going to use isListening somewhere?

isListening: (port = 0) => {
isListening: (port: number) => {
const server = this.secondaryServers.get(port);
if (server) return server.isListening();
return this.httpServer.isListening();
},
};
}

/**
* Indicates if http server has configured to start listening on a configured port.
* 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.
* @internal
* */
private shouldListen(config: HttpConfig) {
return !this.coreContext.env.isDevClusterMaster && config.autoListen;
}

private async createServer(cfg: Partial<HttpConfig>) {
const { port } = cfg;
const config = await this.config$.pipe(first()).toPromise();
Expand Down
24 changes: 13 additions & 11 deletions src/core/server/http/integration_tests/http_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('http service', () => {
.get(root, legacyUrl)
.expect(200, 'ok from legacy server');

expect(response.header['set-cookie']).toBe(undefined);
expect(response.header['set-cookie']).toHaveLength(1);
mshustov marked this conversation as resolved.
Show resolved Hide resolved
});

it('Should pass associated auth state to Legacy platform', async () => {
Expand Down Expand Up @@ -106,7 +106,7 @@ describe('http service', () => {
expect(response.body.state).toEqual(user);
expect(response.body.status).toEqual('authenticated');

expect(response.header['set-cookie']).toBe(undefined);
expect(response.header['set-cookie']).toHaveLength(1);
});
});

Expand All @@ -118,7 +118,7 @@ describe('http service', () => {
afterEach(async () => await root.shutdown());

it('Should support passing request through to the route handler', async () => {
const router = new Router('');
mshustov marked this conversation as resolved.
Show resolved Hide resolved
const router = new Router('/new-platform');
router.get({ path: '/', validate: false }, async (req, res) => res.ok({ content: 'ok' }));

const { http } = await root.setup();
Expand All @@ -130,7 +130,7 @@ describe('http service', () => {
http.registerRouter(router);
await root.start();

await kbnTestServer.request.get(root, '/').expect(200, { content: 'ok' });
await kbnTestServer.request.get(root, '/new-platform/').expect(200, { content: 'ok' });
});

it('Should support redirecting to configured url', async () => {
Expand All @@ -139,7 +139,7 @@ describe('http service', () => {
http.registerOnPostAuth(async (req, t) => t.redirected(redirectTo));
await root.start();

const response = await kbnTestServer.request.get(root, '/').expect(302);
const response = await kbnTestServer.request.get(root, '/new-platform/').expect(302);
expect(response.header.location).toBe(redirectTo);
});

Expand All @@ -151,7 +151,7 @@ describe('http service', () => {
await root.start();

await kbnTestServer.request
.get(root, '/')
.get(root, '/new-platform/')
.expect(400, { statusCode: 400, error: 'Bad Request', message: 'unexpected error' });
});

Expand All @@ -162,7 +162,7 @@ describe('http service', () => {
});
await root.start();

await kbnTestServer.request.get(root, '/').expect({
await kbnTestServer.request.get(root, '/new-platform/').expect({
statusCode: 500,
error: 'Internal Server Error',
message: 'An internal server error occurred',
Expand All @@ -183,15 +183,17 @@ describe('http service', () => {
}
return t.next();
});
const router = new Router('');
const router = new Router('/new-platform');
router.get({ path: '/', validate: false }, async (req, res) =>
// @ts-ignore. don't complain customField is not defined on Request type
res.ok({ customField: String(req.customField) })
);
http.registerRouter(router);
await root.start();

await kbnTestServer.request.get(root, '/').expect(200, { customField: 'undefined' });
await kbnTestServer.request
.get(root, '/new-platform/')
.expect(200, { customField: 'undefined' });
});
});

Expand All @@ -205,10 +207,10 @@ describe('http service', () => {
it('supports Url change on the flight', async () => {
const { http } = await root.setup();
http.registerOnPreAuth((req, t) => {
return t.redirected('/new-url', { forward: true });
return t.redirected('/new-platform/new-url', { forward: true });
});

const router = new Router('/');
const router = new Router('/new-platform');
router.get({ path: '/new-url', validate: false }, async (req, res) =>
res.ok({ key: 'new-url-reached' })
);
Expand Down
12 changes: 6 additions & 6 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),
}));

import { pluginServiceMock } from './plugins/plugins_service.mock';
Expand All @@ -30,9 +30,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 All @@ -41,7 +41,7 @@ jest.mock('./legacy/legacy_service', () => ({
}));

import { configServiceMock } from './config/config_service.mock';
export const configService = configServiceMock.create();
export const mockConfigService = configServiceMock.create();
jest.doMock('./config/config_service', () => ({
ConfigService: jest.fn(() => configService),
ConfigService: jest.fn(() => mockConfigService),
}));
2 changes: 1 addition & 1 deletion src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export interface InternalCoreSetup {
* @public
*/
export interface InternalCoreStart {
http: HttpServiceStart;
// http: HttpServiceStart;
mshustov marked this conversation as resolved.
Show resolved Hide resolved
plugins: PluginsServiceStart;
}

Expand Down
41 changes: 0 additions & 41 deletions src/core/server/legacy/__snapshots__/legacy_service.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading