ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | const proxy = require('http-proxy-middleware'); |
| 4 | |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 5 | const devConfig = require('./tools/getDevConfig'); |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 6 | let devPort = process.env.PORT || devConfig.port; |
| 7 | |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 8 | module.exports = function(server) { |
| 9 | let cookieRules = devConfig.proxyConfig.cookieReplaceRules; |
| 10 | let cookies = devConfig.proxyConfig.cookies; |
| 11 | console.log('---------------------'); |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 12 | |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 13 | let proxyConfigDefaults = { |
| 14 | changeOrigin: true, |
| 15 | secure: false, |
| 16 | onProxyRes: (proxyRes, req, res) => { |
| 17 | let setCookie = proxyRes.headers['set-cookie']; |
| 18 | if (setCookie) { |
| 19 | cookieRules.forEach(function(rule) { |
| 20 | setCookie[0] = setCookie[0].replace( |
| 21 | rule.replace, |
| 22 | rule.with |
| 23 | ); |
| 24 | }); |
| 25 | } |
| 26 | if ( |
| 27 | proxyRes.statusCode === 302 && |
| 28 | proxyRes.headers.location.indexOf(devConfig.proxyConfig.login) > |
| 29 | -1 |
| 30 | ) { |
| 31 | proxyRes.headers.location = `http://localhost:${devPort}/${ |
| 32 | devConfig.proxyConfig.redirectionPath |
| 33 | }`; |
| 34 | let myCookies = []; |
| 35 | for (let cookie in cookies) { |
| 36 | myCookies.push(cookie + '=' + cookies[cookie]); |
| 37 | } |
| 38 | res.setHeader('Set-Cookie', myCookies); |
| 39 | } |
| 40 | } |
| 41 | }; |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 42 | |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 43 | let middlewares = [ |
| 44 | (req, res, next) => { |
| 45 | devConfig.proxyConfig.urlReplaceRules.forEach(function(rule) { |
| 46 | if (req.url.indexOf(rule.url) > -1) { |
| 47 | req.url = req.url.replace(rule.replace, rule.with); |
| 48 | } |
| 49 | }); |
| 50 | devConfig.proxyConfig.jsReplaceRules.forEach(function(rule) { |
| 51 | let regex = new RegExp('^(.*)' + rule.replace); |
| 52 | let match = req.url.match(regex); |
| 53 | let newUrl = match && match[1] + rule.with + '.js'; |
| 54 | if (newUrl) { |
| 55 | console.log(`REWRITING URL: ${req.url} -> ${newUrl}`); |
| 56 | req.url = newUrl; |
| 57 | } |
| 58 | }); |
| 59 | next(); |
| 60 | } |
| 61 | ]; |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 62 | |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 63 | let proxies = []; |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 64 | |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 65 | // standalone back-end (proxyTarget) has higher priority, so it should be first |
| 66 | if (devConfig.proxyTarget) { |
| 67 | console.log('Onboarding proxy set to : ' + devConfig.proxyTarget); |
| 68 | proxies.push({ |
| 69 | target: devConfig.proxyTarget, |
| 70 | config: devConfig.proxyConfig.onboardingProxy |
| 71 | }); |
| 72 | } else { |
| 73 | console.log( |
| 74 | 'Onboarding proxy set to : ' + devConfig.proxyCatalogTarget |
| 75 | ); |
| 76 | } |
| 77 | console.log('Catalog proxy set to : ' + devConfig.proxyCatalogTarget); |
| 78 | proxies.push({ |
| 79 | target: devConfig.proxyCatalogTarget, |
| 80 | config: devConfig.proxyConfig.catalogProxy |
| 81 | }); |
| 82 | proxies.forEach(function(p) { |
| 83 | middlewares.push( |
| 84 | proxy( |
| 85 | p.config.proxy, |
| 86 | Object.assign({}, proxyConfigDefaults, { |
| 87 | target: p.target, |
| 88 | pathRewrite: p.config.rewrite |
| 89 | }) |
| 90 | ) |
| 91 | ); |
| 92 | }); |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 93 | |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 94 | let websocketTarget = devConfig.proxyCatalogTarget; |
| 95 | if (devConfig.proxyWebsocketTarget) { |
| 96 | websocketTarget = devConfig.proxyWebsocketTarget; |
| 97 | } |
| 98 | console.log('Websocket proxy set to : ' + websocketTarget); |
| 99 | console.log('---------------------'); |
| 100 | var wsProxy = proxy( |
| 101 | devConfig.proxyConfig.websocketProxy.proxy, |
| 102 | Object.assign({}, proxyConfigDefaults, { |
| 103 | target: websocketTarget, |
| 104 | ws: true |
| 105 | }) |
| 106 | ); |
| 107 | middlewares.push(wsProxy); |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 108 | |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 109 | server.use(middlewares); |
| 110 | server.on('upgrade', wsProxy.upgrade); |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 111 | }; |