mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-07 05:52:57 +00:00
refactor: use export global variables instead of express
This commit is contained in:
committed by
Rainer Killinger
parent
8c48552abf
commit
59e4009c5d
28
src/app.ts
28
src/app.ts
@@ -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(
|
||||
|
||||
@@ -21,6 +21,7 @@ import {logger} from './common';
|
||||
* Get port from environment and store in Express.
|
||||
*/
|
||||
const port = normalizePort(process.env.PORT || '3000');
|
||||
// TODO: Can we remove that? It doesn't look like it is read at all.
|
||||
app.set('port', port);
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {SCConfigFile} from '@openstapps/core';
|
||||
import {Validator} from '@openstapps/core-tools/lib/validate';
|
||||
import {Logger} from '@openstapps/logger';
|
||||
import * as config from 'config';
|
||||
import {BackendTransport} from './notification/BackendTransport';
|
||||
@@ -23,3 +24,7 @@ export const mailer = BackendTransport.getTransportInstance();
|
||||
export const logger = new Logger(mailer);
|
||||
|
||||
export const configFile: SCConfigFile = config.util.toObject();
|
||||
|
||||
export const validator = new Validator();
|
||||
|
||||
export const isTestEnvironment = process.env.NODE_ENV !== 'production';
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {SCBulkAddRequest, SCBulkAddResponse, SCBulkAddRoute, SCNotFoundErrorResponse} from '@openstapps/core';
|
||||
import {logger} from '../common';
|
||||
import {isTestEnvironment, logger} from '../common';
|
||||
import {BulkStorage} from '../storage/BulkStorage';
|
||||
import {createRoute} from './Route';
|
||||
|
||||
@@ -36,7 +36,7 @@ export const bulkAddRouter = createRoute<SCBulkAddResponse>(
|
||||
|
||||
if (typeof bulk === 'undefined') {
|
||||
logger.warn(`Bulk with ${params.UID} not found.`);
|
||||
throw new SCNotFoundErrorResponse(app.get('isTestEnvironment'));
|
||||
throw new SCNotFoundErrorResponse(isTestEnvironment);
|
||||
}
|
||||
|
||||
await bulkMemory.database.post(request, bulk);
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {SCBulkDoneRequest, SCBulkDoneResponse, SCBulkDoneRoute, SCNotFoundErrorResponse} from '@openstapps/core';
|
||||
import {logger} from '../common';
|
||||
import {isTestEnvironment, logger} from '../common';
|
||||
import {BulkStorage} from '../storage/BulkStorage';
|
||||
import {createRoute} from './Route';
|
||||
|
||||
@@ -36,7 +36,7 @@ export const bulkDoneRouter = createRoute<SCBulkDoneResponse>(
|
||||
|
||||
if (typeof bulk === 'undefined') {
|
||||
logger.warn(`Bulk with ${params.UID} not found.`);
|
||||
throw new SCNotFoundErrorResponse(app.get('isTestEnvironment'));
|
||||
throw new SCNotFoundErrorResponse(isTestEnvironment);
|
||||
}
|
||||
|
||||
bulk.state = 'done';
|
||||
|
||||
@@ -20,7 +20,7 @@ import {
|
||||
SCSearchResponse,
|
||||
SCTooManyRequestsErrorResponse,
|
||||
} from '@openstapps/core';
|
||||
import {configFile} from '../common';
|
||||
import {configFile, isTestEnvironment} from '../common';
|
||||
import {BulkStorage} from '../storage/BulkStorage';
|
||||
import {createRoute} from './Route';
|
||||
|
||||
@@ -37,7 +37,7 @@ export const multiSearchRouter = createRoute<SCMultiSearchResponse | SCTooManyRe
|
||||
const queryNames = Object.keys(request);
|
||||
|
||||
if (queryNames.length > configFile.backend.maxMultiSearchRouteQueries) {
|
||||
throw new SCTooManyRequestsErrorResponse(app.get('isTestEnvironment'));
|
||||
throw new SCTooManyRequestsErrorResponse(isTestEnvironment);
|
||||
}
|
||||
|
||||
// get a map of promises for each query
|
||||
|
||||
@@ -19,11 +19,10 @@ import {
|
||||
SCRoute,
|
||||
SCValidationErrorResponse,
|
||||
} from '@openstapps/core';
|
||||
import {Validator} from '@openstapps/core-tools/lib/validate';
|
||||
import {Application, Router} from 'express';
|
||||
import PromiseRouter from 'express-promise-router';
|
||||
import {ValidationError} from 'jsonschema';
|
||||
import {logger} from '../common';
|
||||
import {isTestEnvironment, logger, validator} from '../common';
|
||||
import {isHttpMethod} from './HTTPTypes';
|
||||
|
||||
/**
|
||||
@@ -71,16 +70,13 @@ export function createRoute<RETURNTYPE>(
|
||||
route[verb](async (req, res) => {
|
||||
|
||||
try {
|
||||
// get the core validator from the app
|
||||
const validator: Validator = req.app.get('validator');
|
||||
|
||||
// validate request
|
||||
const requestValidation = validator.validate(req.body, routeClass.requestBodyName);
|
||||
|
||||
if (requestValidation.errors.length > 0) {
|
||||
const error = new SCValidationErrorResponse(
|
||||
requestValidation.errors,
|
||||
req.app.get('isTestEnvironment'),
|
||||
isTestEnvironment,
|
||||
);
|
||||
res.status(error.statusCode);
|
||||
res.json(error);
|
||||
@@ -97,11 +93,11 @@ export function createRoute<RETURNTYPE>(
|
||||
if (responseErrors.length > 0) {
|
||||
const validationError = new SCValidationErrorResponse(
|
||||
responseErrors,
|
||||
req.app.get('isTestEnvironment'),
|
||||
isTestEnvironment,
|
||||
);
|
||||
const internalServerError = new SCInternalServerErrorResponse(
|
||||
validationError,
|
||||
req.app.get('isTestEnvironment'),
|
||||
isTestEnvironment,
|
||||
);
|
||||
res.status(internalServerError.statusCode);
|
||||
res.json(internalServerError);
|
||||
@@ -125,7 +121,7 @@ export function createRoute<RETURNTYPE>(
|
||||
// the error is not allowed so something went wrong
|
||||
const internalServerError = new SCInternalServerErrorResponse(
|
||||
error,
|
||||
req.app.get('isTestEnvironment'),
|
||||
isTestEnvironment,
|
||||
);
|
||||
res.status(internalServerError.statusCode);
|
||||
res.json(internalServerError);
|
||||
@@ -138,8 +134,8 @@ export function createRoute<RETURNTYPE>(
|
||||
}
|
||||
|
||||
// return a SCMethodNotAllowedErrorResponse on all other HTTP methods
|
||||
route.all((req, res) => {
|
||||
const error = new SCMethodNotAllowedErrorResponse(req.app.get('isTestEnvironment'));
|
||||
route.all((_req, res) => {
|
||||
const error = new SCMethodNotAllowedErrorResponse(isTestEnvironment);
|
||||
res.status(error.statusCode);
|
||||
res.json(error);
|
||||
logger.warn(error);
|
||||
|
||||
Reference in New Issue
Block a user