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

@ -4,17 +4,8 @@
{
"@id": "urn:solid-server:default",
"@type": "Setup",
"Setup:_serverFactory": {
"@id": "urn:solid-server:default:ServerFactory"
},
"Setup:_initializer": {
"@id": "urn:solid-server:default:Initializer"
},
"Setup:_base": {
"@id": "urn:solid-server:default:variable:baseUrl"
},
"Setup:_port": {
"@id": "urn:solid-server:default:variable:port"
}
},
{
@ -38,6 +29,15 @@
"AclInitializer:_aclManager": {
"@id": "urn:solid-server:default:AclManager"
}
},
{
"@type": "ServerInitializer",
"ServerInitializer:_serverFactory": {
"@id": "urn:solid-server:default:ServerFactory"
},
"ServerInitializer:_port": {
"@id": "urn:solid-server:default:variable:port"
}
}
]
}

View File

@ -17,6 +17,7 @@ export * from './init/AclInitializer';
export * from './init/CliRunner';
export * from './init/Initializer';
export * from './init/LoggerInitializer';
export * from './init/ServerInitializer';
export * from './init/Setup';
// LDP/HTTP/Metadata

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

View File

@ -16,6 +16,7 @@ export class ExpressHttpServerFactory implements HttpServerFactory {
}
public startServer(port: number): Server {
this.logger.info(`Starting server at http://localhost:${port}/`);
return this.createApp().listen(port);
}

View File

@ -0,0 +1,18 @@
import { ServerInitializer } from '../../../src/init/ServerInitializer';
import type { HttpServerFactory } from '../../../src/server/HttpServerFactory';
describe('ServerInitializer', (): void => {
const serverFactory: jest.Mocked<HttpServerFactory> = {
startServer: jest.fn(),
};
let initializer: ServerInitializer;
beforeAll(async(): Promise<void> => {
initializer = new ServerInitializer(serverFactory, 3000);
});
it('starts an HTTP server.', async(): Promise<void> => {
await initializer.handle();
expect(serverFactory.startServer).toHaveBeenCalledWith(3000);
});
});

View File

@ -1,24 +1,16 @@
import type { Initializer } from '../../../src/init/Initializer';
import { Setup } from '../../../src/init/Setup';
import type { HttpServerFactory } from '../../../src/server/HttpServerFactory';
describe('Setup', (): void => {
const serverFactory: jest.Mocked<HttpServerFactory> = {
startServer: jest.fn(),
};
const initializer: jest.Mocked<Initializer> = {
handleSafe: jest.fn(),
} as any;
beforeAll(async(): Promise<void> => {
const setup = new Setup(initializer, serverFactory, 'http://localhost:3000/', 3000);
const setup = new Setup(initializer);
await setup.setup();
});
it('starts an HTTP server.', async(): Promise<void> => {
expect(serverFactory.startServer).toHaveBeenCalledWith(3000);
});
it('calls the initializer.', async(): Promise<void> => {
expect(initializer.handleSafe).toHaveBeenCalledTimes(1);
});