blob: 99685424e213ac235e5996e753d41ffea3dfe704 [file] [log] [blame]
ilanap1965d162018-01-04 11:34:59 +02001'use strict';
2
3const proxy = require('http-proxy-middleware');
4
Einav Weiss Keidarf2c47232018-05-30 18:12:02 +03005const devConfig = require('./tools/getDevConfig');
ilanap1965d162018-01-04 11:34:59 +02006let devPort = process.env.PORT || devConfig.port;
7
Einav Weiss Keidarf2c47232018-05-30 18:12:02 +03008module.exports = function(server) {
9 let cookieRules = devConfig.proxyConfig.cookieReplaceRules;
10 let cookies = devConfig.proxyConfig.cookies;
11 console.log('---------------------');
ilanap1965d162018-01-04 11:34:59 +020012
Einav Weiss Keidarf2c47232018-05-30 18:12:02 +030013 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 };
ilanap1965d162018-01-04 11:34:59 +020042
Einav Weiss Keidarf2c47232018-05-30 18:12:02 +030043 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 ];
ilanap1965d162018-01-04 11:34:59 +020062
Einav Weiss Keidarf2c47232018-05-30 18:12:02 +030063 let proxies = [];
ilanap1965d162018-01-04 11:34:59 +020064
Einav Weiss Keidarf2c47232018-05-30 18:12:02 +030065 // 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 });
ilanap1965d162018-01-04 11:34:59 +020093
Einav Weiss Keidarf2c47232018-05-30 18:12:02 +030094 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);
ilanap1965d162018-01-04 11:34:59 +0200108
Einav Weiss Keidarf2c47232018-05-30 18:12:02 +0300109 server.use(middlewares);
110 server.on('upgrade', wsProxy.upgrade);
ilanap1965d162018-01-04 11:34:59 +0200111};