Skip to content

Commit 1c231da

Browse files
committed
removed require for reading bws.config and added BWS_CONFIG environment variable
1 parent 08d948c commit 1c231da

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

packages/bitcore-wallet-service/src/config.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import fs from 'fs';
12
import _ from 'lodash';
2-
import { logger } from './lib/logger';
33

44
const Config = (): any => {
5-
let defaultConfig = {
5+
const defaultConfig = {
66
basePath: '/bws/api',
77
disableLogs: false,
88
port: 3232,
@@ -474,23 +474,41 @@ const Config = (): any => {
474474
// whitelist: []
475475
// },
476476
};
477+
478+
const configPath = process.env.BWS_CONFIG || 'bws.config.js';
477479

478-
// Override default values with bws.config.js' values, if present
480+
if (!fs.existsSync(configPath)) {
481+
throw new Error(`No bitcore config exists at ${configPath}`);
482+
}
483+
484+
const bitcoreConfigStat = fs.statSync(configPath);
485+
if (bitcoreConfigStat.isDirectory()) {
486+
throw new Error(`Provided bitcore config path: ${configPath} is a directory`);
487+
}
488+
489+
let rawConfig;
479490
try {
480-
// eslint-disable-next-line @typescript-eslint/no-require-imports
481-
const bwsConfig = require('../../bws.config');
482-
defaultConfig = _.merge(defaultConfig, bwsConfig);
483-
} catch {
484-
logger.info('bws.config.js not found, using default configuration values');
491+
rawConfig = fs.readFileSync(configPath).toString();
492+
} catch (error) {
493+
throw new Error(`Error in loading bitcore config\nFound file at ${configPath}\n${error}`);
485494
}
486495

496+
let config;
497+
try {
498+
config = JSON.parse(rawConfig);
499+
} catch (error) {
500+
throw new Error(`Error in parsing bitcore config\nFound and loaded file at ${configPath}\n${error}`);
501+
}
502+
503+
config = _.merge(defaultConfig, config);
504+
487505
if (process.env.SENDGRID_API_KEY) {
488506
// override the config value in bws.config.js with env var
489-
defaultConfig.emailOpts.sendGridApiKey = process.env.SENDGRID_API_KEY;
507+
config.emailOpts.sendGridApiKey = process.env.SENDGRID_API_KEY;
490508
}
491509
if (process.env.MAILERSEND_API_KEY) {
492510
// override the config value in bws.config.js with env var
493-
defaultConfig.emailOpts.mailerSendApiKey = process.env.MAILERSEND_API_KEY;
511+
config.emailOpts.mailerSendApiKey = process.env.MAILERSEND_API_KEY;
494512
}
495513

496514
return defaultConfig;

0 commit comments

Comments
 (0)