feat: Enable dependency injection with auto-generated components

This commit is contained in:
Ruben Taelman
2020-08-25 10:24:53 +02:00
committed by Joachim Van Herwegen
parent e88e680ed7
commit db04c55196
13 changed files with 3265 additions and 1836 deletions

50
src/init/CliRunner.ts Normal file
View File

@@ -0,0 +1,50 @@
import { ReadStream, WriteStream } from 'tty';
import { Loader, LoaderProperties } from 'componentsjs';
import yargs from 'yargs';
import { RuntimeConfig } from './RuntimeConfig';
import { Setup } from './Setup';
/**
* Generic run function for starting the server from a given config
* @param args - Command line arguments.
* @param stdin - Standard input stream.
* @param stdout - Standard output stream.
* @param stderr - Standard error stream.
* @param properties - Components loader properties.
*/
export const runCustom = function(
args: string[],
stdin: ReadStream,
stdout: WriteStream,
stderr: WriteStream,
properties: LoaderProperties,
): void {
const { argv } = yargs
.usage('node ./bin/server.js [args]')
.options({
port: { type: 'number', alias: 'p' },
})
.help();
new Promise<RuntimeConfig>(async(resolve): Promise<void> => {
// Setup from config file
const loader = new Loader(properties);
await loader.registerAvailableModuleResources();
const setup: Setup = await loader
.instantiateFromUrl('urn:solid-server:my', `${__dirname}/../../config/config-default.json`);
resolve(await setup.setup({ port: argv.port }));
}).then((runtimeConfig: RuntimeConfig): void => {
stdout.write(`Running at ${runtimeConfig.base}\n`);
}).catch((error): void => {
stderr.write(`${error}\n`);
});
};
/**
* Run function for starting the server from the command line
* @param moduleRootPath - Path to the module's root.
*/
export const runCli = function(moduleRootPath: string): void {
const argv = process.argv.slice(2);
runCustom(argv, process.stdin, process.stdout, process.stderr, { mainModulePath: moduleRootPath });
};

View File

@@ -3,7 +3,7 @@ import { AclManager } from '../authorization/AclManager';
import { ExpressHttpServer } from '../server/ExpressHttpServer';
import { ResourceStore } from '../storage/ResourceStore';
import { DATA_TYPE_BINARY } from '../util/ContentTypes';
import { RuntimeConfig } from './RuntimeConfig';
import { RuntimeConfig, RuntimeConfigData } from './RuntimeConfig';
/**
* Invokes all logic to setup a server.
@@ -28,10 +28,11 @@ export class Setup {
/**
* Set up a server at the given port and base URL.
* @param port - A port number.
* @param base - A base URL.
* @param data - Runtime config data.
*/
public async setup(): Promise<void> {
public async setup(data: RuntimeConfigData = {}): Promise<RuntimeConfig> {
this.runtimeConfig.reset(data);
// Set up acl so everything can still be done by default
// Note that this will need to be adapted to go through all the correct channels later on
const aclSetup = async(): Promise<void> => {
@@ -61,9 +62,10 @@ export class Setup {
},
);
};
await aclSetup();
this.httpServer.listen(this.runtimeConfig.port);
return this.runtimeConfig;
}
}