4 * Copyright 2016 RIFT.IO Inc
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * Node configuration routes module.
22 * Provides a RESTful API to provide configuration
23 * details such as api_server.
24 * @module framework/core/modules/routes/configuration
25 * @author Kiran Kashalkar <kiran.kashalkar@riftio.com>
28 var cors
= require('cors');
29 var bodyParser
= require('body-parser');
30 var configurationAPI
= require('../api/configuration');
31 var Router
= require('express').Router();
32 var utils
= require('../../api_utils/utils');
33 var CONSTANTS
= require('../../api_utils/constants.js');
34 var request
= require('request');
35 var _
= require('lodash');
37 Router
.use(bodyParser
.json());
39 Router
.use(bodyParser
.urlencoded({
43 Router
.put('/server-configuration', cors(), function(req
, res
) {
44 configurationAPI
.update(req
).then(function(data
) {
45 utils
.sendSuccessResponse(data
, res
);
47 utils
.sendErrorResponse(error
, res
);
51 Router
.get('/server-configuration', cors(), function(req
, res
) {
52 configurationAPI
.get(req
).then(function(data
) {
53 utils
.sendSuccessResponse(data
, res
);
55 utils
.sendErrorResponse(error
, res
);
59 Router
.get('/check-auth', function(req
, res
) {
60 console
.log('testing auth')
61 var api_server
= req
.query
["api_server"];
62 var uri
= utils
.confdPort(api_server
) + '/api/config/';
64 checkAuth(uri
, req
).then(function(data
) {
65 utils
.sendSuccessResponse(data
, res
);
67 utils
.sendErrorResponse(error
, res
);
71 function checkAuth(uri
, req
){
72 return new Promise(function(resolve
, reject
) {
76 headers
: _
.extend({}, {
77 'Authorization': req
.get('Authorization'),
78 forever
: CONSTANTS
.FOREVER_ON
,
79 rejectUnauthorized
: false,
81 }, function(error
, response
, body
) {
82 console
.log(arguments
)
83 if( response
.statusCode
== 401) {
84 reject({statusCode
: 401, error
: response
.body
});
86 resolve({statusCode
:200, data
:response
.body
})
93 module
.exports
= Router
;