Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 1 | let path = require('path'); |
| 2 | |
| 3 | const mockApis = require('./configurations/mock.json').sdcConfig; |
| 4 | const proxy = require('http-proxy-middleware'); |
| 5 | const devPort = 9000; |
| 6 | const fePort = 8181; |
| 7 | |
| 8 | module.exports = function(env) { |
| 9 | |
| 10 | // Set default 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 | next(); |
| 36 | } |
| 37 | ]; |
| 38 | |
| 39 | // Redirect all '/sdc1/feProxy/rest' to feHost |
| 40 | middlewares.push( |
| 41 | proxy(['/sdc1/feProxy/rest'],{ |
Idan Amit | b676b6d | 2018-02-01 14:01:11 +0200 | [diff] [blame^] | 42 | target: 'http://localhost:' + fePort, |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 43 | changeOrigin: true, |
| 44 | secure: false |
| 45 | })); |
| 46 | |
Idan Amit | 2c28572 | 2017-12-29 09:40:43 +0200 | [diff] [blame] | 47 | middlewares.push( |
| 48 | proxy(['/sdc1/rest'],{ |
Idan Amit | b676b6d | 2018-02-01 14:01:11 +0200 | [diff] [blame^] | 49 | target: 'http://localhost:' + fePort, |
Idan Amit | 2c28572 | 2017-12-29 09:40:43 +0200 | [diff] [blame] | 50 | changeOrigin: true, |
| 51 | secure: false |
| 52 | })); |
| 53 | |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 54 | // Redirect dcae urls to feHost |
| 55 | middlewares.push( |
| 56 | proxy(['/dcae','/sdc1/feProxy/dcae-api'],{ |
Idan Amit | b676b6d | 2018-02-01 14:01:11 +0200 | [diff] [blame^] | 57 | target: 'http://localhost:' + fePort, |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 58 | changeOrigin: true, |
| 59 | secure: false, |
| 60 | onProxyRes: (proxyRes, req, res) => { |
| 61 | let setCookie = proxyRes.headers['set-cookie']; |
| 62 | if (setCookie) { |
| 63 | setCookie[0] = setCookie[0].replace(/\bSecure\b(; )?/, ''); |
| 64 | } |
| 65 | } |
| 66 | })); |
| 67 | |
| 68 | // Redirect onboarding urls to feHost |
| 69 | middlewares.push( |
| 70 | proxy(['/onboarding','/sdc1/feProxy/onboarding-api'],{ |
Idan Amit | b676b6d | 2018-02-01 14:01:11 +0200 | [diff] [blame^] | 71 | target: 'http://localhost:' + fePort, |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 72 | changeOrigin: true, |
| 73 | secure: false, |
| 74 | onProxyRes: (proxyRes, req, res) => { |
| 75 | let setCookie = proxyRes.headers['set-cookie']; |
| 76 | if (setCookie) { |
| 77 | setCookie[0] = setCookie[0].replace(/\bSecure\b(; )?/, ''); |
| 78 | } |
| 79 | } |
| 80 | })); |
| 81 | |
| 82 | server.use(middlewares); |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | return ServerConfig; |
Idan Amit | 2c28572 | 2017-12-29 09:40:43 +0200 | [diff] [blame] | 87 | } |