UNPKG

microservice-support-toolkit

Version:

A global module with commons utilities for microservice support

53 lines (43 loc) 1.19 kB
const client = require('cloud-config-client'); const YAML = require('yaml'); const path = require('path'); const fs = require('fs'); const logger = require('./logger'); let APPNAME = 'microservice'; let cloudConfigUri = null; let config = null; const init = () => { try { logger.info('Load bootstrap configs...'); const filePath = path.join(__dirname, '../../../../resources/bootstrap.yml'); const file = fs.readFileSync(filePath, 'utf8'); const yamlFile = YAML.parse(file); cloudConfigUri = yamlFile.spring.cloud.config.uri; APPNAME = yamlFile.app.name; } catch (err) { logger.error('/resources/bootstrap.yml NOT FOUND !!!!', err); } }; init(); const load = async () => { await fetch(); return config; }; const fetch = async () => { logger.info('Spring Cloud Config fetch...'); try { config = await client.load({ endpoint: cloudConfigUri, application: APPNAME }); } catch (err) { logger.error('Spring Cloud Config fetch error', err); config = null; } return config; }; const get = property => (property ? config._properties[property] : config); module.exports = { getAppName: () => APPNAME, load, fetch, get };