Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mshustov committed Jun 16, 2019
1 parent 5d90300 commit fa343d4
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 27 deletions.
2 changes: 2 additions & 0 deletions src/core/server/http/http_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ const createSetupContractMock = () => {
get: jest.fn(),
isAuthenticated: jest.fn(),
},
shouldListen: jest.fn(),
createNewServer: async (cfg: Partial<HttpConfig>): Promise<HttpServerSetup> =>
({} as HttpServerSetup),
};
setupContract.shouldListen.mockReturnValue(true);
return setupContract;
};

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

const service = new HttpService({ configService, env, logger });
const { createNewServer, ...setupHttpServer } = await service.setup();
const { createNewServer, shouldListen, ...setupHttpServer } = await service.setup();
expect(createNewServer).toBeDefined();
expect(shouldListen).toBeDefined();
expect(setupHttpServer).toEqual(httpServer);
});

Expand Down
13 changes: 11 additions & 2 deletions src/core/server/http/http_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { HttpsRedirectServer } from './https_redirect_server';
/** @public */
export interface HttpServiceSetup extends HttpServerSetup {
createNewServer: (cfg: Partial<HttpConfig>) => Promise<HttpServerSetup>;
shouldListen: () => boolean;
}
/** @public */
export interface HttpServiceStart {
Expand Down Expand Up @@ -78,7 +79,11 @@ export class HttpService implements CoreService<HttpServiceSetup, HttpServiceSta
const httpSetup = (this.httpServer.setup(config) || {}) as HttpServiceSetup;
const setup = {
...httpSetup,
...{ createNewServer: this.createServer.bind(this) },
...{
createNewServer: this.createServer.bind(this),
// TODO make consistent. here we bind, below we read fresh config.
shouldListen: this.shouldListen.bind(this, config),
},
};
return setup;
}
Expand All @@ -90,7 +95,7 @@ export class HttpService implements CoreService<HttpServiceSetup, HttpServiceSta
// 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 @@ -110,6 +115,10 @@ export class HttpService implements CoreService<HttpServiceSetup, HttpServiceSta
};
}

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);
});

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('');
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
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;
plugins: PluginsServiceStart;
}

Expand Down
20 changes: 13 additions & 7 deletions src/core/server/legacy/legacy_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import { BehaviorSubject, Subject, throwError } from 'rxjs';
// import { merge } from 'lodash';

jest.mock('./legacy_platform_proxy');
jest.mock('../../../legacy/server/kbn_server');
Expand Down Expand Up @@ -75,6 +76,7 @@ beforeEach(() => {
http: {
options: { someOption: 'foo', someAnotherOption: 'bar' },
server: { listener: { addListener: jest.fn() }, route: jest.fn() },
shouldListen: () => true,
},
plugins: {
contracts: new Map([['plugin-id', 'plugin-value']]),
Expand Down Expand Up @@ -339,14 +341,16 @@ describe('once LegacyService is set up without connection info', () => {
const disabledHttpStartDeps = {
core: {
http: {
isListening: () => false,
// isListening: () => false,
},
plugins: { contracts: new Map() },
},
plugins: {},
};
let legacyService: LegacyService;
beforeEach(async () => {
setupDeps.core.http.shouldListen = () => false;

legacyService = new LegacyService({ env, logger, configService: configService as any });

await legacyService.setup(setupDeps);
Expand Down Expand Up @@ -382,6 +386,8 @@ describe('once LegacyService is set up without connection info', () => {

describe('once LegacyService is set up in `devClusterMaster` mode', () => {
beforeEach(() => {
// TODO fix me shouldnt mutate
setupDeps.core.http.shouldListen = () => false;
configService.atPath.mockImplementation(path => {
return new BehaviorSubject(
path === 'dev' ? { basePathProxyTargetPort: 100500 } : { basePath: '/abc' }
Expand All @@ -404,9 +410,9 @@ describe('once LegacyService is set up in `devClusterMaster` mode', () => {
await devClusterLegacyService.setup(setupDeps);
await devClusterLegacyService.start({
core: {
http: {
isListening: () => false,
},
// http: {
// isListening: () => false,
// },
plugins: { contracts: new Map() },
},
plugins: {},
Expand All @@ -432,9 +438,9 @@ describe('once LegacyService is set up in `devClusterMaster` mode', () => {
await devClusterLegacyService.setup(setupDeps);
await devClusterLegacyService.start({
core: {
http: {
isListening: () => false,
},
// http: {
// isListening: () => false,
// },
plugins: { contracts: new Map() },
},
plugins: {},
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/legacy/legacy_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class LegacyService implements CoreService {
// bridge with the "legacy" Kibana. If server isn't run (e.g. if process is
// managed by ClusterManager or optimizer) then we won't have that info,
// so we can't start "legacy" server either.
serverOptions: startDeps.core.http.isListening()
serverOptions: setupDeps.core.http.shouldListen()
? {
...setupDeps.core.http.options,
listener: this.setupProxyListener(setupDeps.core.http.server),
Expand Down
6 changes: 4 additions & 2 deletions src/core/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ export class Server {
}

public async start() {
const httpStart = await this.http.start();
// const httpStart =
const pluginsStart = await this.plugins.start({});

const coreStart = {
http: httpStart,
// http: httpStart,
plugins: pluginsStart,
};

Expand All @@ -96,6 +96,8 @@ export class Server {
plugins: mapToObject(pluginsStart.contracts),
});

await this.http.start();

return coreStart;
}

Expand Down
4 changes: 2 additions & 2 deletions src/legacy/server/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import { format } from 'url';
import { resolve } from 'path';
import _ from 'lodash';
import Boom from 'boom';
import Hapi from 'hapi';
// import Hapi from 'hapi';
import { setupVersionCheck } from './version_check';
import { registerHapiPlugins } from './register_hapi_plugins';
import { setupBasePathProvider } from './setup_base_path_provider';
import { setupXsrf } from './xsrf';

export default async function (kbnServer, server, config) {
kbnServer.server = new Hapi.Server(kbnServer.newPlatform.params.serverOptions);
kbnServer.server = kbnServer.newPlatform.setup.core.http.server;//new Hapi.Server(kbnServer.newPlatform.params.serverOptions);
server = kbnServer.server;

setupBasePathProvider(kbnServer);
Expand Down

0 comments on commit fa343d4

Please sign in to comment.