-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
rest.server.ts
874 lines (777 loc) · 25.8 KB
/
rest.server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
// Node module: @loopback/rest
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {Binding, Constructor, Context, inject} from '@loopback/context';
import {Application, CoreBindings, Server} from '@loopback/core';
import {HttpServer, HttpServerOptions} from '@loopback/http-server';
import {getControllerSpec} from '@loopback/openapi-v3';
import {
OpenApiSpec,
OperationObject,
ServerObject,
} from '@loopback/openapi-v3-types';
import {AssertionError} from 'assert';
import * as cors from 'cors';
import * as debugFactory from 'debug';
import * as express from 'express';
import {PathParams} from 'express-serve-static-core';
import {IncomingMessage, ServerResponse} from 'http';
import {ServerOptions} from 'https';
import {safeDump} from 'js-yaml';
import {ServeStaticOptions} from 'serve-static';
import {HttpHandler} from './http-handler';
import {RestBindings} from './keys';
import {QUERY_NOT_PARSED} from './parser';
import {RequestContext} from './request-context';
import {
ControllerClass,
ControllerFactory,
ControllerInstance,
ControllerRoute,
createControllerFactoryForBinding,
Route,
RouteEntry,
RoutingTable,
StaticAssetsRoute,
} from './router';
import {DefaultSequence, SequenceFunction, SequenceHandler} from './sequence';
import {
FindRoute,
InvokeMethod,
ParseParams,
Reject,
Request,
Response,
Send,
} from './types';
const debug = debugFactory('loopback:rest:server');
export type HttpRequestListener = (
req: IncomingMessage,
res: ServerResponse,
) => void;
export interface HttpServerLike {
requestHandler: HttpRequestListener;
}
const SequenceActions = RestBindings.SequenceActions;
// NOTE(bajtos) we cannot use `import * as cloneDeep from 'lodash/cloneDeep'
// because it produces the following TypeScript error:
// Module '"(...)/node_modules/@types/lodash/cloneDeep/index"' resolves to
// a non-module entity and cannot be imported using this construct.
const cloneDeep: <T>(value: T) => T = require('lodash/cloneDeep');
/**
* A REST API server for use with Loopback.
* Add this server to your application by importing the RestComponent.
* ```ts
* const app = new MyApplication();
* app.component(RestComponent);
* ```
*
* To add additional instances of RestServer to your application, use the
* `.server` function:
* ```ts
* app.server(RestServer, 'nameOfYourServer');
* ```
*
* By default, one instance of RestServer will be created when the RestComponent
* is bootstrapped. This instance can be retrieved with
* `app.getServer(RestServer)`, or by calling `app.get('servers.RestServer')`
* Note that retrieving other instances of RestServer must be done using the
* server's name:
* ```ts
* const server = await app.getServer('foo')
* // OR
* const server = await app.get('servers.foo');
* ```
*
* @export
* @class RestServer
* @extends {Context}
* @implements {Server}
*/
export class RestServer extends Context implements Server, HttpServerLike {
/**
* Handle incoming HTTP(S) request by invoking the corresponding
* Controller method via the configured Sequence.
*
* @example
*
* ```ts
* const app = new Application();
* app.component(RestComponent);
* // setup controllers, etc.
*
* const restServer = await app.getServer(RestServer);
* const httpServer = http.createServer(restServer.requestHandler);
* httpServer.listen(3000);
* ```
*
* @param req The request.
* @param res The response.
*/
public requestHandler: HttpRequestListener;
public readonly config: RestServerConfig;
protected _httpHandler: HttpHandler;
protected get httpHandler(): HttpHandler {
this._setupHandlerIfNeeded();
return this._httpHandler;
}
protected _httpServer: HttpServer | undefined;
protected _expressApp: express.Application;
get listening(): boolean {
return this._httpServer ? this._httpServer.listening : false;
}
get url(): string | undefined {
return this._httpServer && this._httpServer.url;
}
/**
* @memberof RestServer
* Creates an instance of RestServer.
*
* @param {Application} app The application instance (injected via
* CoreBindings.APPLICATION_INSTANCE).
* @param {RestServerConfig=} config The configuration options (injected via
* RestBindings.CONFIG).
*
*/
constructor(
@inject(CoreBindings.APPLICATION_INSTANCE) app: Application,
@inject(RestBindings.CONFIG, {optional: true})
config: RestServerConfig = {},
) {
super(app);
// Can't check falsiness, 0 is a valid port.
if (config.port == null) {
config.port = 3000;
}
if (config.host == null) {
// Set it to '' so that the http server will listen on all interfaces
config.host = undefined;
}
config.openApiSpec = config.openApiSpec || {};
config.openApiSpec.endpointMapping =
config.openApiSpec.endpointMapping || OPENAPI_SPEC_MAPPING;
config.apiExplorer = normalizeApiExplorerConfig(config.apiExplorer);
this.config = config;
this.bind(RestBindings.PORT).to(config.port);
this.bind(RestBindings.HOST).to(config.host);
this.bind(RestBindings.PROTOCOL).to(config.protocol || 'http');
this.bind(RestBindings.HTTPS_OPTIONS).to(config as ServerOptions);
if (config.sequence) {
this.sequence(config.sequence);
}
this._setupRequestHandler();
this.bind(RestBindings.HANDLER).toDynamicValue(() => this.httpHandler);
}
protected _setupRequestHandler() {
this._expressApp = express();
// Disable express' built-in query parser, we parse queries ourselves
// Note that when disabled, express sets query to an empty object,
// which makes it difficult for us to detect whether the query
// has been parsed or not. At the same time, we want `request.query`
// to remain as an object, because everybody in express ecosystem expects
// that property to be defined. A static singleton object to the rescue!
this._expressApp.set('query parser fn', (str: string) => QUERY_NOT_PARSED);
this.requestHandler = this._expressApp;
// Allow CORS support for all endpoints so that users
// can test with online SwaggerUI instance
const corsOptions = this.config.cors || {
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: false,
optionsSuccessStatus: 204,
maxAge: 86400,
credentials: true,
};
this._expressApp.use(cors(corsOptions));
// Set up endpoints for OpenAPI spec/ui
this._setupOpenApiSpecEndpoints();
// Mount our router & request handler
this._expressApp.use((req, res, next) => {
this._handleHttpRequest(req, res).catch(next);
});
// Mount our error handler
this._expressApp.use(
(err: Error, req: Request, res: Response, next: Function) => {
this._onUnhandledError(req, res, err);
},
);
}
/**
* Mount /openapi.json, /openapi.yaml for specs and /swagger-ui, /explorer
* to redirect to externally hosted API explorer
*/
protected _setupOpenApiSpecEndpoints() {
// NOTE(bajtos) Regular routes are handled through Sequence.
// IMO, this built-in endpoint should not run through a Sequence,
// because it's not part of the application API itself.
// E.g. if the app implements access/audit logs, I don't want
// this endpoint to trigger a log entry. If the server implements
// content-negotiation to support XML clients, I don't want the OpenAPI
// spec to be converted into an XML response.
const mapping = this.config.openApiSpec!.endpointMapping!;
// Serving OpenAPI spec
for (const p in mapping) {
this._expressApp.use(p, (req, res) =>
this._serveOpenApiSpec(req, res, mapping[p]),
);
}
const explorerPaths = ['/swagger-ui', '/explorer'];
this._expressApp.get(explorerPaths, (req, res, next) =>
this._redirectToSwaggerUI(req, res, next),
);
}
protected _handleHttpRequest(request: Request, response: Response) {
return this.httpHandler.handleRequest(request, response);
}
protected _setupHandlerIfNeeded() {
// TODO(bajtos) support hot-reloading of controllers
// after the app started. The idea is to rebuild the HttpHandler
// instance whenever a controller was added/deleted.
// See https://github.com/strongloop/loopback-next/issues/433
if (this._httpHandler) return;
/**
* Check if there is custom router in the context
*/
const router = this.getSync(RestBindings.ROUTER, {optional: true});
const routingTable = new RoutingTable(router, this._staticAssetRoute);
this._httpHandler = new HttpHandler(this, routingTable);
for (const b of this.find('controllers.*')) {
const controllerName = b.key.replace(/^controllers\./, '');
const ctor = b.valueConstructor;
if (!ctor) {
throw new Error(
`The controller ${controllerName} was not bound via .toClass()`,
);
}
const apiSpec = getControllerSpec(ctor);
if (!apiSpec) {
// controller methods are specified through app.api() spec
debug('Skipping controller %s - no API spec provided', controllerName);
continue;
}
debug('Registering controller %s', controllerName);
if (apiSpec.components && apiSpec.components.schemas) {
this._httpHandler.registerApiDefinitions(apiSpec.components.schemas);
}
const controllerFactory = createControllerFactoryForBinding(b.key);
this._httpHandler.registerController(apiSpec, ctor, controllerFactory);
}
for (const b of this.find('routes.*')) {
// TODO(bajtos) should we support routes defined asynchronously?
const route = this.getSync<RouteEntry>(b.key);
this._httpHandler.registerRoute(route);
}
// TODO(bajtos) should we support API spec defined asynchronously?
const spec: OpenApiSpec = this.getSync(RestBindings.API_SPEC);
for (const path in spec.paths) {
for (const verb in spec.paths[path]) {
const routeSpec: OperationObject = spec.paths[path][verb];
this._setupOperation(verb, path, routeSpec);
}
}
}
private _setupOperation(verb: string, path: string, spec: OperationObject) {
const handler = spec['x-operation'];
if (typeof handler === 'function') {
// Remove a field value that cannot be represented in JSON.
// Start by creating a shallow-copy of the spec, so that we don't
// modify the original spec object provided by user.
spec = Object.assign({}, spec);
delete spec['x-operation'];
const route = new Route(verb, path, spec, handler);
this._httpHandler.registerRoute(route);
return;
}
const controllerName = spec['x-controller-name'];
if (typeof controllerName === 'string') {
const b = this.find(`controllers.${controllerName}`)[0];
if (!b) {
throw new Error(
`Unknown controller ${controllerName} used by "${verb} ${path}"`,
);
}
const ctor = b.valueConstructor;
if (!ctor) {
throw new Error(
`The controller ${controllerName} was not bound via .toClass()`,
);
}
const controllerFactory = createControllerFactoryForBinding(b.key);
const route = new ControllerRoute(
verb,
path,
spec,
ctor,
controllerFactory,
);
this._httpHandler.registerRoute(route);
return;
}
throw new Error(
`There is no handler configured for operation "${verb} ${path}`,
);
}
private async _serveOpenApiSpec(
request: Request,
response: Response,
specForm?: OpenApiSpecForm,
) {
specForm = specForm || {version: '3.0.0', format: 'json'};
let specObj = this.getApiSpec();
if (this.config.openApiSpec!.setServersFromRequest) {
specObj = Object.assign({}, specObj);
specObj.servers = [{url: this._getUrlForClient(request)}];
}
if (specForm.format === 'json') {
const spec = JSON.stringify(specObj, null, 2);
response.setHeader('content-type', 'application/json; charset=utf-8');
response.end(spec, 'utf-8');
} else {
const yaml = safeDump(specObj, {});
response.setHeader('content-type', 'text/yaml; charset=utf-8');
response.end(yaml, 'utf-8');
}
}
/**
* Get the protocol for a request
* @param request Http request
*/
private _getProtocolForRequest(request: Request) {
return (
(request.get('x-forwarded-proto') || '').split(',')[0] ||
request.protocol ||
this.config.protocol ||
'http'
);
}
/**
* Parse the host:port string into an object for host and port
* @param host The host string
*/
private _parseHostAndPort(host: string | undefined) {
host = host || '';
host = host.split(',')[0];
const portPattern = /:([0-9]+)$/;
const port = (host.match(portPattern) || [])[1] || '';
host = host.replace(portPattern, '');
return {host, port};
}
/**
* Get the URL of the request sent by the client
* @param request Http request
*/
private _getUrlForClient(request: Request) {
const protocol = this._getProtocolForRequest(request);
// The host can be in one of the forms
// [::1]:3000
// [::1]
// 127.0.0.1:3000
// 127.0.0.1
let {host, port} = this._parseHostAndPort(
request.get('x-forwarded-host') || request.headers.host,
);
const forwardedPort = (request.get('x-forwarded-port') || '').split(',')[0];
port = forwardedPort || port;
if (!host) {
// No host detected from http headers. Use the configured values
host = this.config.host!;
port = this.config.port == null ? '' : this.config.port.toString();
}
// clear default ports
port = protocol === 'https' && port === '443' ? '' : port;
port = protocol === 'http' && port === '80' ? '' : port;
// add port number of present
host += port !== '' ? ':' + port : '';
return protocol + '://' + host;
}
private async _redirectToSwaggerUI(
request: Request,
response: Response,
next: express.NextFunction,
) {
const config = this.config.apiExplorer!;
if (config.disabled) {
debug('Redirect to swagger-ui was disabled by configuration.');
return next();
}
debug('Redirecting to swagger-ui from %j.', request.originalUrl);
const protocol = this._getProtocolForRequest(request);
const baseUrl = protocol === 'http' ? config.httpUrl : config.url;
const openApiUrl = `${this._getUrlForClient(request)}/openapi.json`;
const fullUrl = `${baseUrl}?url=${openApiUrl}`;
response.redirect(308, fullUrl);
}
/**
* Register a controller class with this server.
*
* @param {Constructor} controllerCtor The controller class
* (constructor function).
* @returns {Binding} The newly created binding, you can use the reference to
* further modify the binding, e.g. lock the value to prevent further
* modifications.
*
* ```ts
* class MyController {
* }
* app.controller(MyController).lock();
* ```
*
*/
controller(controllerCtor: ControllerClass<ControllerInstance>): Binding {
return this.bind('controllers.' + controllerCtor.name).toClass(
controllerCtor,
);
}
/**
* Register a new Controller-based route.
*
* ```ts
* class MyController {
* greet(name: string) {
* return `hello ${name}`;
* }
* }
* app.route('get', '/greet', operationSpec, MyController, 'greet');
* ```
*
* @param verb HTTP verb of the endpoint
* @param path URL path of the endpoint
* @param spec The OpenAPI spec describing the endpoint (operation)
* @param controllerCtor Controller constructor
* @param controllerFactory A factory function to create controller instance
* @param methodName The name of the controller method
*/
route<I>(
verb: string,
path: string,
spec: OperationObject,
controllerCtor: ControllerClass<I>,
controllerFactory: ControllerFactory<I>,
methodName: string,
): Binding;
/**
* Register a new route invoking a handler function.
*
* ```ts
* function greet(name: string) {
* return `hello ${name}`;
* }
* app.route('get', '/', operationSpec, greet);
* ```
*
* @param verb HTTP verb of the endpoint
* @param path URL path of the endpoint
* @param spec The OpenAPI spec describing the endpoint (operation)
* @param handler The function to invoke with the request parameters
* described in the spec.
*/
route(
verb: string,
path: string,
spec: OperationObject,
handler: Function,
): Binding;
/**
* Register a new generic route.
*
* ```ts
* function greet(name: string) {
* return `hello ${name}`;
* }
* const route = new Route('get', '/', operationSpec, greet);
* app.route(route);
* ```
*
* @param route The route to add.
*/
route(route: RouteEntry): Binding;
route<T>(
routeOrVerb: RouteEntry | string,
path?: string,
spec?: OperationObject,
controllerCtorOrHandler?: ControllerClass<T> | Function,
controllerFactory?: ControllerFactory<T>,
methodName?: string,
): Binding {
if (typeof routeOrVerb === 'object') {
const r = routeOrVerb;
// Encode the path to escape special chars
const encodedPath = encodeURIComponent(r.path).replace(/\./g, '%2E');
return this.bind(`routes.${r.verb} ${encodedPath}`)
.to(r)
.tag('route');
}
if (!path) {
throw new AssertionError({
message: 'path is required for a controller-based route',
});
}
if (!spec) {
throw new AssertionError({
message: 'spec is required for a controller-based route',
});
}
if (arguments.length === 4) {
if (!controllerCtorOrHandler) {
throw new AssertionError({
message: 'handler function is required for a handler-based route',
});
}
return this.route(
new Route(routeOrVerb, path, spec, controllerCtorOrHandler as Function),
);
}
if (!controllerCtorOrHandler) {
throw new AssertionError({
message: 'controller is required for a controller-based route',
});
}
if (!methodName) {
throw new AssertionError({
message: 'methodName is required for a controller-based route',
});
}
return this.route(
new ControllerRoute(
routeOrVerb,
path,
spec,
controllerCtorOrHandler as ControllerClass<T>,
controllerFactory,
methodName,
),
);
}
// The route for static assets
private _staticAssetRoute = new StaticAssetsRoute();
/**
* Mount static assets to the REST server.
* See https://expressjs.com/en/4x/api.html#express.static
* @param path The path(s) to serve the asset.
* See examples at https://expressjs.com/en/4x/api.html#path-examples
* @param rootDir The root directory from which to serve static assets
* @param options Options for serve-static
*/
static(path: PathParams, rootDir: string, options?: ServeStaticOptions) {
this._staticAssetRoute.registerAssets(path, rootDir, options);
}
/**
* Set the OpenAPI specification that defines the REST API schema for this
* server. All routes, parameter definitions and return types will be defined
* in this way.
*
* Note that this will override any routes defined via decorators at the
* controller level (this function takes precedent).
*
* @param {OpenApiSpec} spec The OpenAPI specification, as an object.
* @returns {Binding}
* @memberof RestServer
*/
api(spec: OpenApiSpec): Binding {
return this.bind(RestBindings.API_SPEC).to(spec);
}
/**
* Get the OpenAPI specification describing the REST API provided by
* this application.
*
* This method merges operations (HTTP endpoints) from the following sources:
* - `app.api(spec)`
* - `app.controller(MyController)`
* - `app.route(route)`
* - `app.route('get', '/greet', operationSpec, MyController, 'greet')`
*/
getApiSpec(): OpenApiSpec {
const spec = this.getSync<OpenApiSpec>(RestBindings.API_SPEC);
const defs = this.httpHandler.getApiDefinitions();
// Apply deep clone to prevent getApiSpec() callers from
// accidentally modifying our internal routing data
spec.paths = cloneDeep(this.httpHandler.describeApiPaths());
if (defs) {
spec.components = spec.components || {};
spec.components.schemas = cloneDeep(defs);
}
return spec;
}
/**
* Configure a custom sequence class for handling incoming requests.
*
* ```ts
* class MySequence implements SequenceHandler {
* constructor(
* @inject('send) public send: Send)) {
* }
*
* public async handle({response}: RequestContext) {
* send(response, 'hello world');
* }
* }
* ```
*
* @param value The sequence to invoke for each incoming request.
*/
public sequence(value: Constructor<SequenceHandler>) {
this.bind(RestBindings.SEQUENCE).toClass(value);
}
/**
* Configure a custom sequence function for handling incoming requests.
*
* ```ts
* app.handler(({request, response}, sequence) => {
* sequence.send(response, 'hello world');
* });
* ```
*
* @param handlerFn The handler to invoke for each incoming request.
*/
public handler(handlerFn: SequenceFunction) {
class SequenceFromFunction extends DefaultSequence {
// NOTE(bajtos) Unfortunately, we have to duplicate the constructor
// in order for our DI/IoC framework to inject constructor arguments
constructor(
@inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute,
@inject(SequenceActions.PARSE_PARAMS)
protected parseParams: ParseParams,
@inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod,
@inject(SequenceActions.SEND) public send: Send,
@inject(SequenceActions.REJECT) public reject: Reject,
) {
super(findRoute, parseParams, invoke, send, reject);
}
async handle(context: RequestContext): Promise<void> {
await Promise.resolve(handlerFn(context, this));
}
}
this.sequence(SequenceFromFunction);
}
/**
* Start this REST API's HTTP/HTTPS server.
*
* @returns {Promise<void>}
* @memberof RestServer
*/
async start(): Promise<void> {
// Setup the HTTP handler so that we can verify the configuration
// of API spec, controllers and routes at startup time.
this._setupHandlerIfNeeded();
const port = await this.get(RestBindings.PORT);
const host = await this.get(RestBindings.HOST);
const protocol = await this.get(RestBindings.PROTOCOL);
const httpsOptions = await this.get(RestBindings.HTTPS_OPTIONS);
const serverOptions = {};
if (protocol === 'https') Object.assign(serverOptions, httpsOptions);
Object.assign(serverOptions, {port, host, protocol});
this._httpServer = new HttpServer(this.requestHandler, serverOptions);
await this._httpServer.start();
this.bind(RestBindings.PORT).to(this._httpServer.port);
this.bind(RestBindings.HOST).to(this._httpServer.host);
this.bind(RestBindings.URL).to(this._httpServer.url);
debug('RestServer listening at %s', this._httpServer.url);
}
/**
* Stop this REST API's HTTP/HTTPS server.
*
* @returns {Promise<void>}
* @memberof RestServer
*/
async stop() {
// Kill the server instance.
if (!this._httpServer) return;
await this._httpServer.stop();
this._httpServer = undefined;
}
protected _onUnhandledError(req: Request, res: Response, err: Error) {
if (!res.headersSent) {
res.statusCode = 500;
res.end();
}
// It's the responsibility of the Sequence to handle any errors.
// If an unhandled error escaped, then something very wrong happened
// and it's best to crash the process immediately.
process.nextTick(() => {
throw err;
});
}
}
/**
* The form of OpenAPI specs to be served
*
* @interface OpenApiSpecForm
*/
export interface OpenApiSpecForm {
version?: string;
format?: string;
}
const OPENAPI_SPEC_MAPPING: {[key: string]: OpenApiSpecForm} = {
'/openapi.json': {version: '3.0.0', format: 'json'},
'/openapi.yaml': {version: '3.0.0', format: 'yaml'},
};
/**
* Options to customize how OpenAPI specs are served
*/
export interface OpenApiSpecOptions {
/**
* Mapping of urls to spec forms, by default:
* ```
* {
* '/openapi.json': {version: '3.0.0', format: 'json'},
* '/openapi.yaml': {version: '3.0.0', format: 'yaml'},
* }
* ```
*/
endpointMapping?: {[key: string]: OpenApiSpecForm};
/**
* A flag to force `servers` to be set from the http request for the OpenAPI
* spec
*/
setServersFromRequest?: boolean;
/**
* Configure servers for OpenAPI spec
*/
servers?: ServerObject[];
}
export interface ApiExplorerOptions {
/**
* URL for the hosted API explorer UI
* default to https://loopback.io/api-explorer
*/
url?: string;
/**
* URL for the API explorer served over `http` protocol to deal with mixed
* content security imposed by browsers as the spec is exposed over `http` by
* default.
* See https://github.com/strongloop/loopback-next/issues/1603
*/
httpUrl?: string;
/**
* Set this flag to disable the built-in redirect to externally
* hosted API Explorer UI.
*/
disabled?: true;
}
/**
* Options for RestServer configuration
*/
export interface RestServerOptions {
cors?: cors.CorsOptions;
openApiSpec?: OpenApiSpecOptions;
apiExplorer?: ApiExplorerOptions;
sequence?: Constructor<SequenceHandler>;
}
/**
* Valid configuration for the RestServer constructor.
*
* @export
* @interface RestServerConfig
*/
export type RestServerConfig = RestServerOptions & HttpServerOptions;
function normalizeApiExplorerConfig(
input: ApiExplorerOptions | undefined,
): ApiExplorerOptions {
const config = input || {};
const url = config.url || 'https://explorer.loopback.io';
config.httpUrl =
config.httpUrl || config.url || 'http://explorer.loopback.io';
config.url = url;
return config;
}