feat: Move runtime config into dedicated component, Closes #67

* Move runtime config into dedicated component, Closes #67

* Migrate FileResourceStore to RuntimeConfig
This commit is contained in:
Ruben Taelman
2020-08-26 10:25:47 +02:00
committed by GitHub
parent 4f8ebff7f7
commit 5126356c94
14 changed files with 194 additions and 84 deletions

38
src/init/RuntimeConfig.ts Normal file
View File

@@ -0,0 +1,38 @@
/**
* This class holds all configuration options that can be defined by the user via the command line.
*
* Concretely, this contains data that is only relevant *after* dependency injection.
*/
export class RuntimeConfig implements RuntimeConfigData {
private pport!: number;
private pbase!: string;
private prootFilepath!: string;
public constructor(data: RuntimeConfigData = {}) {
this.reset(data);
}
public reset(data: RuntimeConfigData): void {
this.pport = data.port ?? 3000;
this.pbase = data.base ?? `http://localhost:${this.port}/`;
this.prootFilepath = data.rootFilepath ?? process.cwd();
}
public get base(): string {
return this.pbase;
}
public get port(): number {
return this.pport;
}
public get rootFilepath(): string {
return this.prootFilepath;
}
}
export interface RuntimeConfigData {
port?: number;
base?: string;
rootFilepath?: string;
}

View File

