376264396693971e6adbc0b551a3be5f27c6ec2c
[osm/UI.git] / skyquake / framework / core / modules / api / configuration.js
1 /*
2 *
3 * Copyright 2016 RIFT.IO Inc
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 */
18
19 /**
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>
23 */
24
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',
33 ssl_enabled: true
34 };
35
36 /**
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
40 * with error
41 */
42 configurationAPI.get = function(req) {
43 return new Promise(function(resolve, reject) {
44 resolve({
45 statusCode: constants.HTTP_RESPONSE_CODES.SUCCESS.OK,
46 data: GLOBAL_CONFIGURATION
47 });
48 });
49 };
50
51 /**
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
55 * with error
56 */
57 configurationAPI.update = function(req) {
58 var newConfiguration = req.body;
59 return new Promise(function(resolve, reject) {
60 try {
61 _.merge(GLOBAL_CONFIGURATION, newConfiguration);
62 resolve({
63 statusCode: constants.HTTP_RESPONSE_CODES.SUCCESS.NO_CONTENT,
64 data: {}
65 })
66 } catch (e) {
67 console.log('Merging on new configuration failed. Error:', e);
68 reject({
69 statusCode: constants.HTTP_RESPONSE_CODES.ERROR.INTERNAL_SERVER_ERROR,
70 errorMessage: {
71 error: 'Problem with merging new configuration. Error: ' + JSON.stringify(e)
72 }
73 });
74 }
75 });
76 };
77
78 configurationAPI.globalConfiguration = {};
79 /**
80 * Internally used (by Node.js components) to update server configuration
81 * @param {Object} data - the configuration to merge
82 */
83 configurationAPI.globalConfiguration.update = function(data) {
84 _.merge(GLOBAL_CONFIGURATION, data);
85 };
86
87 /**
88 * Internally used (by Node.js components) to get server configuration
89 * @param {Object} data - the configuration to merge
90 */
91 configurationAPI.globalConfiguration.get = function() {
92 return GLOBAL_CONFIGURATION;
93 };
94
95 module.exports = configurationAPI;