Created
September 25, 2018 19:11
-
-
Save kevyworks/c3b6e6c56ca69a0979384238d7778372 to your computer and use it in GitHub Desktop.
Moleculer `mergeOption`. `nodemon index.js`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict" | |
const _ = require("lodash"); | |
const { ServiceBroker } = require("moleculer"); | |
const mergeConfig = (config = null) => { | |
if (config && _.isString(config)) { | |
config = require(config); | |
} else if (config && _.isObject(config) && _.keysIn(config, "configFile")) { | |
let filePath = config.configFile; | |
delete config.configFile; | |
config = require(filePath); | |
} | |
config = _.defaultsDeep(config, ServiceBroker.defaultOptions); | |
if (config.logger == null) | |
config.logger = console; | |
function normalizeEnvValue(value) { | |
if (value.toLowerCase() === "true" || value.toLowerCase() === "false") { | |
// Convert to boolean | |
value = value === "true"; | |
} else if (!isNaN(value)) { | |
// Convert to number | |
value = Number(value); | |
} | |
return value; | |
} | |
function overwriteFromEnv(obj, prefix) { | |
Object.keys(obj).forEach(key => { | |
const envName = ((prefix ? prefix + "_" : "") + key).toUpperCase(); | |
if (process.env[envName]) { | |
obj[key] = normalizeEnvValue(process.env[envName]); | |
} | |
if (_.isPlainObject(obj[key])) | |
obj[key] = overwriteFromEnv(obj[key], (prefix ? prefix + "_" : "") + key); | |
}); | |
const moleculerPrefix = "MOL_"; | |
Object.keys(process.env) | |
.filter(key => key.startsWith(moleculerPrefix)) | |
.map(key => ({ | |
key, | |
withoutPrefix: key.substr(moleculerPrefix.length) | |
})) | |
.forEach(variable => { | |
const dotted = variable.withoutPrefix | |
.split("__") | |
.map(level => level.toLocaleLowerCase()) | |
.map(level => | |
level | |
.split("_") | |
.map((value, index) => { | |
if (index == 0) { | |
return value; | |
} else { | |
return value[0].toUpperCase() + value.substring(1); | |
} | |
}) | |
.join("") | |
) | |
.join("."); | |
obj = Utils.dotSet(obj, dotted, normalizeEnvValue(process.env[variable.key])); | |
}); | |
return obj; | |
} | |
return overwriteFromEnv(config); | |
}; | |
module.exports = { | |
mergeConfig | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
// Preload .env | |
require("dotenv").config(); | |
const path = require("path"); | |
const { mergeConfig } = require("./helpers"); | |
const { ServiceBroker } = require("moleculer"); | |
const schema = mergeConfig(process.cwd() + "/moleculer.config.js"); | |
let broker = new ServiceBroker(schema); | |
broker.loadServices(path.resolve(process.env.SERVICEDIR), process.env.SERVICES); | |
broker.start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment