Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): Removed OvernightJS #252

Merged
merged 2 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8,155 changes: 112 additions & 8,043 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions packages/server-nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"dependencies": {
"@jitar/caching": "^0.4.0",
"@jitar/runtime": "^0.4.0",
"@overnightjs/core": "^1.7.6",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"express": "^4.18.2",
Expand All @@ -46,4 +45,4 @@
"jitar",
"nodejs"
]
}
}
55 changes: 28 additions & 27 deletions packages/server-nodejs/src/JitarServer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

import { Server as OvernightServer } from '@overnightjs/core';
import bodyParser from 'body-parser';
import express, { Express } from 'express';
import { Logger } from 'tslog';

import { HealthCheck, LocalGateway, LocalNode, LocalRepository, Middleware, ProcedureRuntime, Proxy, Runtime, RemoteClassLoader } from '@jitar/runtime';
Expand Down Expand Up @@ -36,20 +35,22 @@ const STARTUP_MESSAGE = `
By Masking Technology (masking.tech)
`;

export default class JitarServer extends OvernightServer
export default class JitarServer
{
#app: Express;
#runtime?: Runtime;
#serializer: Serializer;

constructor()
{
super(false);

this.#serializer = SerializerBuilder.build(new RemoteClassLoader());

this.app.disable('x-powered-by');
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({ extended: true }));
this.#app = express();

this.#app.use(express.json());
this.#app.use(express.urlencoded({ extended: true }));

this.#app.disable('x-powered-by');
}

