summaryrefslogtreecommitdiffstats
path: root/openecomp-ui/proxy-server.js
blob: 6db8e61ac172c941f4c22111410621f5df073bf3 (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
@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.hig
'use strict';

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

const devConfig = require('./tools/getDevConfig');
let devPort = process.env.PORT || devConfig.port;

module.exports = function(server) {
    console.log('');
    console.log('---------------------');
    console.log('---------------------');
    console.log('---------------------');
    console.log(
        'Local URL: http://localhost:' + devPort + '/sdc1/#!/onboardVendor'
    );
    console.log('---------------------');
    console.log('---------------------');
    console.log('---------------------');
    console.log('Starting dev server with role: ' + devConfig.env.role);
    let userType = devConfig.userTypes[devConfig.env.role];

    let proxyConfigDefaults = {
        changeOrigin: true,
        secure: false,
        logLevel: 'debug',
        onProxyRes: (proxyRes, req, res) => {
            res.cookie(
                devConfig.cookie.userIdSuffix,
                req.headers[devConfig.cookie.userIdSuffix] || userType.userId
            );
            res.cookie(
                devConfig.cookie.userEmail,
                req.headers[devConfig.cookie.userEmail] || userType.email
            );
            res.cookie(
                devConfig.cookie.userFirstName,
                req.headers[devConfig.cookie.userFirstName] ||
                userType.firstName
            );
            res.cookie(
                devConfig.cookie.userLastName,
                req.headers[devConfig.cookie.userLastName] || userType.lastName
            );
            if (
                proxyRes &&
                proxyRes.headers &&
                proxyRes.headers.location &&
                proxyRes.headers.location.indexOf('login') > -1
            ) {
                proxyRes.headers.location = `http://localhost:${devPort}/${
                    devConfig.proxyConfig.redirectionPath
                    }`;
            }
        }
    };

    let middlewares = [
        (req, res, next) => {
            devConfig.proxyConfig.urlReplaceRules.forEach(function(rule) {
                if (req.url.indexOf(rule.url) > -1) {
                    req.url = req.url.replace(rule.replace, rule.with);
                    next();
                }
            });
            devConfig.proxyConfig.jsReplaceRules.forEach(function(rule) {
                let regex = new RegExp('^(.*)' + rule.replace);
                let match = req.url.match(regex);
                let newUrl = match && match[1] + rule.with;
                if (newUrl) {
                    console.log(`REWRITING URL: ${req.url} -> ${newUrl}`);
                    req.url = newUrl;
                    next();
                }
            });
            next();
        }
    ];

    let proxies = [];

    // standalone back-end (proxyTarget) has higher priority, so it should be first
    if (devConfig.proxyTarget) {
        console.log('Onboarding proxy set to : ' + devConfig.proxyTarget);
        proxies.push({
            target: devConfig.proxyTarget,
            config: devConfig.proxyConfig.onboardingProxy
        });
    } else {
        console.log(
            'Onboarding proxy set to : ' + devConfig.proxyCatalogTarget
        );
    }
    console.log('Catalog proxy set to : ' + devConfig.proxyCatalogTarget);
    proxies.push({
        target: devConfig.proxyCatalogTarget,
        config: devConfig.proxyConfig.catalogProxy
    });
    proxies.forEach(function(p) {
        console.log(
            'adding: ' + p.target + ' with rewrite: ' + p.config.rewrite
        );
        middlewares.push(
            proxy(
                p.config.proxy,
                Object.assign({}, proxyConfigDefaults, {
                    target: p.target,
                    loglevel: 'debug',
                    pathRewrite: p.config.rewrite
                })
            )
        );
    });

    if (devConfig.proxyConfig.websocketProxy.enabled) {
        let websocketTarget = devConfig.proxyCatalogTarget;
        if (devConfig.proxyWebsocketTarget) {
            websocketTarget = devConfig.proxyWebsocketTarget;
        }
        console.log('Websocket proxy set to : ' + websocketTarget);
        console.log('---------------------');
        var wsProxy = proxy(
            devConfig.proxyConfig.websocketProxy.proxy,
            Object.assign({}, proxyConfigDefaults, {
                target: websocketTarget,
                ws: true
            })
        );
        middlewares.push(wsProxy);
        server.use(middlewares);
        server.on('upgrade', wsProxy.upgrade);
    } else {
        server.use(middlewares);
    }
};