Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 1 | const mockApis = require('./configurations/mock.json').sdcConfig; |
| 2 | const proxy = require('http-proxy-middleware'); |
| 3 | const devPort = 9000; |
andre.schmid | c5d39ed | 2020-02-17 10:11:54 +0000 | [diff] [blame^] | 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. |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 7 | |
andre.schmid | c5d39ed | 2020-02-17 10:11:54 +0000 | [diff] [blame^] | 8 | module.exports = function (env) { |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 9 | |
andre.schmid | c5d39ed | 2020-02-17 10:11:54 +0000 | [diff] [blame^] | 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); |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 17 | |
andre.schmid | c5d39ed | 2020-02-17 10:11:54 +0000 | [diff] [blame^] | 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]; |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 28 | |
andre.schmid | c5d39ed | 2020-02-17 10:11:54 +0000 | [diff] [blame^] | 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 | ]; |
Michael Lando | a544510 | 2018-03-04 14:53:33 +0200 | [diff] [blame] | 39 | |
andre.schmid | c5d39ed | 2020-02-17 10:11:54 +0000 | [diff] [blame^] | 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 | })); |
Michael Lando | a544510 | 2018-03-04 14:53:33 +0200 | [diff] [blame] | 47 | |
andre.schmid | c5d39ed | 2020-02-17 10:11:54 +0000 | [diff] [blame^] | 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 | })); |
Idan Amit | fad1d73 | 2018-03-13 19:59:46 +0200 | [diff] [blame] | 55 | |
andre.schmid | c5d39ed | 2020-02-17 10:11:54 +0000 | [diff] [blame^] | 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 | })); |
Michael Lando | a544510 | 2018-03-04 14:53:33 +0200 | [diff] [blame] | 69 | |
andre.schmid | c5d39ed | 2020-02-17 10:11:54 +0000 | [diff] [blame^] | 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 | })); |
Michael Lando | a544510 | 2018-03-04 14:53:33 +0200 | [diff] [blame] | 83 | |
andre.schmid | c5d39ed | 2020-02-17 10:11:54 +0000 | [diff] [blame^] | 84 | server.use(middlewares); |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | return serverConfig; |
ys9693 | 16a9fce | 2020-01-19 13:50:02 +0200 | [diff] [blame] | 89 | }; |