fix: set config file before accessing it

Closes #27
This commit is contained in:
Jovan Krunić
2019-03-11 15:45:32 +01:00
committed by Rainer Killinger
parent d110d60123
commit e17db521e2
4 changed files with 11 additions and 13 deletions

View File

@@ -14,7 +14,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {
SCConfigFile,
SCNotFoundErrorResponse,
SCRequestBodyTooLargeErrorResponse,
SCSyntaxErrorResponse,
@@ -26,7 +25,7 @@ import * as cors from 'cors';
import * as express from 'express';
import * as morgan from 'morgan';
import {join} from 'path';
import {logger, mailer} from './common';
import {configFile, logger, mailer} from './common';
import {MailQueue} from './notification/MailQueue';
import {bulkAddRouter} from './routes/BulkAddRoute';
import {bulkDoneRouter} from './routes/BulkDoneRoute';
@@ -41,7 +40,6 @@ import {Elasticsearch} from './storage/elasticsearch/Elasticsearch';
export const app = express();
const isTestEnvironment = process.env.NODE_ENV !== 'production';
const configFile: SCConfigFile = app.get('config');
async function configureApp() {
// request loggers have to be the first middleware to be set in express
@@ -113,9 +111,7 @@ async function configureApp() {
await scValidator.addSchemas(join('node_modules', '@openstapps', 'core', 'lib', 'schema'));
// validate the config file
const configValidation = scValidator.validate(config.util.toObject(), 'SCConfigFile');
// use the config file
app.set('config', config.util.toObject());
const configValidation = scValidator.validate(configFile, 'SCConfigFile');
// validation failed
if (configValidation.errors.length > 0) {
@@ -139,7 +135,7 @@ async function configureApp() {
const database =
new databases[config.get<string>('internal.database.name')](
config.util.toObject(),
configFile,
app.get('mailQueue'),
);

View File

@@ -13,9 +13,13 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {SCConfigFile} from '@openstapps/core';
import {Logger} from '@openstapps/logger';
import * as config from 'config';
import {BackendTransport} from './notification/BackendTransport';
export const mailer = BackendTransport.getTransportInstance();
export const logger = new Logger(mailer);
export const configFile: SCConfigFile = config.util.toObject();

View File

@@ -14,7 +14,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {SCConfigFile, SCIndexResponse, SCIndexRoute} from '@openstapps/core';
import * as config from 'config';
import {configFile} from '../common';
import {createRoute} from './Route';
const indexRouteModel = new SCIndexRoute();
@@ -25,8 +25,7 @@ const indexRouteModel = new SCIndexRoute();
export const indexRouter = createRoute<SCIndexResponse>(
indexRouteModel,
async (_request: SCIndexRoute, _app) => {
const configObject: SCConfigFile = config.util.toObject();
delete configObject.internal;
const {internal, ...configObject}: SCConfigFile = configFile;
return configObject;
},
);

View File

@@ -14,13 +14,13 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {
SCConfigFile,
SCMultiSearchRequest,
SCMultiSearchResponse,
SCMultiSearchRoute,
SCSearchResponse,
SCTooManyRequestsErrorResponse,
} from '@openstapps/core';
import {configFile} from '../common';
import {BulkStorage} from '../storage/BulkStorage';
import {createRoute} from './Route';
@@ -33,11 +33,10 @@ 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 > config.backend.maxMultiSearchRouteQueries) {
if (queryNames.length > configFile.backend.maxMultiSearchRouteQueries) {
throw new SCTooManyRequestsErrorResponse(app.get('isProductiveEnvironment'));
}