3 * Copyright 2016 RIFT.IO Inc
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * Configuration api module. Provides API functions to configure node
21 * @module framework/core/modules/api/configuration
22 * @author Kiran Kashalkar <kiran.kashalkar@riftio.com>
25 var Promise
= require('bluebird');
26 var constants
= require('../../api_utils/constants');
27 var utils
= require('../../api_utils/utils');
28 var request
= utils
.request
;
29 var configurationAPI
= {};
30 var _
= require('lodash');
31 var GLOBAL_CONFIGURATION
= {
32 api_server
: 'localhost',
37 * Get the server configuration for the Express API
38 * @param {Object} req - the Express request object
39 * @return {Function} Promise that resolves with success object or rejects
42 configurationAPI
.get = function(req
) {
43 return new Promise(function(resolve
, reject
) {
45 statusCode
: constants
.HTTP_RESPONSE_CODES
.SUCCESS
.OK
,
46 data
: GLOBAL_CONFIGURATION
52 * Update the server configuration for the Express API
53 * @param {Object} req - the Express request object
54 * @return {Function} Promise that resolves with success object or rejects
57 configurationAPI
.update = function(req
) {
58 var newConfiguration
= req
.body
;
59 return new Promise(function(resolve
, reject
) {
61 _
.merge(GLOBAL_CONFIGURATION
, newConfiguration
);
63 statusCode
: constants
.HTTP_RESPONSE_CODES
.SUCCESS
.NO_CONTENT
,
67 console
.log('Merging on new configuration failed. Error:', e
);
69 statusCode
: constants
.HTTP_RESPONSE_CODES
.ERROR
.INTERNAL_SERVER_ERROR
,
71 error
: 'Problem with merging new configuration. Error: ' + JSON
.stringify(e
)
78 configurationAPI
.globalConfiguration
= {};
80 * Internally used (by Node.js components) to update server configuration
81 * @param {Object} data - the configuration to merge
83 configurationAPI
.globalConfiguration
.update = function(data
) {
84 _
.merge(GLOBAL_CONFIGURATION
, data
);
88 * Internally used (by Node.js components) to get server configuration
89 * @param {Object} data - the configuration to merge
91 configurationAPI
.globalConfiguration
.get = function() {
92 return GLOBAL_CONFIGURATION
;
95 module
.exports
= configurationAPI
;