async start(): Promise<void>
Expand Down Expand Up @@ -89,8 +90,8 @@ export default class JitarServer extends OvernightServer
{
throw new RuntimeNotAvailable();
}
if ( ! (this.#runtime instanceof ProcedureRuntime))

if (!(this.#runtime instanceof ProcedureRuntime))
{
throw new MiddlewareNotSupported();
}
Expand Down Expand Up @@ -128,42 +129,42 @@ export default class JitarServer extends OvernightServer

#addStandAloneControllers(proxy: Proxy, logger: Logger<unknown>, index: string): void
{
super.addControllers(new HealthController(proxy, logger));
super.addControllers(new JitarController(this.app));
super.addControllers(new ModulesController(proxy, this.#serializer, logger));
super.addControllers(new ProceduresController(proxy, logger));
super.addControllers(new RPCController(proxy, this.#serializer, true, logger));
super.addControllers(new AssetsController(this.app, proxy, index, logger));
new HealthController(this.#app, proxy, logger);
new JitarController(this.#app);
new ModulesController(this.#app, proxy, this.#serializer, logger);
new ProceduresController(this.#app, proxy, logger);
new RPCController(this.#app, proxy, this.#serializer, true, logger);
new AssetsController(this.#app, proxy, index, logger);
}

#addRepositoryControllers(repository: LocalRepository, logger: Logger<unknown>, index: string): void
{
super.addControllers(new JitarController(this.app));
super.addControllers(new ModulesController(repository, this.#serializer, logger));
super.addControllers(new AssetsController(this.app, repository, index, logger));
new JitarController(this.#app);
new ModulesController(this.#app, repository, this.#serializer, logger);
new AssetsController(this.#app, repository, index, logger);
}

#addGatewayControllers(gateway: LocalGateway, logger: Logger<unknown>): void
{
super.addControllers(new NodesController(gateway, logger));
super.addControllers(new ProceduresController(gateway, logger));
super.addControllers(new RPCController(gateway, this.#serializer, false, logger));
new NodesController(this.#app, gateway, logger);
new ProceduresController(this.#app, gateway, logger);
new RPCController(this.#app, gateway, this.#serializer, false, logger);
}

#addNodeControllers(node: LocalNode, logger: Logger<unknown>): void
{
super.addControllers(new HealthController(node, logger));
super.addControllers(new ProceduresController(node, logger));
super.addControllers(new RPCController(node, this.#serializer, true, logger));
new HealthController(this.#app, node, logger);
new ProceduresController(this.#app, node, logger);
new RPCController(this.#app, node, this.#serializer, true, logger);
}

#addProxyControllers(proxy: Proxy, logger: Logger<unknown>): void
{
super.addControllers(new ProxyController(this.app, proxy, logger));
new ProxyController(this.#app, proxy, logger);
}

async #startServer(port: string): Promise<void>
{
return new Promise(resolve => { this.app.listen(port, resolve); });
return new Promise(resolve => { this.#app.listen(port, resolve); });
}
}
2 changes: 0 additions & 2 deletions packages/server-nodejs/src/controllers/AssetsController.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@

import { Controller } from '@overnightjs/core';
import express, { Request, Response } from 'express';
import { Logger } from 'tslog';

import { FileNotFound, LocalRepository, Proxy } from '@jitar/runtime';

@Controller('')
export default class AssetsController
{
#repository: LocalRepository | Proxy;
Expand Down
11 changes: 5 additions & 6 deletions packages/server-nodejs/src/controllers/HealthController.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@

import { Controller, Get } from '@overnightjs/core';
import { Request, Response } from 'express';
import express, { Request, Response } from 'express';
import { Logger } from 'tslog';

import { LocalNode, Proxy } from '@jitar/runtime';

@Controller('health')
export default class HealthController
{
#node: LocalNode | Proxy;
#logger: Logger<unknown>;

constructor(node: LocalNode | Proxy, logger: Logger<unknown>)
constructor(app: express.Application, node: LocalNode | Proxy, logger: Logger<unknown>)
{
this.#node = node;
this.#logger = logger;

app.get('/health', (request: Request, response: Response) => { this.getHealth(request, response); });
app.get('/health/status', (request: Request, response: Response) => { this.isHealthy(request, response); });
}

@Get()
async getHealth(request: Request, response: Response): Promise<Response>
{
const health = await this.#node.getHealth();
Expand All @@ -28,7 +28,6 @@ export default class HealthController
return response.status(200).send(data);
}

@Get('status')
async isHealthy(request: Request, response: Response): Promise<Response>
{
const healthy = await this.#node.isHealthy();
Expand Down
2 changes: 0 additions & 2 deletions packages/server-nodejs/src/controllers/JitarController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

import { Controller } from '@overnightjs/core';
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
Expand All @@ -12,7 +11,6 @@ const runtimeLocation = path.join(fileLocation, '../../../runtime/dist');
const reflectionLocation = path.join(fileLocation, '../../../reflection/dist');
const serializationLocation = path.join(fileLocation, '../../../serialization/dist');

@Controller('')
export default class JitarController
{
constructor(app: express.Application)
Expand Down
15 changes: 9 additions & 6 deletions packages/server-nodejs/src/controllers/ModulesController.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@

import { Controller, Get, Post } from '@overnightjs/core';
import { Request, Response } from 'express';
import express, { Request, Response } from 'express';
import { Logger } from 'tslog';

import { ClientIdHelper, LocalRepository, Proxy } from '@jitar/runtime';
import { Serializer } from '@jitar/serialization';

const clientIdHelper = new ClientIdHelper();

@Controller('modules')
export default class ModulesController
{
#repository: LocalRepository | Proxy;
#serializer: Serializer;
#logger: Logger<unknown>;

constructor(repository: LocalRepository | Proxy, serializer: Serializer, logger: Logger<unknown>)
constructor(app: express.Application, repository: LocalRepository | Proxy, serializer: Serializer, logger: Logger<unknown>)
{
this.#repository = repository;
this.#serializer = serializer;
this.#logger = logger;

app.post('/modules', (request: Request, response: Response) => { this.registerClient(request, response); });
app.get('/modules/:clientId/*', (request: Request, response: Response) => { this.getModule(request, response); });
}

@Post()
async registerClient(request: Request, response: Response): Promise<Response>
{
this.#logger.info('Register client');

if ((request.body instanceof Array) === false)
{
// TODO: Throw error.
Expand All @@ -39,9 +41,10 @@ export default class ModulesController
return response.status(200).send(clientId);
}

@Get(':clientId/*')
async getModule(request: Request, response: Response): Promise<Response>
{
this.#logger.info(`Get module for -> '${request.params.clientId}'`);

const clientId = request.params.clientId;

if (typeof clientId !== 'string' || clientIdHelper.validate(clientId) === false)
Expand Down
11 changes: 5 additions & 6 deletions packages/server-nodejs/src/controllers/NodesController.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@

import { Controller, Get, Post } from '@overnightjs/core';
import { Request, Response } from 'express';
import express, { Request, Response } from 'express';
import { Logger } from 'tslog';

import { LocalGateway, RemoteNode } from '@jitar/runtime';

import NodeDto from '../models/NodeDto.js';
import DataConverter from '../utils/DataConverter.js';

@Controller('nodes')
export default class NodesController
{
#gateway: LocalGateway;
#logger: Logger<unknown>;

constructor(gateway: LocalGateway, logger: Logger<unknown>)
constructor(app: express.Application, gateway: LocalGateway, logger: Logger<unknown>)
{
this.#gateway = gateway;
this.#logger = logger;

app.get('/nodes', (request: Request, response: Response) => { this.getNodes(request, response); });
app.post('/nodes', (request: Request, response: Response) => { this.add(request, response); });
}

@Get()
async getNodes(request: Request, response: Response): Promise<Response>
{
const nodes = this.#gateway.nodes.map(node => { return { url: node.url, procedureNames: node.getProcedureNames() }; });
Expand All @@ -30,7 +30,6 @@ export default class NodesController
return response.status(200).send(nodes);
}

@Post()
async add(request: Request, response: Response): Promise<Response>
{
try
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@

import { Controller, Get } from '@overnightjs/core';
import { Request, Response } from 'express';
import express, { Request, Response } from 'express';
import { Logger } from 'tslog';

import { LocalGateway, LocalNode, Proxy } from '@jitar/runtime';

@Controller('procedures')
export default class ProceduresController
{
#runtime: LocalGateway | LocalNode | Proxy;
#logger: Logger<unknown>;

constructor(runtime: LocalGateway | LocalNode | Proxy, logger: Logger<unknown>)
constructor(app: express.Application, runtime: LocalGateway | LocalNode | Proxy, logger: Logger<unknown>)
{
this.#runtime = runtime;
this.#logger = logger;

app.get('/procedures', (request: Request, response: Response) => { this.getProcedureNames(request, response); });
}

@Get()
async getProcedureNames(request: Request, response: Response): Promise<Response>
{
const names = this.#runtime.getProcedureNames();
Expand Down
2 changes: 0 additions & 2 deletions packages/server-nodejs/src/controllers/ProxyController.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@

import { Controller } from '@overnightjs/core';
import express from 'express';
import expressProxy from 'express-http-proxy';
import { IncomingMessage } from 'http';
import { Logger } from 'tslog';

import { Proxy } from '@jitar/runtime';

@Controller('')
export default class ProxyController
{
#logger: Logger<unknown>;
Expand Down
19 changes: 9 additions & 10 deletions packages/server-nodejs/src/controllers/RPCController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

import { Controller, Get, Options, Post } from '@overnightjs/core';
import { Request, Response } from 'express';
import express, { Request, Response } from 'express';
import { Logger } from 'tslog';

import { Version, VersionParser, ProcedureRuntime } from '@jitar/runtime';
Expand All @@ -12,21 +11,24 @@ const RPC_PARAMETERS = ['version', 'serialize'];
const IGNORED_HEADER_KEYS = ['host', 'connection', 'content-length', 'accept-encoding', 'user-agent'];
const CORS_MAX_AGE = 86400;

@Controller('rpc')
export default class RPCController
{
#runtime: ProcedureRuntime;
#serializer: Serializer;
#useSerializer: boolean;
#logger: Logger<unknown>;

constructor(runtime: ProcedureRuntime, serializer: Serializer, useSerializer: boolean, logger: Logger<unknown>)
constructor(app: express.Application, runtime: ProcedureRuntime, serializer: Serializer, useSerializer: boolean, logger: Logger<unknown>)
{
this.#runtime = runtime;
this.#serializer = serializer;
this.#useSerializer = useSerializer;
this.#logger = logger;

app.get('/rpc/*', (request: Request, response: Response) => { this.runGet(request, response); });
app.post('/rpc/*', (request: Request, response: Response) => { this.runPost(request, response); });
app.options('/rpc/*', (request: Request, response: Response) => { this.runOptions(request, response); });

this.#showProcedureInfo();
}

Expand All @@ -44,7 +46,6 @@ export default class RPCController
this.#logger.info('Registered RPC entries', procedureNames);
}

@Get('*')
async runGet(request: Request, response: Response): Promise<Response>
{
const fqn = this.#extractFqn(request);
Expand All @@ -56,27 +57,25 @@ export default class RPCController
return this.#run(fqn, version, args, headers, response, serialize);
}

@Post('*')
async runPost(request: Request, response: Response): Promise<Response>
{
const fqn = this.#extractFqn(request);
const version = this.#extractVersion(request);
const args = await this.#extractBodyArguments(request);
const args = this.#extractBodyArguments(request);
const headers = this.#extractHeaders(request);
const serialize = this.#extractSerialize(request);

return this.#run(fqn, version, args, headers, response, serialize);
}

@Options('*')
async runOptions(request: Request, response: Response): Promise<Response>
{
return this.#setCors(response);
}

#extractFqn(request: Request): string
{
return request.path.substring(1);
return request.path.substring(5);
}

#extractVersion(request: Request): Version
Expand Down Expand Up @@ -111,7 +110,7 @@ export default class RPCController
return args;
}

async #extractBodyArguments(request: Request): Promise<Record<string, unknown>>
#extractBodyArguments(request: Request): Record<string, unknown>
{
return request.body;
}
Expand Down