Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save abdulmuneer22/410de4aff682318ad46646ba3d9c0b9b to your computer and use it in GitHub Desktop.
Save abdulmuneer22/410de4aff682318ad46646ba3d9c0b9b to your computer and use it in GitHub Desktop.
'use strict';
// proxy configurator on apigateway
var httpProxy = require('http-proxy');
var HttpProxyRules = require('http-proxy-rules');
// // Set up proxy rules instance for routing
// var proxyRules = new HttpProxyRules({
// rules: {
// '.*/api/': 'http://127.0.0.1:8093/api/',
// },
// default: 'http://127.0.0.1:8093/api/', // default target
// });
var proxyRulesLocal = new HttpProxyRules({
rules: {
'.*/explorer/interactionservice':
'http://127.0.0.1:3001/explorer/interactionservice/',
'.*/api/interactionservice':
'http://127.0.0.1:3001/api/interactionservice/',
'.*/explorer/matchservice': 'http://127.0.0.1:8181/explorer/matchservice/',
'.*/api/matchservice': 'http://127.0.0.1:8181/api/matchservice/',
'.*/explorer/notification': 'http://127.0.0.1:3002/explorer/notification/',
'.*/api/notification': 'http://127.0.0.1:3002/api/notification/',
'.*/explorer/user-management':
'http://127.0.0.1:3003/explorer/user-management/',
'.*/api/user-management': 'http://127.0.0.1:3003/api/user-management/',
'.*/explorer/configurator':
'http://127.0.0.1:3004/explorer/configurator/',
'.*/api/configurator': 'http://127.0.0.1:3004/api/configurator/',
},
default: 'http://127.0.0.1:3001/api/interactionservice:8888/explorer/', // default target
});
// Create reverse proxy instance
var proxy = httpProxy.createProxy();
function forwardToInternalPath(req, res) {
// debugger
// a match method is exposed on the proxy rules instance to test a request to
// see if it matches against one of the specified rules
var target, accessToken;
var url = req.url;
var urls = String(url).split('/');
if (urls[1] === 'explorer') {
target = proxyRulesLocal.match(req);
if (target) {
return proxy.web(req, res, {
target: target,
});
} else {
res.writeHead(500, {
'Content-Type': 'text/plain',
});
res.send('Invalid request!!');
}
} else {
// api query
if (req.headers.access_token !== 'test') {
res.send(401, 'Invalid Access Token , Access Denied');
} else {
target = proxyRulesLocal.match(req);
if (target) {
return proxy.web(req, res, {
target: target,
});
} else {
res.writeHead(500, {
'Content-Type': 'text/plain',
});
res.send('Invalid request!!');
}
}
}
}
module.exports = function enableAuthentication(server) {
server.use(function(req, res, next) {
if (req.path) {
forwardToInternalPath(req, res);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment