diff options
author | ilanap <ilanap@amdocs.com> | 2018-01-04 11:34:59 +0200 |
---|---|---|
committer | Avi Gaffa <avi.gaffa@amdocs.com> | 2018-01-04 12:43:23 +0000 |
commit | 1965d1663135b880b9774bca2630260e6f28d198 (patch) | |
tree | 41e257652bee2451402df284fd43138cf2fa91a2 /openecomp-ui/proxy-server.js | |
parent | bd33bd2374a63b10414e1b14d32d596188fb1957 (diff) |
Replace restful-js with Axios
Change-Id: If47c5b7708885e84d632255557543d292f3ccd69
Issue-ID: SDC-869
Signed-off-by: ilanap <ilanap@amdocs.com>
Diffstat (limited to 'openecomp-ui/proxy-server.js')
-rw-r--r-- | openecomp-ui/proxy-server.js | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/openecomp-ui/proxy-server.js b/openecomp-ui/proxy-server.js new file mode 100644 index 0000000000..3c4c32cc35 --- /dev/null +++ b/openecomp-ui/proxy-server.js @@ -0,0 +1,102 @@ +'use strict'; + +const proxy = require('http-proxy-middleware'); + +let localDevConfig = {}; +try { + localDevConfig = require('./devConfig'); +} catch (e) {} +const devConfig = Object.assign({}, require('./devConfig.defaults'), localDevConfig); +let devPort = process.env.PORT || devConfig.port; + + +module.exports = function (server) { + let cookieRules = devConfig.proxyConfig.cookieReplaceRules; + let cookies = devConfig.proxyConfig.cookies; + console.log('---------------------'); + + let proxyConfigDefaults = { + changeOrigin: true, + secure: false, + onProxyRes: (proxyRes, req, res) => { + let setCookie = proxyRes.headers['set-cookie']; + if (setCookie) { + cookieRules.forEach(function(rule) { + setCookie[0] = setCookie[0].replace(rule.replace, rule.with); + }); + } + if (proxyRes.statusCode === 302 && proxyRes.headers.location.indexOf(devConfig.proxyConfig.login) > -1) { + proxyRes.headers.location = `http://localhost:${devPort}/${devConfig.proxyConfig.redirectionPath}`; + let myCookies = []; + for (let cookie in cookies) { + myCookies.push(cookie + '=' + cookies[cookie]); + } + res.setHeader('Set-Cookie', myCookies); + } + } + }; + + let middlewares = [ + (req, res, next) => { + devConfig.proxyConfig.urlReplaceRules.forEach(function(rule) { + if (req.url.indexOf(rule.url) > -1) { + req.url = req.url.replace(rule.replace, rule.with); + } + }); + devConfig.proxyConfig.jsReplaceRules.forEach(function(rule) { + let regex = new RegExp('^(.*)' + rule.replace); + let match = req.url.match(regex); + let newUrl = match && match[1] + rule.with + '.js'; + if (newUrl) { + console.log(`REWRITING URL: ${req.url} -> ${newUrl}`); + req.url = newUrl; + } + }); + next(); + } + ]; + + + let proxies = []; + + // standalone back-end (proxyTarget) has higher priority, so it should be first + if (devConfig.proxyTarget) { + console.log('Onboarding proxy set to : ' + devConfig.proxyTarget); + proxies.push({ + target : devConfig.proxyTarget, + config: devConfig.proxyConfig.onboardingProxy} + ); + } else { + console.log('Catalog proxy set to : ' + devConfig.proxyCatalogTarget); + } + console.log('Catalog proxy set to : ' + devConfig.proxyCatalogTarget); + proxies.push({ + target : devConfig.proxyCatalogTarget, + config: devConfig.proxyConfig.catalogProxy} + ); + proxies.forEach(function(p) { + middlewares.push( + proxy(p.config.proxy, Object.assign({}, proxyConfigDefaults, { + target: p.target, + pathRewrite: p.config.rewrite + })) + ); + + }); + + let websocketTarget = devConfig.proxyCatalogTarget; + if (devConfig.proxyWebsocketTarget) { + websocketTarget = devConfig.proxyWebsocketTarget; + } + console.log('Websocket proxy set to : ' + websocketTarget); + console.log('---------------------'); + var wsProxy = proxy(devConfig.proxyConfig.websocketProxy.proxy, Object.assign({}, proxyConfigDefaults, { + target: websocketTarget, + ws: true + })) + middlewares.push(wsProxy); + + + server.use(middlewares); + server.on('upgrade', wsProxy.upgrade); +}; |