update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[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 api_server_port_number: constants.SECURE_SERVER_PORT,
35 idp_server_address: constants.LAUNCHPAD_ADDRESS,
36 idp_server_protocol: constants.IDP_SERVER_PROTOCOL,
37 idp_server_port_number: constants.IDP_PORT_NUMBER
38 };
39
40 /**
41 * Get the server configuration for the Express API
42 * @param {Object} req - the Express request object
43 * @return {Function} Promise that resolves with success object or rejects
44 * with error
45 */
46 configurationAPI.get = function(req) {
47 return new Promise(function(resolve, reject) {
48 resolve({
49 statusCode: constants.HTTP_RESPONSE_CODES.SUCCESS.OK,
50 data: GLOBAL_CONFIGURATION
51 });
52 });
53 };
54
55 /**
56 * Update the server configuration for the Express API
57 * @param {Object} req - the Express request object
58 * @return {Function} Promise that resolves with success object or rejects
59 * with error
60 */
61 configurationAPI.update = function(req) {
62 var newConfiguration = req.body;
63 return new Promise(function(resolve, reject) {
64 try {
65 _.merge(GLOBAL_CONFIGURATION, newConfiguration);
66 resolve({
67 statusCode: constants.HTTP_RESPONSE_CODES.SUCCESS.NO_CONTENT,
68 data: {}
69 })
70 } catch (e) {
71 console.log('Merging on new configuration failed. Error:', e);
72 reject({
73 statusCode: constants.HTTP_RESPONSE_CODES.ERROR.INTERNAL_SERVER_ERROR,
74 errorMessage: {
75 error: 'Problem with merging new configuration. Error: ' + JSON.stringify(e)
76 }
77 });
78 }
79 });
80 };
81
82 configurationAPI.globalConfiguration = {};
83 /**
84 * Internally used (by Node.js components) to update server configuration
85 * @param {Object} data - the configuration to merge
86 */
87 configurationAPI.globalConfiguration.update = function(data) {
88 _.merge(GLOBAL_CONFIGURATION, data);
89 };
90
91 /**
92 * Internally used (by Node.js components) to get server configuration
93 * @param {Object} data - the configuration to merge
94 */
95 configurationAPI.globalConfiguration.get = function() {
96 return GLOBAL_CONFIGURATION;
97 };
98
99 var backendURL = null;
100 configurationAPI.getBackendURL = function () {
101 if (!backendURL) {
102 backendURL = GLOBAL_CONFIGURATION.api_server_protocol + '://' + GLOBAL_CONFIGURATION.api_server + ':' + GLOBAL_CONFIGURATION.api_server_port_number;
103 }
104 return backendURL;
105 }
106
107 configurationAPI.getBackendAPI = function () {
108 return configurationAPI.getBackendURL() + '/v2/api';
109 }
110
111
112
113 module.exports = configurationAPI;