diff --git a/packages/bitcore-wallet-service/src/config.ts b/packages/bitcore-wallet-service/src/config.ts index ada0d2b1aee..ac83d833fff 100644 --- a/packages/bitcore-wallet-service/src/config.ts +++ b/packages/bitcore-wallet-service/src/config.ts @@ -1,8 +1,10 @@ +import fs from 'fs'; +import path from 'path'; import _ from 'lodash'; import { logger } from './lib/logger'; const Config = (): any => { - let defaultConfig = { + const defaultConfig = { basePath: '/bws/api', disableLogs: false, port: 3232, @@ -476,12 +478,42 @@ const Config = (): any => { }; // Override default values with bws.config.js' values, if present + const { BITCORE_CONFIG_PATH = '../../../../', BWS_CONFIG_PATH = '../../' } = process.env; + let bwsConfig; + try { - // eslint-disable-next-line @typescript-eslint/no-require-imports - const bwsConfig = require('../../bws.config'); - defaultConfig = _.merge(defaultConfig, bwsConfig); - } catch { - logger.info('bws.config.js not found, using default configuration values'); + if (BITCORE_CONFIG_PATH) { + if (fs.existsSync(path.join(BITCORE_CONFIG_PATH, 'bitcore.config.json'))) { + bwsConfig = JSON.parse(fs.readFileSync(path.join(BITCORE_CONFIG_PATH, 'bitcore.config.json'), 'utf8')); + } else if (fs.existsSync(BITCORE_CONFIG_PATH) && BITCORE_CONFIG_PATH.endsWith('.json')) { + bwsConfig = JSON.parse(fs.readFileSync(BITCORE_CONFIG_PATH, 'utf8')); + } + bwsConfig = bwsConfig?.bitcoreWalletService; + if (bwsConfig) { + logger.info(`Using JSON config from ${BITCORE_CONFIG_PATH}`); + } + } + + if (!bwsConfig && BWS_CONFIG_PATH) { + if (fs.existsSync(path.join(BWS_CONFIG_PATH, 'bws.config.js'))) { + // eslint-disable-next-line @typescript-eslint/no-require-imports + bwsConfig = require(path.join(BWS_CONFIG_PATH, 'bws.config.js')); + } else if (fs.existsSync(BWS_CONFIG_PATH) && BWS_CONFIG_PATH.endsWith('.js')) { + // eslint-disable-next-line @typescript-eslint/no-require-imports + bwsConfig = require(BWS_CONFIG_PATH); + } + if (bwsConfig) { + logger.info(`Using JS config from ${BWS_CONFIG_PATH}`); + } + } + + if (!bwsConfig) { + logger.info('No config file found. Using default configuration values'); + } + + bwsConfig = _.merge(defaultConfig, bwsConfig); + } catch (err) { + throw new Error('Error loading config file:\n' + err.stack); } if (process.env.SENDGRID_API_KEY) {