@@ -2,6 +2,7 @@ import { AclManager } from '../authorization/AclManager';
import { DATA_TYPE_BINARY } from '../util/ContentTypes';
import { ExpressHttpServer } from '../server/ExpressHttpServer';
import { ResourceStore } from '../storage/ResourceStore';
import { RuntimeConfig } from './RuntimeConfig';
import streamifyArray from 'streamify-array';
/**
@@ -11,11 +12,18 @@ export class Setup {
private readonly httpServer: ExpressHttpServer;
private readonly store: ResourceStore;
private readonly aclManager: AclManager;
private readonly runtimeConfig: RuntimeConfig;
public constructor(httpServer: ExpressHttpServer, store: ResourceStore, aclManager: AclManager) {
public constructor(
httpServer: ExpressHttpServer,
store: ResourceStore,
aclManager: AclManager,
runtimeConfig: RuntimeConfig,
) {
this.httpServer = httpServer;
this.store = store;
this.aclManager = aclManager;
this.runtimeConfig = runtimeConfig;
}
/**
@@ -23,7 +31,7 @@ export class Setup {
* @param port - A port number.
* @param base - A base URL.
*/
public async setup(port: number, base: string): Promise<void> {
public async setup(): Promise<void> {
// 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> => {
@@ -38,10 +46,10 @@ export class Setup {
acl:mode acl:Append;
acl:mode acl:Delete;
acl:mode acl:Control;
acl:accessTo <${base}>;
acl:default <${base}>.`;
acl:accessTo <${this.runtimeConfig.base}>;
acl:default <${this.runtimeConfig.base}>.`;
await this.store.setRepresentation(
await this.aclManager.getAcl({ path: base }),
await this.aclManager.getAcl({ path: this.runtimeConfig.base }),
{
dataType: DATA_TYPE_BINARY,
data: streamifyArray([ acl ]),
@@ -56,6 +64,6 @@ export class Setup {
await aclSetup();
this.httpServer.listen(port);
this.httpServer.listen(this.runtimeConfig.port);
}
}

View File

@@ -12,6 +12,7 @@ import { Representation } from '../ldp/representation/Representation';
import { RepresentationMetadata } from '../ldp/representation/RepresentationMetadata';
import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
import { ResourceStore } from './ResourceStore';
import { RuntimeConfig } from '../init/RuntimeConfig';
import streamifyArray from 'streamify-array';
import { UnsupportedMediaTypeHttpError } from '../util/errors/UnsupportedMediaTypeHttpError';
import { CONTENT_TYPE_QUADS, DATA_TYPE_BINARY, DATA_TYPE_QUAD } from '../util/ContentTypes';
@@ -25,26 +26,30 @@ const { extname, join: joinPath, normalize: normalizePath } = posix;
* All requests will throw an {@link NotFoundHttpError} if unknown identifiers get passed.
*/
export class FileResourceStore implements ResourceStore {
private readonly baseRequestURI: string;
private readonly rootFilepath: string;
private readonly runtimeConfig: RuntimeConfig;
private readonly interactionController: InteractionController;
private readonly metadataController: MetadataController;
/**
* @param baseRequestURI - Will be stripped of all incoming URIs and added to all outgoing ones to find the relative
* path.
* @param rootFilepath - Root filepath in which the resources and containers will be saved as files and directories.
* @param runtimeConfig - The runtime config.
* @param interactionController - Instance of InteractionController to use.
* @param metadataController - Instance of MetadataController to use.
*/
public constructor(baseRequestURI: string, rootFilepath: string, interactionController: InteractionController,
public constructor(runtimeConfig: RuntimeConfig, interactionController: InteractionController,
metadataController: MetadataController) {
this.baseRequestURI = trimTrailingSlashes(baseRequestURI);
this.rootFilepath = trimTrailingSlashes(rootFilepath);
this.runtimeConfig = runtimeConfig;
this.interactionController = interactionController;
this.metadataController = metadataController;
}
public get baseRequestURI(): string {
return trimTrailingSlashes(this.runtimeConfig.base);
}
public get rootFilepath(): string {
return trimTrailingSlashes(this.runtimeConfig.rootFilepath);
}
/**
* Store the incoming data as a file under a file path corresponding to `container.path`,
* where slashes correspond to subdirectories.

View File

@@ -5,6 +5,7 @@ import { NotFoundHttpError } from '../util/errors/NotFoundHttpError';
import { Representation } from '../ldp/representation/Representation';
import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
import { ResourceStore } from './ResourceStore';
import { RuntimeConfig } from '../init/RuntimeConfig';
import streamifyArray from 'streamify-array';
/**
@@ -13,14 +14,15 @@ import streamifyArray from 'streamify-array';
*/
export class SimpleResourceStore implements ResourceStore {
private readonly store: { [id: string]: Representation };
private readonly base: string;
private readonly runtimeConfig: RuntimeConfig;
private index = 0;
/**
* @param base - Will be stripped of all incoming URIs and added to all outgoing ones to find the relative path.
* @param runtimeConfig - Config containing base that will be stripped of all incoming URIs
* and added to all outgoing ones to find the relative path.
*/
public constructor(base: string) {
this.base = base;
public constructor(runtimeConfig: RuntimeConfig) {
this.runtimeConfig = runtimeConfig;
this.store = {
// Default root entry (what you get when the identifier is equal to the base)
@@ -102,8 +104,8 @@ export class SimpleResourceStore implements ResourceStore {
* @returns A string representing the relative path.
*/
private parseIdentifier(identifier: ResourceIdentifier): string {
const path = identifier.path.slice(this.base.length);
if (!identifier.path.startsWith(this.base)) {
const path = identifier.path.slice(this.runtimeConfig.base.length);
if (!identifier.path.startsWith(this.runtimeConfig.base)) {
throw new NotFoundHttpError();
}
return path;

View File

@@ -1,20 +1,21 @@
import { ContainerManager } from './ContainerManager';
import { ensureTrailingSlash } from '../util/Util';
import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
import { RuntimeConfig } from '../init/RuntimeConfig';
/**
* Determines containers based on URL decomposition.
*/
export class UrlContainerManager implements ContainerManager {
private readonly root: string;
private readonly runtimeConfig: RuntimeConfig;
public constructor(root: string) {
this.root = this.canonicalUrl(root);
public constructor(runtimeConfig: RuntimeConfig) {
this.runtimeConfig = runtimeConfig;
}
public async getContainer(id: ResourceIdentifier): Promise<ResourceIdentifier> {
const path = this.canonicalUrl(id.path);
if (this.root === path) {
if (this.canonicalUrl(this.runtimeConfig.base) === path) {
throw new Error('Root does not have a container.');
}