Skip to content

Commit

Permalink
feat: Move runtime config into dedicated component, Closes #67
Browse files Browse the repository at this point in the history
* Move runtime config into dedicated component, Closes #67

* Migrate FileResourceStore to RuntimeConfig
  • Loading branch information
rubensworks authored Aug 26, 2020
1 parent 4f8ebff commit 5126356
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 84 deletions.
21 changes: 11 additions & 10 deletions bin/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
QuadToTurtleConverter,
Representation,
RepresentationConvertingStore,
RuntimeConfig,
Setup,
SimpleAclAuthorizer,
SimpleBodyParser,
Expand All @@ -36,15 +37,13 @@ import {
const { argv } = yargs
.usage('node ./bin/server.js [args]')
.options({
port: { type: 'number', alias: 'p', default: 3000 },
port: { type: 'number', alias: 'p' },
})
.help();

const { port } = argv;

const base = `http://localhost:${port}/`;

// This is instead of the dependency injection that still needs to be added
const runtimeConfig = new RuntimeConfig();

const bodyParser = new CompositeAsyncHandler<HttpRequest, Representation | undefined>([
new SimpleSparqlUpdateBodyParser(),
new SimpleBodyParser(),
Expand All @@ -62,7 +61,7 @@ const permissionsExtractor = new CompositeAsyncHandler([
]);

// Will have to see how to best handle this
const store = new SimpleResourceStore(base);
const store = new SimpleResourceStore(runtimeConfig);
const converter = new CompositeAsyncHandler([
new TurtleToQuadConverter(),
new QuadToTurtleConverter(),
Expand All @@ -73,7 +72,7 @@ const patcher = new SimpleSparqlUpdatePatchHandler(convertingStore, locker);
const patchingStore = new PatchingStore(convertingStore, patcher);

const aclManager = new SimpleExtensionAclManager();
const containerManager = new UrlContainerManager(base);
const containerManager = new UrlContainerManager(runtimeConfig);
const authorizer = new SimpleAclAuthorizer(aclManager, containerManager, patchingStore);

const operationHandler = new CompositeAsyncHandler([
Expand All @@ -97,9 +96,11 @@ const httpHandler = new AuthenticatedLdpHandler({

const httpServer = new ExpressHttpServer(httpHandler);

const setup = new Setup(httpServer, store, aclManager);
setup.setup(port, base).then((): void => {
process.stdout.write(`Running at ${base}\n`);
const setup = new Setup(httpServer, store, aclManager, runtimeConfig);

runtimeConfig.reset({ port: argv.port });
setup.setup().then((): void => {
process.stdout.write(`Running at ${runtimeConfig.base}\n`);
}).catch((error): void => {
process.stderr.write(`${error}\n`);
process.exit(1);
Expand Down
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export * from './src/authorization/SimpleAuthorizer';
export * from './src/authorization/SimpleExtensionAclManager';

// Init
export * from './src/init/RuntimeConfig';
export * from './src/init/Setup';

// LDP/HTTP
Expand Down
38 changes: 38 additions & 0 deletions src/init/RuntimeConfig.ts
Original file line number Diff line number Diff line change
@@ -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;
}
20 changes: 14 additions & 6 deletions src/init/Setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -11,19 +12,26 @@ 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;
}

/**
* Set up a server at the given port and base URL.
* @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> => {
Expand All @@ -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 ]),
Expand All @@ -56,6 +64,6 @@ export class Setup {

await aclSetup();

this.httpServer.listen(port);
this.httpServer.listen(this.runtimeConfig.port);
}
}
21 changes: 13 additions & 8 deletions src/storage/FileResourceStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.
Expand Down
14 changes: 8 additions & 6 deletions src/storage/SimpleResourceStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions src/storage/UrlContainerManager.ts
Original file line number Diff line number Diff line change
@@ -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.');
}

Expand Down
5 changes: 3 additions & 2 deletions test/integration/AuthenticatedLdpHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { QuadToTurtleConverter } from '../../src/storage/conversion/QuadToTurtle
import { Representation } from '../../src/ldp/representation/Representation';
import { RepresentationConvertingStore } from '../../src/storage/RepresentationConvertingStore';
import { ResponseDescription } from '../../src/ldp/operations/ResponseDescription';
import { RuntimeConfig } from '../../src/init/RuntimeConfig';
import { SimpleAuthorizer } from '../../src/authorization/SimpleAuthorizer';
import { SimpleBodyParser } from '../../src/ldp/http/SimpleBodyParser';
import { SimpleCredentialsExtractor } from '../../src/authentication/SimpleCredentialsExtractor';
Expand Down Expand Up @@ -44,7 +45,7 @@ describe('An integrated AuthenticatedLdpHandler', (): void => {
const permissionsExtractor = new BasePermissionsExtractor();
const authorizer = new SimpleAuthorizer();

const store = new SimpleResourceStore('http://test.com/');
const store = new SimpleResourceStore(new RuntimeConfig({ base: 'http://test.com/' }));
const operationHandler = new CompositeAsyncHandler<Operation, ResponseDescription>([
new SimpleGetOperationHandler(store),
new SimplePostOperationHandler(store),
Expand Down Expand Up @@ -115,7 +116,7 @@ describe('An integrated AuthenticatedLdpHandler', (): void => {
]);
const authorizer = new SimpleAuthorizer();

const store = new SimpleResourceStore('http://test.com/');
const store = new SimpleResourceStore(new RuntimeConfig({ base: 'http://test.com/' }));
const converter = new CompositeAsyncHandler([
new QuadToTurtleConverter(),
new TurtleToQuadConverter(),
Expand Down
5 changes: 3 additions & 2 deletions test/integration/Authorization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { QuadToTurtleConverter } from '../../src/storage/conversion/QuadToTurtle
import { RepresentationConvertingStore } from '../../src/storage/RepresentationConvertingStore';
import { ResourceStore } from '../../src/storage/ResourceStore';
import { ResponseDescription } from '../../src/ldp/operations/ResponseDescription';
import { RuntimeConfig } from '../../src/init/RuntimeConfig';
import { SimpleAclAuthorizer } from '../../src/authorization/SimpleAclAuthorizer';
import { SimpleBodyParser } from '../../src/ldp/http/SimpleBodyParser';
import { SimpleCredentialsExtractor } from '../../src/authentication/SimpleCredentialsExtractor';
Expand Down Expand Up @@ -80,7 +81,7 @@ describe('A server with authorization', (): void => {
bodyParser,
});

const store = new SimpleResourceStore('http://test.com/');
const store = new SimpleResourceStore(new RuntimeConfig({ base: 'http://test.com/' }));
const converter = new CompositeAsyncHandler([
new QuadToTurtleConverter(),
new TurtleToQuadConverter(),
Expand All @@ -91,7 +92,7 @@ describe('A server with authorization', (): void => {
const permissionsExtractor = new BasePermissionsExtractor();
const authorizer = new SimpleAclAuthorizer(
new SimpleExtensionAclManager(),
new UrlContainerManager('http://test.com/'),
new UrlContainerManager(new RuntimeConfig({ base: 'http://test.com/' })),
convertingStore,
);

Expand Down
43 changes: 43 additions & 0 deletions test/unit/init/RuntimeConfig.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { RuntimeConfig } from '../../../src/init/RuntimeConfig';

describe('RuntimeConfig', (): void => {
it('handles undefined args.', async(): Promise<void> => {
const config = new RuntimeConfig();
expect(config.port).toEqual(3000);
expect(config.base).toEqual('http://localhost:3000/');
});

it('handles empty args.', async(): Promise<void> => {
const config = new RuntimeConfig({});
expect(config.port).toEqual(3000);
expect(config.base).toEqual('http://localhost:3000/');
});

it('handles args with port.', async(): Promise<void> => {
const config = new RuntimeConfig({ port: 1234 });
expect(config.port).toEqual(1234);
expect(config.base).toEqual('http://localhost:1234/');
});

it('handles args with base.', async(): Promise<void> => {
const config = new RuntimeConfig({ base: 'http://example.org/' });
expect(config.port).toEqual(3000);
expect(config.base).toEqual('http://example.org/');
});

it('handles args with port and base.', async(): Promise<void> => {
const config = new RuntimeConfig({ port: 1234, base: 'http://example.org/' });
expect(config.port).toEqual(1234);
expect(config.base).toEqual('http://example.org/');
});

it('handles resetting data.', async(): Promise<void> => {
const config = new RuntimeConfig({});
expect(config.port).toEqual(3000);
expect(config.base).toEqual('http://localhost:3000/');

config.reset({ port: 1234, base: 'http://example.org/' });
expect(config.port).toEqual(1234);
expect(config.base).toEqual('http://example.org/');
});
});
7 changes: 4 additions & 3 deletions test/unit/init/Setup.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RuntimeConfig } from '../../../src/init/RuntimeConfig';
import { Setup } from '../../../src/init/Setup';

describe('Setup', (): void => {
Expand All @@ -15,16 +16,16 @@ describe('Setup', (): void => {
httpServer = {
listen: jest.fn(),
};
setup = new Setup(httpServer, store, aclManager);
setup = new Setup(httpServer, store, aclManager, new RuntimeConfig());
});

it('starts an HTTP server.', async(): Promise<void> => {
await setup.setup(3000, 'http://localhost:3000/');
await setup.setup();
expect(httpServer.listen).toHaveBeenCalledWith(3000);
});

it('invokes ACL initialization.', async(): Promise<void> => {
await setup.setup(3000, 'http://localhost:3000/');
await setup.setup();
expect(aclManager.getAcl).toHaveBeenCalledWith({ path: 'http://localhost:3000/' });
expect(store.setRepresentation).toHaveBeenCalledTimes(1);
});
Expand Down
Loading

0 comments on commit 5126356

Please sign in to comment.