feat: use config for MultiSearchRoute

This commit is contained in:
Wieland Schöbl
2019-02-13 17:10:47 +01:00
committed by Rainer Killinger
parent 2b894bd1b6
commit 827827905b
6 changed files with 254 additions and 143 deletions

View File

@@ -65,6 +65,8 @@ async function configureApp() {
// validate the config file
const configValidation = scValidator.validate(config.util.toObject(), 'SCConfigFile');
// use the config file
app.set('config', config.util.toObject());
// validation failed
if (configValidation.errors.length > 0) {

View File

@@ -14,6 +14,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {
SCConfigFile,
SCMultiSearchRequest,
SCMultiSearchResponse,
SCMultiSearchRoute,
@@ -32,11 +33,12 @@ export const multiSearchRouter = createRoute<SCMultiSearchResponse | SCTooManyRe
multiSearchRouteModel,
async (request: SCMultiSearchRequest, app) => {
const config: SCConfigFile = app.get('config');
const bulkMemory: BulkStorage = app.get('bulk');
const queryNames = Object.keys(request);
if (queryNames.length > 5) {
return new SCTooManyRequestsErrorResponse(app.get('isProductiveEnvironment'));
if (queryNames.length > config.backend.maxMultiSearchRouteQueries) {
throw new SCTooManyRequestsErrorResponse(app.get('isProductiveEnvironment'));
}
// get a map of promises for each query

View File

@@ -22,6 +22,7 @@ import {
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 {isHttpMethod} from './HTTPTypes';
@@ -91,11 +92,11 @@ export function createRoute<RETURNTYPE>(
const response = await handler(req.body, req.app, req.params);
// validate response generated by handler
const responseValidation = validator.validate(response, routeClass.responseBodyName);
const responseErrors: ValidationError[] = validator.validate(response, routeClass.responseBodyName).errors;
if (responseValidation.errors.length > 0) {
if (responseErrors.length > 0) {
const validationError = new SCValidationErrorResponse(
responseValidation.errors,
responseErrors,
req.app.get('isProductiveEnvironment'),
);
const internalServerError = new SCInternalServerErrorResponse(
@@ -115,7 +116,7 @@ export function createRoute<RETURNTYPE>(
res.json(response);
} catch (error) {
// if the error response is allowed on the route
if (routeClass.errorNames.indexOf(error.constructor.name) > -1) {
if (routeClass.errorNames.some((constructorType) => error instanceof constructorType)) {
// respond with the error from the handler
res.status(error.statusCode);
res.json(error);