github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-supply-chain-master/server/system/config.js (about) 1 /** 2 * Copyright 2017 Intel Corporation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * ---------------------------------------------------------------------------- 16 */ 17 'use strict' 18 19 const fs = require('fs') 20 const path = require('path') 21 22 const loadConfig = (defaultValue = {}) => { 23 try { 24 return require('../config.json') 25 } catch (err) { 26 // Throw error on bad JSON, otherwise ignore 27 if (err instanceof SyntaxError) throw err 28 return {} 29 } 30 } 31 32 const config = loadConfig() 33 34 const initConfigValue = (key, defaultValue = null) => { 35 config[key] = process.env[key] || config[key] || defaultValue 36 } 37 38 // Setup non-sensitive config variable with sensible defaults, 39 // if not set in environment variables or config.json 40 initConfigValue('PORT', 3000) 41 initConfigValue('RETRY_WAIT', 5000) 42 initConfigValue('DEFAULT_SUBMIT_WAIT', 5000000) 43 initConfigValue('VALIDATOR_URL', 'tcp://localhost:4004') 44 initConfigValue('DB_HOST', 'localhost') 45 initConfigValue('DB_PORT', 28015) 46 initConfigValue('DB_NAME', 'supply_chain') 47 initConfigValue('SIGNING_ALGORITHM', 'secp256k1') 48 49 // Setup config variables with no defaults 50 initConfigValue('MAPS_API_KEY') 51 52 // Setup sensitive variable, warning user if using defaults 53 initConfigValue('JWT_SECRET') 54 initConfigValue('PRIVATE_KEY') 55 56 if (!config.PRIVATE_KEY) { 57 config.PRIVATE_KEY = Array(64).fill('1').join('') 58 console.warn( 59 'WARNING! No signing key provided. Batch signing will be insecure!') 60 console.warn( 61 'Set "PRIVATE_KEY" as an environment variable or in "config.json" file.') 62 } 63 64 if (!config.JWT_SECRET) { 65 config.JWT_SECRET = 'supply-chain-secret' 66 console.warn( 67 'WARNING! No secret provided. JWT authorization tokens will be insecure!') 68 console.warn( 69 'Set "JWT_SECRET" as an environment variable or in "config.json" file.') 70 } 71 72 // Config method to set a new value, then write it to config.json 73 config.set = (key, value) => { 74 config[key] = value 75 76 const diskConfig = loadConfig() 77 diskConfig[key] = value 78 79 const configPath = path.resolve(__dirname, '../config.json') 80 const jsonConfig = JSON.stringify(diskConfig, null, 2) 81 fs.writeFile(configPath, jsonConfig, 'utf8', err => { 82 if (err) console.error(err) 83 }) 84 } 85 86 module.exports = config