Created
January 27, 2021 03:37
-
-
Save TroublesNCuddles/ca43f7a05e7efa88006ebdcc7dd94ce8 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* | |
* REQUIREMENTS | |
* | |
*/ | |
const {build: buildEnvironmentConfig} = require('./bootstrap/environment.js'); | |
const {build: buildFileConfiguration} = require('./bootstrap/flatfile.js'); | |
const {LOG_LEVELS} = require('../src/javascript/shared/constants/index.js'); | |
const {general: {mergeDeep, fetchValueFromObject, fillObject}} = require('../src/javascript/shared/utilities/index.js'); | |
const NPMPackageJSON = require('../package.json'); | |
const {Server: {TroublesomeServer}} = require('../src/javascript/index.js') | |
const {Logger, Transports: {Console: ConsoleLoggerTransport}} = require('../src/javascript/shared/logger/index.js'); | |
/** | |
* | |
* CONSTANTS | |
* | |
*/ | |
const { | |
DEFAULT_FLATFILE_PATHS | |
} = require('./bootstrap/constants.js'); | |
/** | |
* | |
* FUNCTIONS | |
* | |
*/ | |
const pickClientOptions = (configuration = {}, logger) => { | |
if (!configuration || !configuration.client || !Array.isArray(configuration.client.__SERVER_OPTIONS)) { | |
return configuration; | |
} | |
for (const server_option of configuration.client.__SERVER_OPTIONS) { | |
const value = fetchValueFromObject(server_option, undefined, configuration); | |
const key_pieces = ['client', ...server_option.split('=>')]; | |
configuration = mergeDeep(configuration, fillObject(key_pieces, value)); | |
} | |
return configuration; | |
}; | |
const cleanClientOptions = (configuration, logger) => { | |
if (!configuration || !configuration.client || !Array.isArray(configuration.client.__SERVER_OPTIONS)) { | |
return configuration; | |
} | |
delete configuration.client.__SERVER_OPTIONS; | |
return configuration; | |
} | |
const loadConfiguration = async (logger) => { | |
logger.info("Loading Configurations"); | |
const environmental_options = buildEnvironmentConfig(process.env); | |
const file_paths = fetchValueFromObject("bootstrap=>flatfile_paths", '').split(';').filter(el => !!el); | |
logger.fine("Loading FlatFile configurations from %s", [...DEFAULT_FLATFILE_PATHS, ...file_paths].join(', ')); | |
const file_options = await buildFileConfiguration(file_paths); | |
logger.fine( | |
'Got %d PRIMARY key(s) from Environmental Options and %d PRIMARY key(s) from Flat File options', | |
Object.keys(environmental_options).length, | |
Object.keys(file_options).length | |
); | |
let configuration = mergeDeep(file_options, environmental_options); | |
configuration = pickClientOptions(configuration, logger); | |
configuration = cleanClientOptions(configuration, logger); | |
return configuration; | |
}; | |
/** | |
* | |
* BOOTSTRAP | |
* | |
*/ | |
const logger = new Logger({ | |
components: ['Bootstrap'], | |
transports: [new ConsoleLoggerTransport()], | |
level: LOG_LEVELS.DEBUG | |
}); | |
loadConfiguration(logger) | |
.then(configuration => { | |
logger.info('Initializing %s v%s by %s', NPMPackageJSON.name, NPMPackageJSON.version, NPMPackageJSON.author); | |
return new Promise((resolve, reject) => { | |
try { | |
return resolve(new TroublesomeServer(configuration)); | |
} catch(e) { | |
return reject(e); | |
} | |
}); | |
}) | |
.then(app => app.run()) | |
.then(() => logger.info('Done')) | |
.catch(e => { | |
logger.fatal(e); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment