feat: Split up server creation and request handling

This allows us to decouple the WebSocket listening from the HTTP configs,
making these features completely orthogonal.
This commit is contained in:
Joachim Van Herwegen
2022-09-29 16:34:38 +02:00
parent 764ce3cc28
commit 4223dcf8a4
64 changed files with 949 additions and 694 deletions

View File

@@ -1,5 +1,8 @@
import type { Server } from 'http';
import { URL } from 'url';
import { promisify } from 'util';
import { getLoggerFor } from '../logging/LogUtil';
import { isHttpsServer } from '../server/HttpServerFactory';
import type { HttpServerFactory } from '../server/HttpServerFactory';
import type { Finalizable } from './final/Finalizable';
import { Initializer } from './Initializer';
@@ -8,6 +11,8 @@ import { Initializer } from './Initializer';
* Creates and starts an HTTP server.
*/
export class ServerInitializer extends Initializer implements Finalizable {
protected readonly logger = getLoggerFor(this);
private readonly serverFactory: HttpServerFactory;
private readonly port: number;
@@ -20,7 +25,11 @@ export class ServerInitializer extends Initializer implements Finalizable {
}
public async handle(): Promise<void> {
this.server = this.serverFactory.startServer(this.port);
this.server = await this.serverFactory.createServer();
const url = new URL(`http${isHttpsServer(this.server) ? 's' : ''}://localhost:${this.port}/`).href;
this.logger.info(`Listening to server at ${url}`);
this.server.listen(this.port);
}
public async finalize(): Promise<void> {