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