AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | const proxy = require('http-proxy-middleware'); |
| 4 | |
| 5 | let localDevConfig = {}; |
| 6 | try { |
| 7 | localDevConfig = require('./devConfig'); |
| 8 | } catch (e) {} |
| 9 | const devConfig = Object.assign({}, require('./devConfig.defaults'), localDevConfig); |
| 10 | let devPort = process.env.PORT || devConfig.port; |
| 11 | |
| 12 | let jsonConfig = { |
| 13 | "appContextPath" : "/onboarding" |
| 14 | }; |
| 15 | |
| 16 | try { |
| 17 | jsonConfig = require('./src/sdc-app/config/config.json'); |
| 18 | } catch (e) { |
| 19 | console.log('could not load config. using deault value instead'); |
| 20 | } |
| 21 | |
| 22 | module.exports = function (server) { |
| 23 | let proxyConfigDefaults = { |
| 24 | changeOrigin: true, |
| 25 | secure: false, |
| 26 | onProxyRes: (proxyRes, req, res) => { |
| 27 | let setCookie = proxyRes.headers['set-cookie']; |
| 28 | if (setCookie) { |
| 29 | setCookie[0] = setCookie[0].replace(/\bSecure\b(; )?/, ''); |
| 30 | } |
| 31 | if (proxyRes.statusCode === 302 && proxyRes.headers.location.indexOf('login') > -1) { |
| 32 | proxyRes.headers.location = `http://localhost:${devPort}/sdc1#/onboardVendor`; |
| 33 | res.setHeader('Set-Cookie', [ |
| 34 | 'HTTP_CSP_EMAIL=csantana@sdc.com', |
| 35 | 'HTTP_CSP_FIRSTNAME=Carlos', |
| 36 | 'HTTP_CSP_LASTNAME=Santana', |
| 37 | 'HTTP_CSP_WSTYPE=Intranet', |
| 38 | 'HTTP_IV_REMOTE_ADDRESS=0.0.0.0', |
| 39 | 'HTTP_IV_USER=cs0008', |
| 40 | 'USER_ID=cs0008' |
| 41 | ]); |
| 42 | } |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | let middlewares = [ |
| 47 | (req, res, next) => { |
| 48 | if (req.url.indexOf('/proxy-designer1') > -1) { |
| 49 | req.url = req.url.replace('/proxy-designer1', ''); |
| 50 | } |
| 51 | |
| 52 | if (req.url.indexOf(jsonConfig.appContextPath + '/resources') > -1) { |
| 53 | req.url = req.url.replace(jsonConfig.appContextPath, ''); |
| 54 | } |
| 55 | |
| 56 | let match = req.url.match(/^(.*)_en.js$/); |
| 57 | let newUrl = match && match[1] + '.js'; |
| 58 | if (newUrl) { |
| 59 | console.log(`REWRITING URL: ${req.url} -> ${newUrl}`); |
| 60 | req.url = newUrl; |
| 61 | } |
| 62 | next(); |
| 63 | } |
| 64 | ]; |
| 65 | |
| 66 | // standalon back-end (proxyTarget) has higher priority, so it should be first |
| 67 | if (devConfig.proxyTarget) { |
| 68 | middlewares.push( |
| 69 | proxy(['/api', '/onboarding-api', '/sdc1/feProxy/onboarding-api'], Object.assign({}, proxyConfigDefaults, { |
| 70 | target: devConfig.proxyTarget, |
| 71 | pathRewrite: { |
| 72 | '/sdc1/feProxy/onboarding-api': '/onboarding-api' |
| 73 | } |
| 74 | })) |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | // ATT environment (proxyATTTarget) has lower priority, so it should be second |
| 79 | if (devConfig.proxyATTTarget) { |
| 80 | middlewares.push( |
| 81 | proxy(['/sdc1', '/onboarding-api', '/scripts', '/styles'], Object.assign({}, proxyConfigDefaults, { |
| 82 | target: devConfig.proxyATTTarget, |
| 83 | pathRewrite: { |
| 84 | // Workaround for some weird proxy issue |
| 85 | '/sdc1/feProxy/onboarding-api': '/sdc1/feProxy/onboarding-api', |
| 86 | '/onboarding-api': '/sdc1/feProxy/onboarding-api' |
| 87 | } |
| 88 | })) |
| 89 | ); |
| 90 | } |
| 91 | server.use(middlewares); |
| 92 | }; |