mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
refactor: Split off ServerInitializer.
This commit is contained in:
parent
b0ecf1c1d8
commit
04a91858c2
@ -4,17 +4,8 @@
|
|||||||
{
|
{
|
||||||
"@id": "urn:solid-server:default",
|
"@id": "urn:solid-server:default",
|
||||||
"@type": "Setup",
|
"@type": "Setup",
|
||||||
"Setup:_serverFactory": {
|
|
||||||
"@id": "urn:solid-server:default:ServerFactory"
|
|
||||||
},
|
|
||||||
"Setup:_initializer": {
|
"Setup:_initializer": {
|
||||||
"@id": "urn:solid-server:default: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": {
|
"AclInitializer:_aclManager": {
|
||||||
"@id": "urn:solid-server:default: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"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ export * from './init/AclInitializer';
|
|||||||
export * from './init/CliRunner';
|
export * from './init/CliRunner';
|
||||||
export * from './init/Initializer';
|
export * from './init/Initializer';
|
||||||
export * from './init/LoggerInitializer';
|
export * from './init/LoggerInitializer';
|
||||||
|
export * from './init/ServerInitializer';
|
||||||
export * from './init/Setup';
|
export * from './init/Setup';
|
||||||
|
|
||||||
// LDP/HTTP/Metadata
|
// LDP/HTTP/Metadata
|
||||||
|
@ -3,12 +3,9 @@ import type { ReadStream, WriteStream } from 'tty';
|
|||||||
import type { LoaderProperties } from 'componentsjs';
|
import type { LoaderProperties } from 'componentsjs';
|
||||||
import { Loader } from 'componentsjs';
|
import { Loader } from 'componentsjs';
|
||||||
import yargs from 'yargs';
|
import yargs from 'yargs';
|
||||||
import { getLoggerFor } from '../logging/LogUtil';
|
|
||||||
import { ensureTrailingSlash } from '../util/PathUtil';
|
import { ensureTrailingSlash } from '../util/PathUtil';
|
||||||
import type { Setup } from './Setup';
|
import type { Setup } from './Setup';
|
||||||
|
|
||||||
const logger = getLoggerFor('CliRunner');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generic run function for starting the server from a given config
|
* Generic run function for starting the server from a given config
|
||||||
* @param args - Command line arguments.
|
* @param args - Command line arguments.
|
||||||
@ -41,7 +38,7 @@ export const runCli = function({
|
|||||||
})
|
})
|
||||||
.help();
|
.help();
|
||||||
|
|
||||||
(async(): Promise<string> => {
|
(async(): Promise<void> => {
|
||||||
// Load provided or default config file
|
// Load provided or default config file
|
||||||
const configPath = params.config ?
|
const configPath = params.config ?
|
||||||
path.join(process.cwd(), params.config) :
|
path.join(process.cwd(), params.config) :
|
||||||
@ -63,10 +60,8 @@ export const runCli = function({
|
|||||||
params.podTemplateFolder ?? path.join(__dirname, '../../templates'),
|
params.podTemplateFolder ?? path.join(__dirname, '../../templates'),
|
||||||
},
|
},
|
||||||
}) as Setup;
|
}) as Setup;
|
||||||
return await setup.setup();
|
await setup.setup();
|
||||||
})().then((baseUrl: string): void => {
|
})().catch((error): void => {
|
||||||
logger.info(`Running at ${baseUrl}`);
|
|
||||||
}).catch((error): void => {
|
|
||||||
// This is the only time we can *not* use the logger to print error messages, as dependency injection has failed.
|
// This is the only time we can *not* use the logger to print error messages, as dependency injection has failed.
|
||||||
stderr.write(`${error}\n`);
|
stderr.write(`${error}\n`);
|
||||||
});
|
});
|
||||||
|
20
src/init/ServerInitializer.ts
Normal file
20
src/init/ServerInitializer.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,33 +1,13 @@
|
|||||||
import type { HttpServerFactory } from '../server/HttpServerFactory';
|
|
||||||
import type { Initializer } from './Initializer';
|
import type { Initializer } from './Initializer';
|
||||||
|
|
||||||
/**
|
|
||||||
* Invokes all logic to setup a server.
|
|
||||||
*/
|
|
||||||
export class Setup {
|
export class Setup {
|
||||||
private readonly serverFactory: HttpServerFactory;
|
|
||||||
private readonly initializer: Initializer;
|
private readonly initializer: Initializer;
|
||||||
private readonly base: string;
|
|
||||||
private readonly port: number;
|
|
||||||
|
|
||||||
public constructor(
|
public constructor(initializer: Initializer) {
|
||||||
initializer: Initializer,
|
|
||||||
serverFactory: HttpServerFactory,
|
|
||||||
base: string,
|
|
||||||
port: number,
|
|
||||||
) {
|
|
||||||
this.initializer = initializer;
|
this.initializer = initializer;
|
||||||
this.serverFactory = serverFactory;
|
|
||||||
this.base = base;
|
|
||||||
this.port = port;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public async setup(): Promise<void> {
|
||||||
* Set up a server.
|
|
||||||
*/
|
|
||||||
public async setup(): Promise<string> {
|
|
||||||
await this.initializer.handleSafe();
|
await this.initializer.handleSafe();
|
||||||
this.serverFactory.startServer(this.port);
|
|
||||||
return this.base;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ export class ExpressHttpServerFactory implements HttpServerFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public startServer(port: number): Server {
|
public startServer(port: number): Server {
|
||||||
|
this.logger.info(`Starting server at http://localhost:${port}/`);
|
||||||
return this.createApp().listen(port);
|
return this.createApp().listen(port);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
18
test/unit/init/ServerInitializer.test.ts
Normal file
18
test/unit/init/ServerInitializer.test.ts
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
@ -1,24 +1,16 @@
|
|||||||
import type { Initializer } from '../../../src/init/Initializer';
|
import type { Initializer } from '../../../src/init/Initializer';
|
||||||
import { Setup } from '../../../src/init/Setup';
|
import { Setup } from '../../../src/init/Setup';
|
||||||
import type { HttpServerFactory } from '../../../src/server/HttpServerFactory';
|
|
||||||
|
|
||||||
describe('Setup', (): void => {
|
describe('Setup', (): void => {
|
||||||
const serverFactory: jest.Mocked<HttpServerFactory> = {
|
|
||||||
startServer: jest.fn(),
|
|
||||||
};
|
|
||||||
const initializer: jest.Mocked<Initializer> = {
|
const initializer: jest.Mocked<Initializer> = {
|
||||||
handleSafe: jest.fn(),
|
handleSafe: jest.fn(),
|
||||||
} as any;
|
} as any;
|
||||||
|
|
||||||
beforeAll(async(): Promise<void> => {
|
beforeAll(async(): Promise<void> => {
|
||||||
const setup = new Setup(initializer, serverFactory, 'http://localhost:3000/', 3000);
|
const setup = new Setup(initializer);
|
||||||
await setup.setup();
|
await setup.setup();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('starts an HTTP server.', async(): Promise<void> => {
|
|
||||||
expect(serverFactory.startServer).toHaveBeenCalledWith(3000);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('calls the initializer.', async(): Promise<void> => {
|
it('calls the initializer.', async(): Promise<void> => {
|
||||||
expect(initializer.handleSafe).toHaveBeenCalledTimes(1);
|
expect(initializer.handleSafe).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user