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 | |
Matteo Scandolo | c9306e4 | 2016-08-04 16:24:30 -0700 | [diff] [blame] | 22 | if (process.env.ONOS_EXTERNAL_APP_DIRS) { |
| 23 | var external_apps = process.env.ONOS_EXTERNAL_APP_DIRS.replace(/\s/,'').split(','); |
| 24 | external_apps = external_apps.reduce(function(dict, app){ |
| 25 | var pieces = app.split(':'); |
| 26 | var appName = pieces[0]; |
| 27 | var appPath = pieces[1]; |
| 28 | dict[appName] = appPath; |
| 29 | return dict; |
| 30 | }, {}); |
| 31 | } |
| 32 | |
Matteo Scandolo | cf06077 | 2016-05-13 15:54:44 -0700 | [diff] [blame] | 33 | var defaultViews = fs.readdirSync('./app/view/'); |
Matteo Scandolo | fa0548b | 2016-05-20 09:49:36 -0700 | [diff] [blame] | 34 | var viewNameMatcher = new RegExp(/\/onos\/ui\/app\/view\/(.+)\/.+\.(?:js|css|html)/); |
Matteo Scandolo | cf06077 | 2016-05-13 15:54:44 -0700 | [diff] [blame] | 35 | |
Matteo Scandolo | c9306e4 | 2016-08-04 16:24:30 -0700 | [diff] [blame] | 36 | var checkExternalApp = function (url) { |
| 37 | if(external_apps){ |
| 38 | for(var i = 0; i < Object.keys(external_apps).length; i++){ |
| 39 | var key = Object.keys(external_apps)[i]; |
| 40 | if(url.indexOf(key) !== -1){ |
| 41 | return key; |
| 42 | }; |
| 43 | } |
| 44 | } |
| 45 | return false; |
| 46 | }; |
| 47 | |
Matteo Scandolo | cf06077 | 2016-05-13 15:54:44 -0700 | [diff] [blame] | 48 | proxy.on('upgrade', function (req, socket, head) { |
| 49 | console.log('[WS]: ', head); |
| 50 | proxy.ws(req, socket, head); |
| 51 | }); |
| 52 | |
Matteo Scandolo | c9306e4 | 2016-08-04 16:24:30 -0700 | [diff] [blame] | 53 | proxy.on('error', function (error, req, res) { |
Matteo Scandolo | cf06077 | 2016-05-13 15:54:44 -0700 | [diff] [blame] | 54 | res.writeHead(500, { |
| 55 | 'Content-Type': 'text/plain' |
| 56 | }); |
| 57 | console.error('[Proxy]', error); |
| 58 | }); |
| 59 | |
| 60 | module.exports = { |
| 61 | 'files': [ |
| 62 | './app/**/*.css', |
| 63 | './app/**/*.js', |
| 64 | './app/**/*.json', |
| 65 | './app/**/*.html', |
| 66 | './app/**/*.jpg', |
| 67 | './app/**/*.png', |
| 68 | './app/**/*.gif', |
| 69 | '../../../../../apps/**/*.js', |
| 70 | '../../../../../apps/**/*.html', |
| 71 | '../../../../../apps/**/*.css' |
| 72 | ], |
| 73 | proxy: { |
| 74 | target: "http://localhost:8181", |
| 75 | ws: true, |
Matteo Scandolo | c9306e4 | 2016-08-04 16:24:30 -0700 | [diff] [blame] | 76 | middleware: function (req, res, next) { |
Matteo Scandolo | cf06077 | 2016-05-13 15:54:44 -0700 | [diff] [blame] | 77 | |
| 78 | var viewName = viewNameMatcher.exec(req.url); |
Matteo Scandolo | c9306e4 | 2016-08-04 16:24:30 -0700 | [diff] [blame] | 79 | var isExternalApp = checkExternalApp(req.url); |
| 80 | |
| 81 | if (!!viewName && !isExternalApp && defaultViews.indexOf(viewName[1]) === -1) { |
| 82 | // in this case it is an application that extend the view |
Matteo Scandolo | fa0548b | 2016-05-20 09:49:36 -0700 | [diff] [blame] | 83 | // so we redirect the request to the app folder in case of .js, .css, .html |
Matteo Scandolo | cf06077 | 2016-05-13 15:54:44 -0700 | [diff] [blame] | 84 | req.url = req.url.replace('/onos/ui/', '/apps/' + viewName[1] + '/app/src/main/resources/'); |
| 85 | proxy.web(req, res); |
| 86 | } |
Matteo Scandolo | c9306e4 | 2016-08-04 16:24:30 -0700 | [diff] [blame] | 87 | else if (isExternalApp) { |
| 88 | // in this case it is an external application (not in ONOS source code) that extend the view |
| 89 | // so we redirect the request to the app folder in case of .js, .css, .html |
| 90 | req.url = req.url.replace('/onos/ui/', '/src/main/resources/'); |
| 91 | proxy.web(req, res); |
| 92 | } |
Matteo Scandolo | fa0548b | 2016-05-20 09:49:36 -0700 | [diff] [blame] | 93 | // NOTE onos.js and index.html should not be proxied (require server side injection) |
Matteo Scandolo | c9306e4 | 2016-08-04 16:24:30 -0700 | [diff] [blame] | 94 | else if (req.url.match(/(?:js|css|html)/) && req.url !== '/onos/ui/onos.js' && |
| 95 | req.url !== '/onos/ui/index.html' && req.url !== '/onos/ui/nav.html') { |
Matteo Scandolo | cf06077 | 2016-05-13 15:54:44 -0700 | [diff] [blame] | 96 | // redirect onos base js files to the source folder |
| 97 | req.url = req.url.replace('/onos/ui/', '/web/gui/src/main/webapp/'); |
Matteo Scandolo | c9306e4 | 2016-08-04 16:24:30 -0700 | [diff] [blame] | 98 | proxy.web(req, res); |
Matteo Scandolo | cf06077 | 2016-05-13 15:54:44 -0700 | [diff] [blame] | 99 | } |
Matteo Scandolo | fa0548b | 2016-05-20 09:49:36 -0700 | [diff] [blame] | 100 | else { |
Matteo Scandolo | cf06077 | 2016-05-13 15:54:44 -0700 | [diff] [blame] | 101 | return next(); |
| 102 | } |
| 103 | } |
| 104 | }, |
| 105 | 'port': 3000, |
| 106 | 'open': false |
| 107 | }; |