mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
refactor: Add ExpressHttpServerFactory.
This commit is contained in:
parent
4ef4d44a3a
commit
e39e7963eb
@ -2,9 +2,9 @@
|
|||||||
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^1.0.0/components/context.jsonld",
|
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^1.0.0/components/context.jsonld",
|
||||||
"@graph": [
|
"@graph": [
|
||||||
{
|
{
|
||||||
"@id": "urn:solid-server:default:HttpServer",
|
"@id": "urn:solid-server:default:ServerFactory",
|
||||||
"@type": "ExpressHttpServer",
|
"@type": "ExpressHttpServerFactory",
|
||||||
"ExpressHttpServer:_handler": {
|
"ExpressHttpServerFactory:_handler": {
|
||||||
"@id": "urn:solid-server:default:HttpHandler"
|
"@id": "urn:solid-server:default:HttpHandler"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,8 @@
|
|||||||
{
|
{
|
||||||
"@id": "urn:solid-server:default",
|
"@id": "urn:solid-server:default",
|
||||||
"@type": "Setup",
|
"@type": "Setup",
|
||||||
"Setup:_httpServer": {
|
"Setup:_serverFactory": {
|
||||||
"@id": "urn:solid-server:default:HttpServer"
|
"@id": "urn:solid-server:default:ServerFactory"
|
||||||
},
|
},
|
||||||
"Setup:_store": {
|
"Setup:_store": {
|
||||||
"@id": "urn:solid-server:default:ResourceStore_Patching"
|
"@id": "urn:solid-server:default:ResourceStore_Patching"
|
||||||
|
2
index.ts
2
index.ts
@ -84,7 +84,7 @@ export * from './src/logging/VoidLoggerFactory';
|
|||||||
export * from './src/logging/WinstonLoggerFactory';
|
export * from './src/logging/WinstonLoggerFactory';
|
||||||
|
|
||||||
// Server
|
// Server
|
||||||
export * from './src/server/ExpressHttpServer';
|
export * from './src/server/ExpressHttpServerFactory';
|
||||||
export * from './src/server/HttpHandler';
|
export * from './src/server/HttpHandler';
|
||||||
export * from './src/server/HttpRequest';
|
export * from './src/server/HttpRequest';
|
||||||
export * from './src/server/HttpResponse';
|
export * from './src/server/HttpResponse';
|
||||||
|
@ -3,7 +3,7 @@ import type { AclManager } from '../authorization/AclManager';
|
|||||||
import { RepresentationMetadata } from '../ldp/representation/RepresentationMetadata';
|
import { RepresentationMetadata } from '../ldp/representation/RepresentationMetadata';
|
||||||
import type { LoggerFactory } from '../logging/LoggerFactory';
|
import type { LoggerFactory } from '../logging/LoggerFactory';
|
||||||
import { getLoggerFor, setGlobalLoggerFactory } from '../logging/LogUtil';
|
import { getLoggerFor, setGlobalLoggerFactory } from '../logging/LogUtil';
|
||||||
import type { ExpressHttpServer } from '../server/ExpressHttpServer';
|
import type { ExpressHttpServerFactory } from '../server/ExpressHttpServerFactory';
|
||||||
import type { ResourceStore } from '../storage/ResourceStore';
|
import type { ResourceStore } from '../storage/ResourceStore';
|
||||||
import { TEXT_TURTLE } from '../util/ContentTypes';
|
import { TEXT_TURTLE } from '../util/ContentTypes';
|
||||||
import { CONTENT_TYPE } from '../util/UriConstants';
|
import { CONTENT_TYPE } from '../util/UriConstants';
|
||||||
@ -13,7 +13,7 @@ import { CONTENT_TYPE } from '../util/UriConstants';
|
|||||||
*/
|
*/
|
||||||
export class Setup {
|
export class Setup {
|
||||||
protected readonly logger = getLoggerFor(this);
|
protected readonly logger = getLoggerFor(this);
|
||||||
private readonly httpServer: ExpressHttpServer;
|
private readonly serverFactory: ExpressHttpServerFactory;
|
||||||
private readonly store: ResourceStore;
|
private readonly store: ResourceStore;
|
||||||
private readonly aclManager: AclManager;
|
private readonly aclManager: AclManager;
|
||||||
private readonly loggerFactory: LoggerFactory;
|
private readonly loggerFactory: LoggerFactory;
|
||||||
@ -21,14 +21,14 @@ export class Setup {
|
|||||||
private readonly port: number;
|
private readonly port: number;
|
||||||
|
|
||||||
public constructor(
|
public constructor(
|
||||||
httpServer: ExpressHttpServer,
|
serverFactory: ExpressHttpServerFactory,
|
||||||
store: ResourceStore,
|
store: ResourceStore,
|
||||||
aclManager: AclManager,
|
aclManager: AclManager,
|
||||||
loggerFactory: LoggerFactory,
|
loggerFactory: LoggerFactory,
|
||||||
base: string,
|
base: string,
|
||||||
port: number,
|
port: number,
|
||||||
) {
|
) {
|
||||||
this.httpServer = httpServer;
|
this.serverFactory = serverFactory;
|
||||||
this.store = store;
|
this.store = store;
|
||||||
this.aclManager = aclManager;
|
this.aclManager = aclManager;
|
||||||
this.loggerFactory = loggerFactory;
|
this.loggerFactory = loggerFactory;
|
||||||
@ -73,7 +73,7 @@ export class Setup {
|
|||||||
this.logger.debug('Setup default ACL settings');
|
this.logger.debug('Setup default ACL settings');
|
||||||
await aclSetup();
|
await aclSetup();
|
||||||
|
|
||||||
this.httpServer.listen(this.port);
|
this.serverFactory.startServer(this.port);
|
||||||
|
|
||||||
return this.base;
|
return this.base;
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,9 @@ import type { Express } from 'express';
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { getLoggerFor } from '../logging/LogUtil';
|
import { getLoggerFor } from '../logging/LogUtil';
|
||||||
import type { HttpHandler } from './HttpHandler';
|
import type { HttpHandler } from './HttpHandler';
|
||||||
|
import type { HttpServerFactory } from './HttpServerFactory';
|
||||||
|
|
||||||
export class ExpressHttpServer {
|
export class ExpressHttpServerFactory implements HttpServerFactory {
|
||||||
protected readonly logger = getLoggerFor(this);
|
protected readonly logger = getLoggerFor(this);
|
||||||
|
|
||||||
private readonly handler: HttpHandler;
|
private readonly handler: HttpHandler;
|
||||||
@ -14,7 +15,7 @@ export class ExpressHttpServer {
|
|||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
public listen(port?: number): Server {
|
public startServer(port: number): Server {
|
||||||
const app = express();
|
const app = express();
|
||||||
this.setup(app);
|
this.setup(app);
|
||||||
return app.listen(port);
|
return app.listen(port);
|
8
src/server/HttpServerFactory.ts
Normal file
8
src/server/HttpServerFactory.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import type { Server } from 'http';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A factory for HTTP servers
|
||||||
|
*/
|
||||||
|
export interface HttpServerFactory {
|
||||||
|
startServer: (port: number) => Server;
|
||||||
|
}
|
@ -3,7 +3,7 @@ import type { ResourceIdentifier } from '../../../src/ldp/representation/Resourc
|
|||||||
import { VoidLoggerFactory } from '../../../src/logging/VoidLoggerFactory';
|
import { VoidLoggerFactory } from '../../../src/logging/VoidLoggerFactory';
|
||||||
|
|
||||||
describe('Setup', (): void => {
|
describe('Setup', (): void => {
|
||||||
let httpServer: any;
|
let serverFactory: any;
|
||||||
let store: any;
|
let store: any;
|
||||||
let aclManager: any;
|
let aclManager: any;
|
||||||
let setup: Setup;
|
let setup: Setup;
|
||||||
@ -14,15 +14,15 @@ describe('Setup', (): void => {
|
|||||||
aclManager = {
|
aclManager = {
|
||||||
getAcl: jest.fn(async(): Promise<ResourceIdentifier> => ({ path: 'http://test.com/.acl' })),
|
getAcl: jest.fn(async(): Promise<ResourceIdentifier> => ({ path: 'http://test.com/.acl' })),
|
||||||
};
|
};
|
||||||
httpServer = {
|
serverFactory = {
|
||||||
listen: jest.fn(),
|
startServer: jest.fn(),
|
||||||
};
|
};
|
||||||
setup = new Setup(httpServer, store, aclManager, new VoidLoggerFactory(), 'http://localhost:3000/', 3000);
|
setup = new Setup(serverFactory, store, aclManager, new VoidLoggerFactory(), 'http://localhost:3000/', 3000);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('starts an HTTP server.', async(): Promise<void> => {
|
it('starts an HTTP server.', async(): Promise<void> => {
|
||||||
await setup.setup();
|
await setup.setup();
|
||||||
expect(httpServer.listen).toHaveBeenCalledWith(3000);
|
expect(serverFactory.startServer).toHaveBeenCalledWith(3000);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('invokes ACL initialization.', async(): Promise<void> => {
|
it('invokes ACL initialization.', async(): Promise<void> => {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import type { Server } from 'http';
|
import type { Server } from 'http';
|
||||||
import request from 'supertest';
|
import request from 'supertest';
|
||||||
import { ExpressHttpServer } from '../../../src/server/ExpressHttpServer';
|
import { ExpressHttpServerFactory } from '../../../src/server/ExpressHttpServerFactory';
|
||||||
import { HttpHandler } from '../../../src/server/HttpHandler';
|
import { HttpHandler } from '../../../src/server/HttpHandler';
|
||||||
import type { HttpRequest } from '../../../src/server/HttpRequest';
|
import type { HttpRequest } from '../../../src/server/HttpRequest';
|
||||||
import type { HttpResponse } from '../../../src/server/HttpResponse';
|
import type { HttpResponse } from '../../../src/server/HttpResponse';
|
||||||
@ -17,7 +17,7 @@ class SimpleHttpHandler extends HttpHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('ExpressHttpServer', (): void => {
|
describe('ExpressHttpServerFactory', (): void => {
|
||||||
let server: Server;
|
let server: Server;
|
||||||
let canHandleJest: jest.Mock<Promise<void>, []>;
|
let canHandleJest: jest.Mock<Promise<void>, []>;
|
||||||
let handleJest: jest.Mock<Promise<void>, [any]>;
|
let handleJest: jest.Mock<Promise<void>, [any]>;
|
||||||
@ -37,8 +37,8 @@ describe('ExpressHttpServer', (): void => {
|
|||||||
handler.canHandle = canHandleJest;
|
handler.canHandle = canHandleJest;
|
||||||
handler.handle = handleJest;
|
handler.handle = handleJest;
|
||||||
|
|
||||||
const expressServer = new ExpressHttpServer(handler);
|
const factory = new ExpressHttpServerFactory(handler);
|
||||||
server = expressServer.listen();
|
server = factory.startServer(5555);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(async(): Promise<void> => {
|
afterEach(async(): Promise<void> => {
|
Loading…
x
Reference in New Issue
Block a user