Fix dev config designer user in catalog-ui
[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 const fePort = 8181;
5 const feHost = "localhost";
6 const portalCookieValue = "randomValue"; //for dev solely, in production - the webseal would add the cookie by itself.
7
8 module.exports = function (env) {
9
10     // Set default user role
11     if (!env) {
12         env = {
13             role: "designer"
14         };
15     }
16     console.log("Starting dev server with role: " + env.role);
17
18     const serverConfig = {
19         port: devPort,
20         historyApiFallback: true,
21         inline: true,
22         stats: {
23             colors: true,
24             exclude: ['node_modules']
25         },
26         setup: server => {
27             let userType = mockApis.userTypes[env.role];
28
29             let middlewares = [
30                 (req, res, next) => {
31                     res.cookie(mockApis.cookie.userIdSuffix, req.headers[mockApis.cookie.userIdSuffix] || userType.userId);
32                     res.cookie(mockApis.cookie.userEmail, req.headers[mockApis.cookie.userEmail] || userType.email);
33                     res.cookie(mockApis.cookie.userFirstName, req.headers[mockApis.cookie.userFirstName] || userType.firstName);
34                     res.cookie(mockApis.cookie.userLastName, req.headers[mockApis.cookie.userLastName] || userType.lastName);
35                     res.cookie(mockApis.cookie.portalCookie, portalCookieValue);
36                     next();
37                 }
38             ];
39
40             // Redirect all '/sdc1/feProxy/rest' to feHost
41             middlewares.push(
42                 proxy(['/sdc1/feProxy/rest', '/sdc1/feProxy/uicache'], {
43                     target: 'http://' + feHost + ':' + fePort,
44                     changeOrigin: true,
45                     secure: false
46                 }));
47
48             // Redirect all '/sdc1/rest' to feHost
49             middlewares.push(
50                 proxy(['/sdc1/rest'], {
51                     target: 'http://' + feHost + ':' + fePort,
52                     changeOrigin: true,
53                     secure: false
54                 }));
55
56             // Redirect dcae urls to feHost
57             middlewares.push(
58                 proxy(['/dcae', '/sdc1/feProxy/dcae-api'], {
59                     target: 'http://' + feHost + ':' + fePort,
60                     changeOrigin: true,
61                     secure: false,
62                     onProxyRes: (proxyRes, req, res) => {
63                         let setCookie = proxyRes.headers['set-cookie'];
64                         if (setCookie) {
65                             setCookie[0] = setCookie[0].replace(/\bSecure\b(; )?/, '');
66                         }
67                     }
68                 }));
69
70             // Redirect onboarding urls to feHost
71             middlewares.push(
72                 proxy(['/onboarding', '/sdc1/feProxy/onboarding-api'], {
73                     target: 'http://' + feHost + ':' + fePort,
74                     changeOrigin: true,
75                     secure: false,
76                     onProxyRes: (proxyRes, req, res) => {
77                         let setCookie = proxyRes.headers['set-cookie'];
78                         if (setCookie) {
79                             setCookie[0] = setCookie[0].replace(/\bSecure\b(; )?/, '');
80                         }
81                     }
82                 }));
83
84             server.use(middlewares);
85         }
86     };
87
88     return serverConfig;
89 };