2020-08-19 06:36:59 +00:00
|
|
|
import * as bodyParser from "body-parser";
|
2020-08-25 08:21:55 +00:00
|
|
|
import * as cors from 'cors';
|
|
|
|
import * as express from "express";
|
2020-08-19 06:36:59 +00:00
|
|
|
import { initialize } from "express-openapi";
|
2020-11-11 16:08:11 +00:00
|
|
|
import { readFile } from "fs/promises";
|
2020-08-19 06:36:59 +00:00
|
|
|
import { assignIn } from 'lodash';
|
2020-11-11 16:08:11 +00:00
|
|
|
import { join } from "path";
|
2020-10-24 05:57:48 +00:00
|
|
|
import "reflect-metadata";
|
2020-08-25 08:21:55 +00:00
|
|
|
import { Logger } from 'winston';
|
2020-10-24 05:57:48 +00:00
|
|
|
import { IoSocketServer } from "../lib/communication/IoSocketServer";
|
2020-08-30 08:19:17 +00:00
|
|
|
import { getLinkedDispatcher } from "../lib/dispatcher/getLinkedDispatcher";
|
2020-08-25 08:21:55 +00:00
|
|
|
import { getBackendAccesors } from './getBackendAccessors';
|
2020-08-19 06:36:59 +00:00
|
|
|
|
2020-11-11 16:08:11 +00:00
|
|
|
/**
|
|
|
|
* Function to generate the Base Api Documentation
|
|
|
|
* @param title The Title of the API
|
|
|
|
* @param version The Version
|
|
|
|
* @param basePath A Basepath for the API
|
|
|
|
*/
|
|
|
|
function _genApiDoc(title: string, version: string, basePath: string, definitions: any = {}){
|
|
|
|
// Default API Doc
|
|
|
|
const apiDoc = {
|
|
|
|
swagger: '2.0',
|
|
|
|
basePath: basePath || '/v1',
|
|
|
|
info: {
|
|
|
|
title,
|
|
|
|
version
|
|
|
|
},
|
|
|
|
definitions,
|
|
|
|
paths: {}
|
|
|
|
};
|
|
|
|
|
|
|
|
return apiDoc;
|
|
|
|
}
|
2020-10-24 05:57:48 +00:00
|
|
|
|
2020-08-25 22:11:26 +00:00
|
|
|
/**
|
|
|
|
* Function to start a Open-API-Server
|
|
|
|
* @param options Options for the Server
|
|
|
|
*/
|
2020-11-11 16:08:11 +00:00
|
|
|
export async function startBackend(options: {
|
2020-08-19 06:36:59 +00:00
|
|
|
port?: number,
|
2020-08-25 22:11:26 +00:00
|
|
|
backendPort?: number,
|
2020-08-19 06:36:59 +00:00
|
|
|
basePath?: string,
|
2020-08-25 08:21:55 +00:00
|
|
|
logger?: Logger,
|
|
|
|
} = {}) {
|
2020-08-19 06:36:59 +00:00
|
|
|
const app: express.Application = (express as any)();
|
|
|
|
|
|
|
|
// Define the Default Options
|
|
|
|
const defaults = {
|
|
|
|
port: 3001,
|
2020-08-25 22:11:26 +00:00
|
|
|
backendPort: 3002,
|
2020-08-19 06:36:59 +00:00
|
|
|
basePath: '/api'
|
|
|
|
}
|
|
|
|
// Mix the Options.
|
|
|
|
const opts = assignIn(defaults, options);
|
|
|
|
|
|
|
|
app.use(bodyParser.json());
|
2020-08-25 08:21:55 +00:00
|
|
|
app.use(cors());
|
|
|
|
|
2020-11-11 16:08:11 +00:00
|
|
|
let _definition = {};
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Extract the Elements provided in the API-Document.
|
|
|
|
_definition = JSON.parse(await readFile(join(__dirname, 'apidoc.json'), {
|
|
|
|
encoding: 'utf8'
|
|
|
|
})).definitions;
|
|
|
|
} catch(e){
|
|
|
|
if (opts.logger) {
|
|
|
|
opts.logger.error('cant parse apidoc.json');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 06:36:59 +00:00
|
|
|
initialize({
|
2020-11-11 16:08:11 +00:00
|
|
|
apiDoc: _genApiDoc('Backend API', '1.0.0', opts.basePath, _definition),
|
2020-08-19 06:36:59 +00:00
|
|
|
app,
|
2020-08-25 08:32:46 +00:00
|
|
|
paths: './dist/open-api',
|
2020-08-19 06:36:59 +00:00
|
|
|
routesGlob: '**/*.{ts,js}',
|
2020-08-25 08:21:55 +00:00
|
|
|
routesIndexFileRegExp: /(?:index)?\.[tj]s$/,
|
|
|
|
dependencies: {
|
2020-08-30 08:19:17 +00:00
|
|
|
_dispatcher: getLinkedDispatcher(
|
2020-10-24 05:57:48 +00:00
|
|
|
{
|
|
|
|
communicator: new IoSocketServer(opts.backendPort, 'generic', 'generic')
|
|
|
|
}
|
2020-08-25 22:11:26 +00:00
|
|
|
)
|
2020-08-25 08:21:55 +00:00
|
|
|
},
|
2020-08-19 06:36:59 +00:00
|
|
|
});
|
2020-08-25 08:21:55 +00:00
|
|
|
|
2020-08-19 06:36:59 +00:00
|
|
|
app.use(((err, req, res, next) => {
|
|
|
|
res.status(err.status).json(err);
|
|
|
|
}) as express.ErrorRequestHandler);
|
2020-08-25 08:21:55 +00:00
|
|
|
|
2020-08-19 06:36:59 +00:00
|
|
|
const server = app.listen(opts.port);
|
|
|
|
|
|
|
|
const accessor = getBackendAccesors(app);
|
|
|
|
|
2020-08-25 08:21:55 +00:00
|
|
|
if (options.logger) {
|
|
|
|
options.logger.info('Server Running on http://localhost:' + opts.port.toString() + opts.basePath);
|
|
|
|
options.logger.info('API Documentation available on http://localhost:' + opts.port.toString() + opts.basePath + '/api-docs');
|
|
|
|
options.logger.debug('Checkout http://localhost:3000/docs')
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2020-08-19 06:36:59 +00:00
|
|
|
app,
|
|
|
|
// Accessor for the Server
|
|
|
|
accessor,
|
|
|
|
// Function to extract the Definitions.
|
|
|
|
definitionUri: opts.basePath + '/api-docs',
|
|
|
|
// Function to Close the Server
|
|
|
|
close() {
|
|
|
|
server.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|