UNPKG

3.1 kBJavaScriptView Raw
1// Configuration service
2'use strict';
3
4const path = require('path');
5const chalk = require('chalk');
6const _ = require('lodash');
7const winston = require('winston');
8const cluster = require('cluster');
9
10// Config Holders
11var configs = [];
12
13// Print Once
14function _printOnce(callback){
15 if((cluster.isMaster || (cluster.worker && cluster.worker.id==1)) && callback && process.env.NODE_ENV!='test'){
16 callback();
17 }
18}
19
20// Init/load config
21function init(moduleName){
22 if(!moduleName || moduleName==''){
23 moduleName = 'server';
24 }
25
26 // Special RAW access to methods
27 if(moduleName=='_raw'){
28 return {
29 init: init,
30 get: get,
31 load: load
32 };
33 }
34
35 // Preload requested config
36 get(moduleName);
37
38 return {
39 get: get,
40 load: load
41 };
42}
43
44// Load config file
45function load(moduleName){
46 if(!moduleName || moduleName==''){
47 moduleName = 'server';
48 }
49
50 // Detect env
51 var env = process.env.NODE_ENV || 'development';
52 if(moduleName == 'server'){
53 _printOnce(()=>{
54 winston.info(chalk.blue(moduleName), 'environment:', chalk.green(env));
55 });
56 }
57
58 // Load default config
59 var defaultConfig;
60 var defaultConfigFilePath;
61 if(moduleName == 'server'){
62 defaultConfigFilePath = path.join(global.mio.appPath, 'etc','default.json');
63 }else{
64 defaultConfigFilePath = path.join(global.mio.appPath, 'modules', moduleName, 'etc', 'default.json');
65 }
66 try {
67 defaultConfig = require(defaultConfigFilePath);
68 } catch (err) {
69 if(moduleName == 'server' && process.env.NODE_ENV!='test'){
70 winston.error(chalk.red('No default config file', defaultConfigFilePath));
71 }
72 }
73
74 // Load config
75 var config;
76 var configFilePath;
77 if(moduleName == 'server'){
78 configFilePath = path.join(global.mio.appPath, 'etc', env+'.json');
79 }else{
80 configFilePath = path.join(global.mio.appPath, 'modules', moduleName, 'etc', env+'.json');
81 }
82 try {
83 config = require(configFilePath);
84 _printOnce(()=>{
85 var mode = config.mode || env;
86 winston.info('Using Configuration for', chalk.blue(moduleName), '-', chalk.blue(mode));
87 });
88 } catch (err) {
89 if (err.code && err.code === 'MODULE_NOT_FOUND') {
90 if(moduleName == 'server'){
91 if(process.env.NODE_ENV!='test'){
92 winston.error(chalk.red('No config file', configFilePath));
93 }
94 throw new Error('No config file');
95 }
96 } else {
97 throw err;
98 }
99 }
100
101 // Merge Env Config and Default config
102 config = _.merge(defaultConfig, config);
103 configs[moduleName] = config;
104
105 // Set Winston config level according to config
106 if(config && config.logs && config.logs.level){
107 _printOnce(()=>{
108 winston.level = config.logs.level;
109 winston.info('Set winstone level:', chalk.green(config.logs.level));
110 });
111 }
112
113 // Return loaded config
114 return configs[moduleName];
115}
116
117// Get loaded config file
118function get(moduleName){
119 if(!moduleName || moduleName==''){
120 moduleName = 'server';
121 }
122
123 if(configs[moduleName]){
124 return configs[moduleName];
125 }else{
126 return load(moduleName);
127 }
128}
129
130module.exports = init;