refactor: use export global variables instead of express

This commit is contained in:
Wieland Schöbl
2019-03-18 10:12:53 +01:00
committed by Rainer Killinger
parent 8c48552abf
commit 59e4009c5d
7 changed files with 27 additions and 37 deletions

View File

@@ -19,13 +19,12 @@ import {
SCSyntaxErrorResponse,
SCUnsupportedMediaTypeErrorResponse,
} from '@openstapps/core';
import {Validator} from '@openstapps/core-tools/lib/validate';
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, logger, mailer} from './common';
import {configFile, isTestEnvironment, logger, mailer, validator} from './common';
import {MailQueue} from './notification/MailQueue';
import {bulkAddRouter} from './routes/BulkAddRoute';
import {bulkDoneRouter} from './routes/BulkDoneRoute';
@@ -39,7 +38,6 @@ import {DatabaseConstructor} from './storage/Database';
import {Elasticsearch} from './storage/elasticsearch/Elasticsearch';
export const app = express();
const isTestEnvironment = process.env.NODE_ENV !== 'production';
async function configureApp() {
// request loggers have to be the first middleware to be set in express
@@ -107,11 +105,10 @@ async function configureApp() {
};
// validate config file
const scValidator = new Validator();
await scValidator.addSchemas(join('node_modules', '@openstapps', 'core', 'lib', 'schema'));
await validator.addSchemas(join('node_modules', '@openstapps', 'core', 'lib', 'schema'));
// validate the config file
const configValidation = scValidator.validate(configFile, 'SCConfigFile');
const configValidation = validator.validate(configFile, 'SCConfigFile');
// validation failed
if (configValidation.errors.length > 0) {
@@ -126,17 +123,11 @@ async function configureApp() {
throw new Error('You have to configure a database');
}
if (typeof mailer !== 'undefined') {
// set a mailQueue to use the backend mailer
if (config.has('internal.monitoring')) {
app.set('mailQueue', new MailQueue(mailer));
}
}
const database =
new databases[config.get<string>('internal.database.name')](
configFile,
app.get('mailQueue'),
// mailQueue
typeof mailer !== 'undefined' && config.has('internal.monitoring') ? new MailQueue(mailer) : undefined,
);
if (typeof database === 'undefined') {
@@ -145,12 +136,9 @@ async function configureApp() {
logger.ok('Validated config file sucessfully');
// make the validator available on the app
app.set('validator', scValidator);
// treats /foo and /foo/ as two different routes
// see http://expressjs.com/en/api.html#app.set
app.set('strict routing', true);
app.enable('strict routing');
// make the bulk storage available to all http middlewares/routes
app.set(
@@ -158,6 +146,8 @@ async function configureApp() {
new BulkStorage(database),
);
app.set('env', process.env.NODE_ENV);
const corsOptions = {
allowedHeaders: [
'DNT',
@@ -182,8 +172,6 @@ async function configureApp() {
// allow cors preflight requests on every route
app.options('*', cors(corsOptions));
app.set('isTestEnvironment', isTestEnvironment);
// load routes before plugins
// they now can be used or overwritten by any plugin
app.use(