1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | 'use strict';
|
25 |
|
26 |
|
27 |
|
28 | var USER_CONFIG = 'config.js';
|
29 |
|
30 | var path = require('path');
|
31 | var fs = require('fs');
|
32 | var _ = require('lodash');
|
33 |
|
34 | var config = {
|
35 | rootDir: path.resolve(__dirname, '..'),
|
36 | userHome: process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'],
|
37 | debug: !!process.env.DEBUG
|
38 | };
|
39 | config.nodeModules = path.resolve(config.rootDir, 'node_modules');
|
40 | config.tmpDir = path.join(config.userHome, '.a127');
|
41 | mkDir(config.tmpDir);
|
42 |
|
43 | module.exports = config;
|
44 |
|
45 |
|
46 |
|
47 | config.swagger = {
|
48 | fileName: 'api/swagger/swagger.yaml',
|
49 | editorDir: path.resolve(config.nodeModules, 'swagger-editor')
|
50 | };
|
51 |
|
52 |
|
53 |
|
54 | var USERGRID_TMP = tempDir('usergrid');
|
55 |
|
56 | config.usergrid = {
|
57 | tmpDir: USERGRID_TMP,
|
58 | outLog: path.resolve(USERGRID_TMP, 'usergrid.log'),
|
59 | errLog: path.resolve(USERGRID_TMP, 'usergrid.log'),
|
60 | pidFile: path.resolve(USERGRID_TMP, 'usergrid.pid'),
|
61 | startTimeout: 20000,
|
62 | port: 8080,
|
63 | thriftPort: 9160
|
64 | };
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | config.account = {
|
70 | file: path.resolve(config.tmpDir, 'accounts')
|
71 | };
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | config.project = {
|
77 | port: process.env.PORT || 10010,
|
78 | skeletonDir: path.resolve(__dirname, '..', 'project-skeleton')
|
79 | };
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | config.services = {
|
85 | file: path.resolve(config.tmpDir, 'services')
|
86 | };
|
87 |
|
88 |
|
89 |
|
90 | loadUserConfig();
|
91 |
|
92 |
|
93 | loadEnvConfig();
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | function tempDir(relativePath) {
|
99 | if (!relativePath) { return config.tmpDir; }
|
100 | var dirPath = path.resolve(config.tmpDir, relativePath);
|
101 | mkDir(dirPath);
|
102 | return dirPath;
|
103 | }
|
104 |
|
105 | function mkDir(path) {
|
106 | try {
|
107 | fs.mkdirSync(path, '0700');
|
108 | } catch (err) {
|
109 | if (err.code !== 'EEXIST') { throw err; }
|
110 | }
|
111 | }
|
112 |
|
113 | function loadUserConfig() {
|
114 | try {
|
115 | var confPath = path.join(config.tmpDir, USER_CONFIG);
|
116 | var userConf = require(confPath);
|
117 | _.merge(config, userConf);
|
118 | if (config.debug) {
|
119 | console.log('user config loaded from ' + confPath);
|
120 | }
|
121 | } catch (err) {
|
122 |
|
123 | }
|
124 | }
|
125 |
|
126 | function loadEnvConfig() {
|
127 | _.each(process.env, function(value, key) {
|
128 | if (key.indexOf('a127_') == 0) {
|
129 | var split = key.split('_');
|
130 | var configItem = config;
|
131 | for (var i = 1; i < split.length; i++) {
|
132 | var subKey = split[i];
|
133 | if (i < split.length - 1) {
|
134 | if (!configItem[subKey]) { configItem[subKey] = {}; }
|
135 | configItem = configItem[subKey];
|
136 | } else {
|
137 | configItem[subKey] = value;
|
138 | }
|
139 | }
|
140 | }
|
141 | });
|
142 | } |
\ | No newline at end of file |