1 | /**
|
2 | * Copyright 2014 Skytap Inc.
|
3 | *
|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | * you may not use this file except in compliance with the License.
|
6 | * You may obtain a copy of the License at
|
7 | *
|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 | *
|
10 | * Unless required by applicable law or agreed to in writing, software
|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | * See the License for the specific language governing permissions and
|
14 | * limitations under the License.
|
15 | **/
|
16 |
|
17 | var Config = require('./config'),
|
18 | Logger = require('./logger'),
|
19 | fs = require('fs'),
|
20 | cluster = require('cluster'),
|
21 | uuid = require('node-uuid');
|
22 |
|
23 | /**
|
24 | * Module to store environment-specific details.
|
25 | **/
|
26 | var Environment = {
|
27 |
|
28 | environment : 'default',
|
29 |
|
30 | //////////////////////////////////////////////////////////////////////////
|
31 | // Public methods ///////////////////////////////////////////////////////
|
32 | ////////////////////////////////////////////////////////////////////////
|
33 |
|
34 | /**
|
35 | * Environment.getBasePath() -> String
|
36 | *
|
37 | * Returns the application base path.
|
38 | **/
|
39 | getBasePath : function() {
|
40 | return this.basePath;
|
41 | },
|
42 |
|
43 | /**
|
44 | * Environment.getEnvironment() -> String
|
45 | *
|
46 | * Returns the environment the application is running in.
|
47 | **/
|
48 | getEnvironment : function () {
|
49 | return this.environment;
|
50 | },
|
51 |
|
52 | /**
|
53 | * Environment.getInstance() -> Integer
|
54 | *
|
55 | * Returns the instance ID.
|
56 | **/
|
57 | getInstance : function () {
|
58 | return this.instance;
|
59 | },
|
60 |
|
61 | /**
|
62 | * Environment.getContextId() -> String
|
63 | *
|
64 | * Returns the context ID.
|
65 | **/
|
66 | getContextId : function () {
|
67 | return this.contextId;
|
68 | },
|
69 |
|
70 | /**
|
71 | * Environment.getContextName() -> String
|
72 | *
|
73 | * Returns the context name.
|
74 | **/
|
75 | getContextName : function () {
|
76 | return this.contextName;
|
77 | },
|
78 |
|
79 | /**
|
80 | * Environment.initialize(options)
|
81 | * - options (Object): Environment options
|
82 | **/
|
83 | initialize : function(options) {
|
84 | this.environment = options.environment;
|
85 | this.basePath = options.basePath;
|
86 | this.instance = options.instance;
|
87 | this.loggers = options.loggers;
|
88 | this.contextId = (uuid.v4()+'.'+ process.pid).replace(/-/g, '');
|
89 | this.contextName = options.contextName || 'minorjs';
|
90 |
|
91 | Config.load(
|
92 | this.environment,
|
93 | this._loadConfigs()
|
94 | );
|
95 |
|
96 | this._initLogger();
|
97 | },
|
98 |
|
99 | /**
|
100 | * Environment.isWorker() -> Boolean
|
101 | *
|
102 | * Return whether the process is a cluster worker.
|
103 | **/
|
104 | isWorker : function () {
|
105 | return cluster.isWorker;
|
106 | },
|
107 |
|
108 | /**
|
109 | * Environment.isDevelopment() -> Boolean
|
110 | *
|
111 | * Return whether the application is running in development.
|
112 | **/
|
113 | isDevelopment : function() {
|
114 | return this.environment === 'development';
|
115 | },
|
116 |
|
117 | /**
|
118 | * Environment.isProduction() -> Boolean
|
119 | *
|
120 | * Return whether the application is running in production.
|
121 | **/
|
122 | isProduction : function() {
|
123 | return this.environment === 'production';
|
124 | },
|
125 |
|
126 | //////////////////////////////////////////////////////////////////////////
|
127 | // Pseudo-private methods ///////////////////////////////////////////////
|
128 | ////////////////////////////////////////////////////////////////////////
|
129 |
|
130 | /**
|
131 | * Environment._getConfigFilePath() -> String
|
132 | *
|
133 | * Constructs the full path to a configuration file.
|
134 | **/
|
135 | _getConfigFilePath : function () {
|
136 | return this.basePath + '/config/app.json';
|
137 | },
|
138 |
|
139 | /**
|
140 | * Environment._getLoggers() -> Array
|
141 | **/
|
142 | _getLoggers : function () {
|
143 | var environment = this.isProduction() ? 'production' : 'development';
|
144 | return this.loggers[environment] !== 'undefined' ? this.loggers[environment] : [];
|
145 | },
|
146 |
|
147 | /**
|
148 | * Environment._initLogger()
|
149 | *
|
150 | * Initializes logging for the application.
|
151 | **/
|
152 | _initLogger : function() {
|
153 | Logger.initialize({
|
154 | basePath : this.getBasePath(),
|
155 | instance : this.instance,
|
156 | loggers : this._getLoggers()
|
157 | });
|
158 | },
|
159 |
|
160 | /**
|
161 | * Environment._loadConfigs() -> Object
|
162 | *
|
163 | * Loads the configuration file for this app.
|
164 | **/
|
165 | _loadConfigs : function () {
|
166 | var path = this._getConfigFilePath(),
|
167 | configs = {};
|
168 |
|
169 | if (fs.existsSync(path)) {
|
170 | configs = JSON.parse(fs.readFileSync(path, 'utf8'));
|
171 | }
|
172 |
|
173 | return configs;
|
174 | }
|
175 | };
|
176 |
|
177 | module.exports = Environment; |
\ | No newline at end of file |