Skip to content

Commit

Permalink
Merge 41694a1 into a3f10bc
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenVerborgh committed Jul 26, 2021
2 parents a3f10bc + 41694a1 commit bbea5e5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 55 deletions.
4 changes: 0 additions & 4 deletions config/http/static/default.json
Expand Up @@ -21,10 +21,6 @@
{
"StaticAssetHandler:_assets_key": "/.well_known/css/images/",
"StaticAssetHandler:_assets_value": "$PACKAGE_ROOT/templates/images/"
},
{
"StaticAssetHandler:_assets_key": "/idp/style.css",
"StaticAssetHandler:_assets_value": "$PACKAGE_ROOT/templates/identity/email-password/main.css"
}
]
}
Expand Down
98 changes: 47 additions & 51 deletions src/init/AppRunner.ts
Expand Up @@ -8,42 +8,38 @@ import { getLoggerFor } from '../logging/LogUtil';
import { absoluteFilePath, ensureTrailingSlash, joinFilePath } from '../util/PathUtil';
import type { App } from './App';

export interface CliParams {
loggingLevel: string;
port: number;
baseUrl?: string;
rootFilePath?: string;
sparqlEndpoint?: string;
showStackTrace?: boolean;
podConfigJson?: string;
}

export class AppRunner {
private readonly logger = getLoggerFor(this);

/**
* Generic function for getting the app object to start 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 getApp(
loaderProperties: IComponentsManagerBuilderOptions<App>,
configFile: string,
variableParams: ConfigVariables,
): Promise<App> {
const variables = this.createVariables(variableParams);
return this.createApp(loaderProperties, configFile, variables);
}

/**
* Generic run function for starting the server from JavaScript for a given config.
* Starts the server with a given config.
* This method can be used to start the server from within another JavaScript application.
* @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<App>,
configFile: string,
variableParams: ConfigVariables,
variableParams: CliParams,
): Promise<void> {
const app = await this.getApp(loaderProperties, configFile, variableParams);
const app = await this.createApp(loaderProperties, configFile, variableParams);
await app.start();
}

/**
* 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.
* Starts the server as a command-line application.
* Made non-async to lower the risk of unhandled promise rejections.
* @param args - Command line arguments.
* @param stderr - Standard error stream.
*/
Expand Down Expand Up @@ -95,8 +91,8 @@ export class AppRunner {
};
const configFile = this.resolveFilePath(params.config, 'config/default.json');

// Create and execute the server initializer
this.getApp(loaderProperties, configFile, params)
// Create and execute the app
this.createApp(loaderProperties, configFile, params)
.then(
async(app): Promise<void> => app.start(),
(error: Error): void => {
Expand All @@ -112,19 +108,34 @@ export class AppRunner {
}

/**
* Resolves a path relative to the current working directory,
* falling back to a path relative to this module.
* Creates the main app object to start the server from a given config.
* @param loaderProperties - Components.js loader properties.
* @param configFile - Path to a Components.js config file.
* @param variables - Variables to pass into the config file.
*/
protected resolveFilePath(cwdPath?: string | null, modulePath = ''): string {
return typeof cwdPath === 'string' ?
absoluteFilePath(cwdPath) :
joinFilePath(__dirname, '../../', modulePath);
public async createApp(
loaderProperties: IComponentsManagerBuilderOptions<App>,
configFile: string,
variables: CliParams | Record<string, any>,
): Promise<App> {
// Translate command-line parameters if needed
if (typeof variables.loggingLevel === 'string') {
variables = this.createVariables(variables as CliParams);
}

// Set up Components.js
const componentsManager = await ComponentsManager.build(loaderProperties);
await componentsManager.configRegistry.register(configFile);

// Create the app
const app = 'urn:solid-server:default:App';
return await componentsManager.instantiate(app, { variables });
}

/**
* Translates command-line parameters into configuration variables
* Translates command-line parameters into Components.js variables.
*/
protected createVariables(params: ConfigVariables): Record<string, any> {
protected createVariables(params: CliParams): Record<string, any> {
return {
'urn:solid-server:default:variable:baseUrl':
params.baseUrl ? ensureTrailingSlash(params.baseUrl) : `http://localhost:${params.port}/`,
Expand All @@ -140,27 +151,12 @@ export class AppRunner {
}

/**
* Creates the server initializer
* Resolves a path relative to the current working directory,
* falling back to a path relative to this module.
*/
protected async createApp(
componentsProperties: IComponentsManagerBuilderOptions<App>,
configFile: string,
variables: Record<string, any>,
): Promise<App> {
const componentsManager = await ComponentsManager.build(componentsProperties);

const initializer = 'urn:solid-server:default:App';
await componentsManager.configRegistry.register(configFile);
return await componentsManager.instantiate(initializer, { variables });
protected resolveFilePath(cwdPath?: string | null, modulePath = ''): string {
return typeof cwdPath === 'string' ?
absoluteFilePath(cwdPath) :
joinFilePath(__dirname, '../../', modulePath);
}
}

export interface ConfigVariables {
loggingLevel: string;
port: number;
baseUrl?: string;
rootFilePath?: string;
sparqlEndpoint?: string;
showStackTrace?: boolean;
podConfigJson?: string;
}

0 comments on commit bbea5e5

Please sign in to comment.