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