aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-ui/proxy-server.js
blob: 4733d38a899afcb21cfbbf857fb47b669f744369 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict';

const proxy = require('http-proxy-middleware');

let localDevConfig = {};
try {
	localDevConfig = require('./devConfig');
} catch (e) {}
const devConfig = Object.assign({}, require('./devConfig.defaults'), localDevConfig);
let devPort = process.env.PORT || devConfig.port;

let jsonConfig = {
	"appContextPath" : "/onboarding"
};

try {
	jsonConfig = require('./src/sdc-app/config/config.json');
} catch (e) {
	console.log('could not load config. using deault value instead');
}

module.exports = function (server) {
	let proxyConfigDefaults = {
		changeOrigin: true,
		secure: false,
		onProxyRes: (proxyRes, req, res) => {
			let setCookie = proxyRes.headers['set-cookie'];
			if (setCookie) {
				setCookie[0] = setCookie[0].replace(/\bSecure\b(; )?/, '');
			}
			if (proxyRes.statusCode === 302 && proxyRes.headers.location.indexOf('login') > -1) {
				proxyRes.headers.location = `http://localhost:${devPort}/sdc1#/onboardVendor`;
				res.setHeader('Set-Cookie', [
					'HTTP_CSP_EMAIL=csantana@sdc.com',
					'HTTP_CSP_FIRSTNAME=Carlos',
					'HTTP_CSP_LASTNAME=Santana',
					'HTTP_CSP_WSTYPE=Intranet',
					'HTTP_IV_REMOTE_ADDRESS=0.0.0.0',
					'HTTP_IV_USER=cs0008',
					'USER_ID=cs0008'
				]);
			}
		}
	};

	let middlewares = [
		(req, res, next) => {
			if (req.url.indexOf('/proxy-designer1') > -1) {
				req.url = req.url.replace('/proxy-designer1', '');
			}

			if (req.url.indexOf(jsonConfig.appContextPath + '/resources') > -1) {
				req.url = req.url.replace(jsonConfig.appContextPath, '');
			}

			let match = req.url.match(/^(.*)_en.js$/);
			let newUrl = match && match[1] + '.js';
			if (newUrl) {
				console.log(`REWRITING URL: ${req.url} -> ${newUrl}`);
				req.url = newUrl;
			}
			next();
		}
	];

	// standalon back-end (proxyTarget) has higher priority, so it should be first
	if (devConfig.proxyTarget) {
		middlewares.push(
			proxy(['/api', '/onboarding-api', '/sdc1/feProxy/onboarding-api'], Object.assign({}, proxyConfigDefaults, {
				target: devConfig.proxyTarget,
				pathRewrite: {
					'/sdc1/feProxy/onboarding-api': '/onboarding-api'
				}
			}))
		);
	}

	// ATT environment (proxyATTTarget) has lower priority, so it should be second
	if (devConfig.proxyATTTarget) {
		middlewares.push(
			proxy(['/sdc1', '/onboarding-api', '/scripts', '/styles'], Object.assign({}, proxyConfigDefaults, {
				target: devConfig.proxyATTTarget,
				pathRewrite: {
					// Workaround for some weird proxy issue
					'/sdc1/feProxy/onboarding-api': '/sdc1/feProxy/onboarding-api',
					'/onboarding-api': '/sdc1/feProxy/onboarding-api'
				}
			}))
		);
	}
	server.use(middlewares);
};