UNPKG

3.77 kBJavaScriptView Raw
1/****************************************************************************
2 The MIT License (MIT)
3
4 Copyright (c) 2014 Apigee Corporation
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24'use strict';
25
26// see config.js to override or create user-specific properties //
27
28var USER_CONFIG = 'config.js';
29
30var path = require('path');
31var fs = require('fs');
32var _ = require('lodash');
33
34var config = {
35 rootDir: path.resolve(__dirname, '..'),
36 userHome: process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'],
37 debug: !!process.env.DEBUG
38};
39config.nodeModules = path.resolve(config.rootDir, 'node_modules');
40config.tmpDir = path.join(config.userHome, '.a127');
41mkDir(config.tmpDir);
42
43module.exports = config;
44
45// swagger editor //
46
47config.swagger = {
48 fileName: 'api/swagger/swagger.yaml',
49 editorDir: path.resolve(config.nodeModules, 'swagger-editor')
50};
51
52// usergrid //
53
54var USERGRID_TMP = tempDir('usergrid');
55
56config.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// account //
68
69config.account = {
70 file: path.resolve(config.tmpDir, 'accounts')
71};
72
73
74// project //
75
76config.project = {
77 port: process.env.PORT || 10010,
78 skeletonDir: path.resolve(__dirname, '..', 'project-skeleton')
79};
80
81
82// services //
83
84config.services = {
85 file: path.resolve(config.tmpDir, 'services')
86};
87
88
89// load home directory config
90loadUserConfig();
91
92// load env vars
93loadEnvConfig();
94
95
96// utility
97
98function tempDir(relativePath) {
99 if (!relativePath) { return config.tmpDir; }
100 var dirPath = path.resolve(config.tmpDir, relativePath);
101 mkDir(dirPath);
102 return dirPath;
103}
104
105function mkDir(path) {
106 try {
107 fs.mkdirSync(path, '0700');
108 } catch (err) {
109 if (err.code !== 'EEXIST') { throw err; }
110 }
111}
112
113function 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 // ignore
123 }
124}
125
126function 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