Skip to content

Commit

Permalink
feat(rest): make sure rest options are passed to http-server
Browse files Browse the repository at this point in the history
Fixes #6170

Signed-off-by: Raymond Feng <enjoyjava@gmail.com>
  • Loading branch information
raymondfeng committed Aug 24, 2020
1 parent ffde907 commit e9af196
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
11 changes: 6 additions & 5 deletions packages/http-server/src/http-server.ts
Expand Up @@ -76,7 +76,7 @@ export class HttpServer {
private requestListener: RequestListener;
readonly server: http.Server | https.Server;
private _stoppable: stoppable.StoppableServer;
private serverOptions: HttpServerOptions;
readonly serverOptions: HttpServerOptions;

/**
* @param requestListener
Expand All @@ -88,10 +88,11 @@ export class HttpServer {
) {
debug('Http server options', serverOptions);
this.requestListener = requestListener;
this.serverOptions = Object.assign(
{port: 0, host: undefined},
serverOptions,
);
this.serverOptions = {
port: 0,
host: undefined,
...serverOptions,
};
if (this.serverOptions.path) {
debug('Http server with IPC path %s', this.serverOptions.path);
const ipcPath = this.serverOptions.path;
Expand Down
18 changes: 17 additions & 1 deletion packages/rest/src/__tests__/unit/rest.server/rest.server.unit.ts
Expand Up @@ -3,7 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Context, Application} from '@loopback/core';
import {Application, Context} from '@loopback/core';
import {anOperationSpec} from '@loopback/openapi-spec-builder';
import {expect} from '@loopback/testlab';
import {
Expand Down Expand Up @@ -94,6 +94,22 @@ describe('RestServer', () => {
expect(server.getSync(RestBindings.PATH)).to.equal(path);
});

it('honors gracePeriodForClose', async () => {
const app = new Application({
rest: {gracePeriodForClose: 1000},
});
app.component(RestComponent);
const server = await app.getServer(RestServer);
await server.start();
try {
expect(server.httpServer?.serverOptions.gracePeriodForClose).to.eql(
1000,
);
} finally {
await server.stop();
}
});

it('honors basePath in config', async () => {
const app = new Application({
rest: {port: 0, basePath: '/api'},
Expand Down
9 changes: 5 additions & 4 deletions packages/rest/src/rest.server.ts
Expand Up @@ -170,6 +170,10 @@ export class RestServer extends BaseMiddlewareRegistry
return this._httpServer ? this._httpServer.listening : false;
}

get httpServer(): HttpServer | undefined {
return this._httpServer;
}

/**
* The base url for the server, including the basePath if set. For example,
* the value will be 'http://localhost:3000/api' if `basePath` is set to
Expand Down Expand Up @@ -978,10 +982,7 @@ export class RestServer extends BaseMiddlewareRegistry
return;
}

const serverOptions = {};
if (protocol === 'https') Object.assign(serverOptions, httpsOptions);
Object.assign(serverOptions, {port, host, protocol, path});

const serverOptions = {...httpsOptions, port, host, protocol, path};
this._httpServer = new HttpServer(this.requestHandler, serverOptions);

await this._httpServer.start();
Expand Down

0 comments on commit e9af196

Please sign in to comment.