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.
22 * Utils for use across the api_server.
23 * @module framework/core/utils
26 var fs
= require('fs');
27 var request
= require('request');
28 var Promise
= require('promise');
29 var CONSTANTS
= require('./constants.js');
30 var CONFD_PORT
= '8008';
31 var APIVersion
= '/v1';
32 var _
= require('lodash');
34 var requestWrapper = function(request
) {
35 if (process
.env
.HTTP_PROXY
&& process
.env
.HTTP_PROXY
!= '') {
36 request
= request
.defaults({
37 'proxy': process
.env
.HTTP_PROXY
43 var confdPort = function(api_server
) {
45 api_server
= api_server
.replace(api_server
.match(/[0-9](:[0-9]+)/)[1], '')
49 return api_server
+ ':' + CONFD_PORT
;
53 var validateResponse = function(callerName
, error
, response
, body
, resolve
, reject
) {
57 console
.log('Problem with "', callerName
, '": ', error
);
60 error
: 'Problem with ' + callerName
+ ': ' + error
64 } else if (response
.statusCode
>= 400) {
65 console
.log('Problem with "', callerName
, '": ', response
.statusCode
, ':', body
);
66 res
.statusCode
= response
.statusCode
;
69 if (response
.statusCode
== 401) {
71 error
: 'Authentication needed' + body
78 error
: 'Problem with ' + callerName
+ ': ' + response
.statusCode
+ ': ' + typeof(body
) == 'string' ? body
: JSON
.stringify(body
),
84 } else if (response
.statusCode
== 204) {
86 statusCode
: response
.statusCode
,
96 var checkAuthorizationHeader = function(req
) {
97 return new Promise(function(resolve
, reject
) {
98 if (req
.get('Authorization') == null) {
106 if (process
.env
.LOG_REQUESTS
) {
107 var logFile
= process
.env
.REQUESTS_LOG_FILE
;
109 if (logFile
&& logFile
!= '') {
110 validateResponse = function(callerName
, error
, response
, body
, resolve
, reject
) {
114 console
.log('Problem with "', callerName
, '": ', error
);
115 res
.statusCode
= 500;
117 error
: 'Problem with ' + callerName
+ ': ' + error
120 fs
.appendFileSync(logFile
, 'Request API: ' + response
.request
.uri
.href
+ ' ; ' + 'Error: ' + error
);
122 } else if (response
.statusCode
>= 400) {
123 console
.log('Problem with "', callerName
, '": ', response
.statusCode
, ':', body
);
124 res
.statusCode
= response
.statusCode
;
127 if (response
.statusCode
== 401) {
129 error
: 'Authentication needed' + body
132 fs
.appendFileSync(logFile
, 'Request API: ' + response
.request
.uri
.href
+ ' ; ' + 'Error Body: ' + body
);
137 error
: 'Problem with ' + callerName
+ ': ' + response
.statusCode
+ ': ' + body
141 fs
.appendFileSync(logFile
, 'Request API: ' + response
.request
.uri
.href
+ ' ; ' + 'Error Body: ' + body
);
143 } else if (response
.statusCode
== 204) {
145 fs
.appendFileSync(logFile
, 'Request API: ' + response
.request
.uri
.href
+ ' ; ' + 'Response Body: ' + body
);
148 fs
.appendFileSync(logFile
, 'Request API: ' + response
.request
.uri
.href
+ ' ; ' + 'Response Body: ' + body
);
156 * Serve the error response back back to HTTP requester
157 * @param {Object} error - object of the format
159 * statusCode - HTTP code to respond back with
160 * error - actual error JSON object to serve
162 * @param {Function} res - a handle to the express response function
164 var sendErrorResponse = function(error
, res
) {
165 res
.status(error
.statusCode
);
170 * Serve the success response back to HTTP requester
171 * @param {Object} response - object of the format
173 * statusCode - HTTP code to respond back with
174 * data - actual data JSON object to serve
176 * @param {Function} res - a handle to the express response function
178 var sendSuccessResponse = function(response
, res
) {
179 res
.status(response
.statusCode
);
180 res
.send(response
.data
);
183 var passThroughConstructor = function(app
) {
184 app
.get('/passthrough/:type/*', function(req
, res
) {
185 var url
= req
.params
[0];
186 var type
= req
.params
.type
;
187 var api_server
= req
.query
["api_server"];
188 var uri
= confdPort(api_server
) + APIVersion
+ '/' + url
+ '?deep';
189 // Check that type is valid
198 new Promise(function(resolve
, reject
) {
202 headers
: _
.extend({}, CONSTANTS
.HTTP_HEADERS
.accept
[type
], {
203 'Authorization': req
.get('Authorization'),
204 forever
: CONSTANTS
.FOREVER_ON
,
205 rejectUnauthorized
: false,
207 }, function(error
, response
, body
) {
208 if (validateResponse('Passthrough: ' + url
, error
, response
, body
, resolve
, reject
)) {
209 resolve(JSON
.parse(response
.body
))
212 }).then(function(data
) {
215 res
.send({'error': error
, uri
: uri
})
220 var getPortForProtocol = function(protocol
) {
231 * Ensure confd port is on api_server variable.
233 confdPort
: confdPort
,
235 validateResponse
: validateResponse
,
237 checkAuthorizationHeader
: checkAuthorizationHeader
,
239 request
: requestWrapper
.call(null, request
),
241 sendErrorResponse
: sendErrorResponse
,
243 sendSuccessResponse
: sendSuccessResponse
,
245 passThroughConstructor
: passThroughConstructor
,
247 getPortForProtocol
: getPortForProtocol