1 |
|
2 | 'use strict';
|
3 |
|
4 | const path = require('path');
|
5 | const chalk = require('chalk');
|
6 | const _ = require('lodash');
|
7 | const winston = require('winston');
|
8 | const cluster = require('cluster');
|
9 |
|
10 |
|
11 | var configs = [];
|
12 |
|
13 |
|
14 | function _printOnce(callback){
|
15 | if((cluster.isMaster || (cluster.worker && cluster.worker.id==1)) && callback && process.env.NODE_ENV!='test'){
|
16 | callback();
|
17 | }
|
18 | }
|
19 |
|
20 |
|
21 | function init(moduleName){
|
22 | if(!moduleName || moduleName==''){
|
23 | moduleName = 'server';
|
24 | }
|
25 |
|
26 |
|
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
|
45 | function load(moduleName){
|
46 | if(!moduleName || moduleName==''){
|
47 | moduleName = 'server';
|
48 | }
|
49 |
|
50 |
|
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 |
|
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 |
|
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 |
|
102 | config = _.merge(defaultConfig, config);
|
103 | configs[moduleName] = config;
|
104 |
|
105 |
|
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 |
|
114 | return configs[moduleName];
|
115 | }
|
116 |
|
117 |
|
118 | function 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 |
|
130 | module.exports = init;
|