refactor: adjust code after updated dependencies

Closes #39
This commit is contained in:
Jovan Krunić
2019-06-05 13:58:26 +02:00
committed by Rainer Killinger
parent 42c7350c36
commit 8b457c9911
24 changed files with 574 additions and 343 deletions

View File

@@ -19,26 +19,33 @@ import {
SCSyntaxErrorResponse,
SCUnsupportedMediaTypeErrorResponse,
} from '@openstapps/core';
import {Logger} from '@openstapps/logger';
import * as config from 'config';
import * as cors from 'cors';
import * as express from 'express';
import * as morgan from 'morgan';
import {join} from 'path';
import {configFile, isTestEnvironment, logger, mailer, validator} from './common';
import {MailQueue} from './notification/MailQueue';
import {bulkAddRouter} from './routes/BulkAddRoute';
import {bulkDoneRouter} from './routes/BulkDoneRoute';
import {bulkRouter} from './routes/BulkRoute';
import {indexRouter} from './routes/IndexRoute';
import {multiSearchRouter} from './routes/MultiSearchRoute';
import {searchRouter} from './routes/SearchRoute';
import {thingUpdateRouter} from './routes/ThingUpdateRoute';
import {BulkStorage} from './storage/BulkStorage';
import {DatabaseConstructor} from './storage/Database';
import {Elasticsearch} from './storage/elasticsearch/Elasticsearch';
import {configFile, isTestEnvironment, mailer, validator} from './common';
import {MailQueue} from './notification/mail-queue';
import {bulkAddRouter} from './routes/bulk-add-route';
import {bulkDoneRouter} from './routes/bulk-done-route';
import {bulkRouter} from './routes/bulk-route';
import {indexRouter} from './routes/index-route';
import {multiSearchRouter} from './routes/multi-search-route';
import {searchRouter} from './routes/search-route';
import {thingUpdateRouter} from './routes/thing-update-route';
import {BulkStorage} from './storage/bulk-storage';
import {DatabaseConstructor} from './storage/database';
import {Elasticsearch} from './storage/elasticsearch/elasticsearch';
/**
* Created express application
*/
export const app = express();
/**
* Configure the backend
*/
async function configureApp() {
// request loggers have to be the first middleware to be set in express
app.use(morgan('dev'));
@@ -62,10 +69,11 @@ async function configureApp() {
const err = new SCUnsupportedMediaTypeErrorResponse(isTestEnvironment);
res.status(err.statusCode);
res.json(err);
return;
}
const bodyBuffer: any[] = [];
const bodyBuffer: Buffer[] = [];
// we don't know the full size, the only way we can get is by adding up all individual chunk sizes
let bodySize = 0;
const chunkGatherer = (chunk: Buffer) => {
@@ -78,6 +86,7 @@ async function configureApp() {
const err = new SCRequestBodyTooLargeErrorResponse(isTestEnvironment);
res.status(err.statusCode);
res.json(err);
return;
}
// push the chunk in the buffer
@@ -85,7 +94,8 @@ async function configureApp() {
};
const endCallback = () => {
req.body = Buffer.concat(bodyBuffer).toString();
req.body = Buffer.concat(bodyBuffer)
.toString();
try {
req.body = JSON.parse(req.body);
@@ -94,13 +104,15 @@ async function configureApp() {
const err = new SCSyntaxErrorResponse(catchErr.message, isTestEnvironment);
res.status(err.statusCode);
res.json(err);
return;
}
};
req.on('data', chunkGatherer).on('end', endCallback);
req.on('data', chunkGatherer)
.on('end', endCallback);
});
const databases: {[name: string]: DatabaseConstructor} = {
const databases: {[name: string]: DatabaseConstructor; } = {
elasticsearch: Elasticsearch,
};
@@ -113,8 +125,7 @@ async function configureApp() {
// validation failed
if (configValidation.errors.length > 0) {
throw new Error(
'Validation of config file failed. Errors were: ' +
JSON.stringify(configValidation.errors),
`Validation of config file failed. Errors were: ${JSON.stringify(configValidation.errors)}`,
);
}
@@ -130,11 +141,13 @@ async function configureApp() {
typeof mailer !== 'undefined' && config.has('internal.monitoring') ? new MailQueue(mailer) : undefined,
);
await database.init();
if (typeof database === 'undefined') {
throw new Error('No implementation for configured database found. Please check your configuration.');
}
logger.ok('Validated config file sucessfully');
Logger.ok('Validated config file sucessfully');
// treats /foo and /foo/ as two different routes
// see http://expressjs.com/en/api.html#app.set
@@ -194,8 +207,10 @@ async function configureApp() {
// TODO: implement a route to register plugins
}
configureApp().then(() => {
logger.ok('Sucessfully configured express server');
}).catch((err) => {
configureApp()
.then(() => {
Logger.ok('Sucessfully configured express server');
})
.catch((err) => {
throw err;
});