feat: Expose AppRunner.run for easily serving from JS apps

* feat: make methods in CliRunner public

* change: rename CliRunner to AppRunner

* fix: process being passed incorrectly to runCli

* feat: expose AppRunner.run for easily serving from JS apps

* change: only make run methods on AppRunner public
This commit is contained in:
Ruben Taelman
2021-04-28 09:59:10 +02:00
committed by GitHub
parent f4a09a414d
commit d1eadd75e7
5 changed files with 387 additions and 315 deletions

View File

@@ -17,7 +17,7 @@ export * from './authorization/WebAclAuthorizer';
// Init
export * from './init/AclInitializer';
export * from './init/CliRunner';
export * from './init/AppRunner';
export * from './init/ConfigPodInitializer';
export * from './init/Initializer';
export * from './init/LoggerInitializer';

View File

@@ -8,16 +8,32 @@ import { getLoggerFor } from '../logging/LogUtil';
import { absoluteFilePath, ensureTrailingSlash, joinFilePath } from '../util/PathUtil';
import type { Initializer } from './Initializer';
export class CliRunner {
export class AppRunner {
private readonly logger = getLoggerFor(this);
/**
* Generic run function for starting the server from a given config
* Generic run function for starting the server from JavaScript for a given config.
* @param loaderProperties - Components.js loader properties.
* @param configFile - Path to the server config file.
* @param variableParams - Variables to pass into the config file.
*/
public async run(
loaderProperties: IComponentsManagerBuilderOptions<Initializer>,
configFile: string,
variableParams: ConfigVariables,
): Promise<void> {
const variables = this.createVariables(variableParams);
const initializer = await this.createInitializer(loaderProperties, configFile, variables);
await initializer.handleSafe();
}
/**
* Generic run function for starting the server on the CLI from a given config
* Made run to be non-async to lower the chance of unhandled promise rejection errors in the future.
* @param args - Command line arguments.
* @param stderr - Standard error stream.
*/
public run({
public runCli({
argv = process.argv,
stderr = process.stderr,
}: {
@@ -103,7 +119,7 @@ export class CliRunner {
/**
* Translates command-line parameters into configuration variables
*/
protected createVariables(params: Record<string, any>): Record<string, any> {
protected createVariables(params: ConfigVariables): Record<string, any> {
return {
'urn:solid-server:default:variable:baseUrl':
params.baseUrl ? ensureTrailingSlash(params.baseUrl) : `http://localhost:${params.port}/`,
@@ -132,3 +148,12 @@ export class CliRunner {
return await componentsManager.instantiate(initializer, { variables });
}
}
export interface ConfigVariables {
loggingLevel: string;
port: number;
baseUrl?: string;
rootFilePath?: string;
sparqlEndpoint?: string;
podConfigJson?: string;
}