bugfix for operations screen
[sdc.git] / catalog-ui / webpack.server.js
1 const mockApis = require('./configurations/mock.json').sdcConfig;
2 const proxy = require('http-proxy-middleware');
3 const devPort = 9000;
4
5 const fePort = 8181;
6 const feHost = "localhost";
7 const protocol="http";
8 const isDirectToFE = false;
9
10 /*
11 For kubernetes
12 const fePort = 30207;
13 const wfPort = 30256;
14 const feHost = "kubernetes_master";
15 const protocol="https";
16 const isDirectToFE = true;// whether to proxy to the k8s proxy or to the BE
17 */
18 const portalCookieValue = "randomValue"; //for dev solely, in production - the webseal would add the cookie by itself.
19
20 module.exports = function (env) {
21
22     // Set default user role
23     if (!env) {
24         env = {
25             role: "designer"
26         };
27     }
28     console.log("Starting dev server with role: " + env.role);
29
30     const serverConfig = {
31         port: devPort,
32         historyApiFallback: true,
33         inline: true,
34         stats: {
35             colors: true,
36             exclude: ['node_modules']
37         },
38         setup: server => {
39             let userType = mockApis.userTypes[env.role];
40
41             let middlewares = [
42                 (req, res, next) => {
43                     res.cookie(mockApis.cookie.userIdSuffix, req.headers[mockApis.cookie.userIdSuffix] || userType.userId);
44                     res.cookie(mockApis.cookie.userEmail, req.headers[mockApis.cookie.userEmail] || userType.email);
45                     res.cookie(mockApis.cookie.userFirstName, req.headers[mockApis.cookie.userFirstName] || userType.firstName);
46                     res.cookie(mockApis.cookie.userLastName, req.headers[mockApis.cookie.userLastName] || userType.lastName);
47                     res.cookie(mockApis.cookie.portalCookie, portalCookieValue);
48                     next();
49                 }
50             ];
51
52             // Redirect all '/sdc1/feProxy/rest' to feHost
53             let feProxyOptions = {
54                 target: protocol + '://' + feHost + ':' + fePort,
55                 changeOrigin: true,
56                 secure: false,
57                 logLevel: 'debug'
58             }    
59             if (isDirectToFE) {
60                 feProxyOptions.pathRewrite= {
61                     '^/sdc1/feProxy/rest' : '/sdc1/feProxy/rest'
62                 }
63             } else {
64                 feProxyOptions.pathRewrite= {
65                     '^/sdc1/feProxy/rest' : '/sdc2/rest'
66                 }
67             }    
68             middlewares.push(
69                 proxy(['/sdc1/feProxy/rest'], feProxyOptions));
70
71             // Redirect all '/sdc1/rest' to feHost
72             middlewares.push(
73                 proxy(['/sdc1/rest'],{
74                     target: protocol + '://' + feHost + ':' + fePort,
75                     changeOrigin: true,
76                     secure: false
77                 }));
78
79             // Redirect dcae urls to feHost
80             middlewares.push(
81                 proxy(['/dcae','/sdc1/feProxy/dcae-api'], {
82                     target: protocol + '://' + feHost + ':' + fePort,
83                     changeOrigin: true,
84                     secure: false,
85                     onProxyRes: (proxyRes, req, res) => {
86                         let setCookie = proxyRes.headers['set-cookie'];
87                         if (setCookie) {
88                             setCookie[0] = setCookie[0].replace(/\bSecure\b(; )?/, '');
89                         }
90                     }
91                 }));
92
93             // Redirect onboarding urls to feHost
94             middlewares.push(
95                 proxy(['/onboarding', '/sdc1/feProxy/onboarding-api'], {
96                     target: protocol + '://' + feHost + ':' + fePort,
97                     changeOrigin: true,
98                     secure: false,
99                     onProxyRes: (proxyRes, req, res) => {
100                         let setCookie = proxyRes.headers['set-cookie'];
101                         if (setCookie) {
102                             setCookie[0] = setCookie[0].replace(/\bSecure\b(; )?/, '');
103                         }
104                     }
105                 }));
106
107             // Redirect workflow urls to feHost
108             middlewares.push(
109                 proxy(['/sdc1/feProxy/wf', '/wf'], {
110                     target: protocol + '://' + feHost + ':' + wfPort,
111                     changeOrigin: true,
112                     logLevel: 'debug',
113                     secure: false,
114                     pathRewrite: {
115                         '^/sdc1/feProxy' : ''
116                     },
117                     onProxyRes: (proxyRes, req, res) => {
118                         let setCookie = proxyRes.headers['set-cookie'];
119                         if (setCookie) {
120                             setCookie[0] = setCookie[0].replace(/\bSecure\b(; )?/, '');
121                         }
122                     }
123                 }));
124             server.use(middlewares);
125         }
126     };
127
128     return serverConfig;
129 };