Fix 404 in sdc-FE calls to workflow
[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/rest' to feHost
60             middlewares.push(
61                 proxy(['/sdc1/rest'],{
62                     target: protocol + '://' + feHost + ':' + fePort,
63                     changeOrigin: true,
64                     secure: false
65                 }));
66
67             // Redirect dcae urls to feHost
68             middlewares.push(
69                 proxy(['/dcae','/sdc1/feProxy/dcae-api'], {
70                     target: protocol + '://' + feHost + ':' + fePort,
71                     changeOrigin: true,
72                     secure: false,
73                     onProxyRes: (proxyRes, req, res) => {
74                         let setCookie = proxyRes.headers['set-cookie'];
75                         if (setCookie) {
76                             setCookie[0] = setCookie[0].replace(/\bSecure\b(; )?/, '');
77                         }
78                     }
79                 }));
80
81             // Redirect onboarding urls to feHost
82             middlewares.push(
83                 proxy(['/onboarding', '/sdc1/feProxy/onboarding-api'], {
84                     target: protocol + '://' + feHost + ':' + fePort,
85                     changeOrigin: true,
86                     secure: false,
87                     onProxyRes: (proxyRes, req, res) => {
88                         let setCookie = proxyRes.headers['set-cookie'];
89                         if (setCookie) {
90                             setCookie[0] = setCookie[0].replace(/\bSecure\b(; )?/, '');
91                         }
92                     }
93                 }));
94
95             // Redirect workflow urls to feHost
96             middlewares.push(
97                 proxy(['/wf', '/sdc1/feProxy/wf'], {
98                     target: protocol + '://' + feHost + ':' + fePort,
99                     changeOrigin: true,
100                     logLevel: 'debug',
101                     secure: false,
102                     onProxyRes: (proxyRes, req, res) => {
103                         let setCookie = proxyRes.headers['set-cookie'];
104                         if (setCookie) {
105                             setCookie[0] = setCookie[0].replace(/\bSecure\b(; )?/, '');
106                         }
107                     }
108                 }));
109             server.use(middlewares);
110         }
111     };
112
113     return serverConfig;
114 };