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) { |
ilanap | 1d8517d | 2021-01-17 17:09:35 +0200 | [diff] [blame] | 9 | console.log(''); |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 10 | console.log('---------------------'); |
ilanap | 1d8517d | 2021-01-17 17:09:35 +0200 | [diff] [blame] | 11 | console.log('---------------------'); |
| 12 | console.log('---------------------'); |
| 13 | console.log( |
| 14 | 'Local URL: http://localhost:' + devPort + '/sdc1/#!/onboardVendor' |
| 15 | ); |
| 16 | console.log('---------------------'); |
| 17 | console.log('---------------------'); |
| 18 | console.log('---------------------'); |
| 19 | console.log('Starting dev server with role: ' + devConfig.env.role); |
| 20 | let userType = devConfig.userTypes[devConfig.env.role]; |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 21 | |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 22 | let proxyConfigDefaults = { |
| 23 | changeOrigin: true, |
| 24 | secure: false, |
ilanap | 1d8517d | 2021-01-17 17:09:35 +0200 | [diff] [blame] | 25 | logLevel: 'debug', |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 26 | onProxyRes: (proxyRes, req, res) => { |
ilanap | 1d8517d | 2021-01-17 17:09:35 +0200 | [diff] [blame] | 27 | res.cookie( |
| 28 | devConfig.cookie.userIdSuffix, |
| 29 | req.headers[devConfig.cookie.userIdSuffix] || userType.userId |
| 30 | ); |
| 31 | res.cookie( |
| 32 | devConfig.cookie.userEmail, |
| 33 | req.headers[devConfig.cookie.userEmail] || userType.email |
| 34 | ); |
| 35 | res.cookie( |
| 36 | devConfig.cookie.userFirstName, |
| 37 | req.headers[devConfig.cookie.userFirstName] || |
| 38 | userType.firstName |
| 39 | ); |
| 40 | res.cookie( |
| 41 | devConfig.cookie.userLastName, |
| 42 | req.headers[devConfig.cookie.userLastName] || userType.lastName |
| 43 | ); |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 44 | if ( |
ilanap | 1d8517d | 2021-01-17 17:09:35 +0200 | [diff] [blame] | 45 | proxyRes && |
| 46 | proxyRes.headers && |
| 47 | proxyRes.headers.location && |
| 48 | proxyRes.headers.location.indexOf('login') > -1 |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 49 | ) { |
| 50 | proxyRes.headers.location = `http://localhost:${devPort}/${ |
| 51 | devConfig.proxyConfig.redirectionPath |
ilanap | 1d8517d | 2021-01-17 17:09:35 +0200 | [diff] [blame] | 52 | }`; |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | }; |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 56 | |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 57 | let middlewares = [ |
| 58 | (req, res, next) => { |
| 59 | devConfig.proxyConfig.urlReplaceRules.forEach(function(rule) { |
| 60 | if (req.url.indexOf(rule.url) > -1) { |
| 61 | req.url = req.url.replace(rule.replace, rule.with); |
ilanap | 1d8517d | 2021-01-17 17:09:35 +0200 | [diff] [blame] | 62 | next(); |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 63 | } |
| 64 | }); |
| 65 | devConfig.proxyConfig.jsReplaceRules.forEach(function(rule) { |
| 66 | let regex = new RegExp('^(.*)' + rule.replace); |
| 67 | let match = req.url.match(regex); |
ilanap | 1d8517d | 2021-01-17 17:09:35 +0200 | [diff] [blame] | 68 | let newUrl = match && match[1] + rule.with; |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 69 | if (newUrl) { |
| 70 | console.log(`REWRITING URL: ${req.url} -> ${newUrl}`); |
| 71 | req.url = newUrl; |
ilanap | 1d8517d | 2021-01-17 17:09:35 +0200 | [diff] [blame] | 72 | next(); |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 73 | } |
| 74 | }); |
| 75 | next(); |
| 76 | } |
| 77 | ]; |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 78 | |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 79 | let proxies = []; |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 80 | |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 81 | // standalone back-end (proxyTarget) has higher priority, so it should be first |
| 82 | if (devConfig.proxyTarget) { |
| 83 | console.log('Onboarding proxy set to : ' + devConfig.proxyTarget); |
| 84 | proxies.push({ |
| 85 | target: devConfig.proxyTarget, |
| 86 | config: devConfig.proxyConfig.onboardingProxy |
| 87 | }); |
| 88 | } else { |
| 89 | console.log( |
| 90 | 'Onboarding proxy set to : ' + devConfig.proxyCatalogTarget |
| 91 | ); |
| 92 | } |
| 93 | console.log('Catalog proxy set to : ' + devConfig.proxyCatalogTarget); |
| 94 | proxies.push({ |
| 95 | target: devConfig.proxyCatalogTarget, |
| 96 | config: devConfig.proxyConfig.catalogProxy |
| 97 | }); |
| 98 | proxies.forEach(function(p) { |
ilanap | 1d8517d | 2021-01-17 17:09:35 +0200 | [diff] [blame] | 99 | console.log( |
| 100 | 'adding: ' + p.target + ' with rewrite: ' + p.config.rewrite |
| 101 | ); |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 102 | middlewares.push( |
| 103 | proxy( |
| 104 | p.config.proxy, |
| 105 | Object.assign({}, proxyConfigDefaults, { |
| 106 | target: p.target, |
ilanap | 1d8517d | 2021-01-17 17:09:35 +0200 | [diff] [blame] | 107 | loglevel: 'debug', |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 108 | pathRewrite: p.config.rewrite |
| 109 | }) |
| 110 | ) |
| 111 | ); |
| 112 | }); |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 113 | |
ilanap | 1d8517d | 2021-01-17 17:09:35 +0200 | [diff] [blame] | 114 | if (devConfig.proxyConfig.websocketProxy.enabled) { |
| 115 | let websocketTarget = devConfig.proxyCatalogTarget; |
| 116 | if (devConfig.proxyWebsocketTarget) { |
| 117 | websocketTarget = devConfig.proxyWebsocketTarget; |
| 118 | } |
| 119 | console.log('Websocket proxy set to : ' + websocketTarget); |
| 120 | console.log('---------------------'); |
| 121 | var wsProxy = proxy( |
| 122 | devConfig.proxyConfig.websocketProxy.proxy, |
| 123 | Object.assign({}, proxyConfigDefaults, { |
| 124 | target: websocketTarget, |
| 125 | ws: true |
| 126 | }) |
| 127 | ); |
| 128 | middlewares.push(wsProxy); |
| 129 | server.use(middlewares); |
| 130 | server.on('upgrade', wsProxy.upgrade); |
| 131 | } else { |
| 132 | server.use(middlewares); |
Einav Weiss Keidar | f2c4723 | 2018-05-30 18:12:02 +0300 | [diff] [blame] | 133 | } |
ilanap | 1965d16 | 2018-01-04 11:34:59 +0200 | [diff] [blame] | 134 | }; |