blob: 6db8e61ac172c941f4c22111410621f5df073bf3 [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) {
ilanap1d8517d2021-01-17 17:09:35 +02009 console.log('');
Einav Weiss Keidarf2c47232018-05-30 18:12:02 +030010 console.log('---------------------');
ilanap1d8517d2021-01-17 17:09:35 +020011 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];
ilanap1965d162018-01-04 11:34:59 +020021
Einav Weiss Keidarf2c47232018-05-30 18:12:02 +030022 let proxyConfigDefaults = {
23 changeOrigin: true,
24 secure: false,
ilanap1d8517d2021-01-17 17:09:35 +020025 logLevel: 'debug',
Einav Weiss Keidarf2c47232018-05-30 18:12:02 +030026 onProxyRes: (proxyRes, req, res) => {
ilanap1d8517d2021-01-17 17:09:35 +020027 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 Keidarf2c47232018-05-30 18:12:02 +030044 if (
ilanap1d8517d2021-01-17 17:09:35 +020045 proxyRes &&
46 proxyRes.headers &&
47 proxyRes.headers.location &&
48 proxyRes.headers.location.indexOf('login') > -1
Einav Weiss Keidarf2c47232018-05-30 18:12:02 +030049 ) {
50 proxyRes.headers.location = `http://localhost:${devPort}/${
51 devConfig.proxyConfig.redirectionPath
ilanap1d8517d2021-01-17 17:09:35 +020052 }`;
Einav Weiss Keidarf2c47232018-05-30 18:12:02 +030053 }
54 }
55 };
ilanap1965d162018-01-04 11:34:59 +020056
Einav Weiss Keidarf2c47232018-05-30 18:12:02 +030057 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);
ilanap1d8517d2021-01-17 17:09:35 +020062 next();
Einav Weiss Keidarf2c47232018-05-30 18:12:02 +030063 }
64 });
65 devConfig.proxyConfig.jsReplaceRules.forEach(function(rule) {
66 let regex = new RegExp('^(.*)' + rule.replace);
67 let match = req.url.match(regex);
ilanap1d8517d2021-01-17 17:09:35 +020068 let newUrl = match && match[1] + rule.with;
Einav Weiss Keidarf2c47232018-05-30 18:12:02 +030069 if (newUrl) {
70 console.log(`REWRITING URL: ${req.url} -> ${newUrl}`);
71 req.url = newUrl;
ilanap1d8517d2021-01-17 17:09:35 +020072 next();
Einav Weiss Keidarf2c47232018-05-30 18:12:02 +030073 }
74 });
75 next();
76 }
77 ];
ilanap1965d162018-01-04 11:34:59 +020078
Einav Weiss Keidarf2c47232018-05-30 18:12:02 +030079 let proxies = [];
ilanap1965d162018-01-04 11:34:59 +020080
Einav Weiss Keidarf2c47232018-05-30 18:12:02 +030081 // 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) {
ilanap1d8517d2021-01-17 17:09:35 +020099 console.log(
100 'adding: ' + p.target + ' with rewrite: ' + p.config.rewrite
101 );
Einav Weiss Keidarf2c47232018-05-30 18:12:02 +0300102 middlewares.push(
103 proxy(
104 p.config.proxy,
105 Object.assign({}, proxyConfigDefaults, {
106 target: p.target,
ilanap1d8517d2021-01-17 17:09:35 +0200107 loglevel: 'debug',
Einav Weiss Keidarf2c47232018-05-30 18:12:02 +0300108 pathRewrite: p.config.rewrite
109 })
110 )
111 );
112 });
ilanap1965d162018-01-04 11:34:59 +0200113
ilanap1d8517d2021-01-17 17:09:35 +0200114 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 Keidarf2c47232018-05-30 18:12:02 +0300133 }
ilanap1965d162018-01-04 11:34:59 +0200134};