refactor: Split off ServerInitializer.

This commit is contained in:
Ruben Verborgh
2020-12-07 22:33:37 +01:00
committed by Joachim Van Herwegen
parent b0ecf1c1d8
commit 04a91858c2
8 changed files with 55 additions and 48 deletions

View File

@@ -3,12 +3,9 @@ import type { ReadStream, WriteStream } from 'tty';
import type { LoaderProperties } from 'componentsjs';
import { Loader } from 'componentsjs';
import yargs from 'yargs';
import { getLoggerFor } from '../logging/LogUtil';
import { ensureTrailingSlash } from '../util/PathUtil';
import type { Setup } from './Setup';
const logger = getLoggerFor('CliRunner');
/**
* Generic run function for starting the server from a given config
* @param args - Command line arguments.
@@ -41,7 +38,7 @@ export const runCli = function({
})
.help();
(async(): Promise<string> => {
(async(): Promise<void> => {
// Load provided or default config file
const configPath = params.config ?
path.join(process.cwd(), params.config) :
@@ -63,10 +60,8 @@ export const runCli = function({
params.podTemplateFolder ?? path.join(__dirname, '../../templates'),
},
}) as Setup;
return await setup.setup();
})().then((baseUrl: string): void => {
logger.info(`Running at ${baseUrl}`);
}).catch((error): void => {
await setup.setup();
})().catch((error): void => {
// This is the only time we can *not* use the logger to print error messages, as dependency injection has failed.
stderr.write(`${error}\n`);
});

View File

@@ -0,0 +1,20 @@
import type { HttpServerFactory } from '../server/HttpServerFactory';
import { Initializer } from './Initializer';
/**
* Creates and starts an HTTP server.
*/
export class ServerInitializer extends Initializer {
private readonly serverFactory: HttpServerFactory;
private readonly port: number;
public constructor(serverFactory: HttpServerFactory, port: number) {
super();
this.serverFactory = serverFactory;
this.port = port;
}
public async handle(): Promise<void> {
this.serverFactory.startServer(this.port);
}
}

View File

@@ -1,33 +1,13 @@
import type { HttpServerFactory } from '../server/HttpServerFactory';
import type { Initializer } from './Initializer';
/**
* Invokes all logic to setup a server.
*/
export class Setup {
private readonly serverFactory: HttpServerFactory;
private readonly initializer: Initializer;
private readonly base: string;
private readonly port: number;
public constructor(
initializer: Initializer,
serverFactory: HttpServerFactory,
base: string,
port: number,
) {
public constructor(initializer: Initializer) {
this.initializer = initializer;
this.serverFactory = serverFactory;
this.base = base;
this.port = port;
}
/**
* Set up a server.
*/
public async setup(): Promise<string> {
public async setup(): Promise<void> {
await this.initializer.handleSafe();
this.serverFactory.startServer(this.port);
return this.base;
}
}