Matteo Scandolo | cf06077 | 2016-05-13 15:54:44 -0700 | [diff] [blame] | 1 | 'use strict'; |
| 2 | /* |
| 3 | |-------------------------------------------------------------------------- |
| 4 | | Browser-sync config file |
| 5 | |-------------------------------------------------------------------------- |
| 6 | | |
| 7 | | For up-to-date information about the options: |
| 8 | | http://www.browsersync.io/docs/options/ |
| 9 | | |
| 10 | | There are more options than you see here, these are just the ones that are |
| 11 | | set internally. See the website for more info. |
| 12 | | |
| 13 | | |
| 14 | */ |
| 15 | var fs = require('fs'); |
| 16 | var httpProxy = require('http-proxy'); |
| 17 | |
| 18 | var proxy = httpProxy.createProxyServer({ |
| 19 | target: 'http://localhost:8182', |
| 20 | }); |
| 21 | |
| 22 | var defaultViews = fs.readdirSync('./app/view/'); |
| 23 | var viewNameMatcher = new RegExp(/\/onos\/ui\/app\/view\/(.+)\/.+\.js/); |
| 24 | |
| 25 | proxy.on('upgrade', function (req, socket, head) { |
| 26 | console.log('[WS]: ', head); |
| 27 | proxy.ws(req, socket, head); |
| 28 | }); |
| 29 | |
| 30 | proxy.on('error', function(error, req, res) { |
| 31 | res.writeHead(500, { |
| 32 | 'Content-Type': 'text/plain' |
| 33 | }); |
| 34 | console.error('[Proxy]', error); |
| 35 | }); |
| 36 | |
| 37 | module.exports = { |
| 38 | 'files': [ |
| 39 | './app/**/*.css', |
| 40 | './app/**/*.js', |
| 41 | './app/**/*.json', |
| 42 | './app/**/*.html', |
| 43 | './app/**/*.jpg', |
| 44 | './app/**/*.png', |
| 45 | './app/**/*.gif', |
| 46 | '../../../../../apps/**/*.js', |
| 47 | '../../../../../apps/**/*.html', |
| 48 | '../../../../../apps/**/*.css' |
| 49 | ], |
| 50 | proxy: { |
| 51 | target: "http://localhost:8181", |
| 52 | ws: true, |
| 53 | middleware: function(req, res, next){ |
| 54 | |
| 55 | |
| 56 | var viewName = viewNameMatcher.exec(req.url); |
| 57 | |
| 58 | if(!!viewName && defaultViews.indexOf(viewName[1]) === -1){ |
| 59 | // in this case it is an external application that extend the view |
| 60 | // so we redirect the request to the app folder |
| 61 | req.url = req.url.replace('/onos/ui/', '/apps/' + viewName[1] + '/app/src/main/resources/'); |
| 62 | proxy.web(req, res); |
| 63 | } |
| 64 | // NOTE onos.js should not be proxied (require server side injection) |
| 65 | else if(req.url.match(/.js$/) && req.url !== '/onos/ui/onos.js'){ |
| 66 | // redirect onos base js files to the source folder |
| 67 | req.url = req.url.replace('/onos/ui/', '/web/gui/src/main/webapp/'); |
| 68 | proxy.web(req, res); |
| 69 | } |
| 70 | else{ |
| 71 | return next(); |
| 72 | } |
| 73 | } |
| 74 | }, |
| 75 | 'port': 3000, |
| 76 | 'open': false |
| 77 | }; |