blob: 210560a1360abeb5fa388a12867b514b0de36a88 [file] [log] [blame]
Matteo Scandolocf060772016-05-13 15:54:44 -07001'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 */
15var fs = require('fs');
16var httpProxy = require('http-proxy');
17
18var proxy = httpProxy.createProxyServer({
19 target: 'http://localhost:8182',
20});
21
Matteo Scandoloc9306e42016-08-04 16:24:30 -070022if (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 Scandolocf060772016-05-13 15:54:44 -070033var defaultViews = fs.readdirSync('./app/view/');
Matteo Scandolofa0548b2016-05-20 09:49:36 -070034var viewNameMatcher = new RegExp(/\/onos\/ui\/app\/view\/(.+)\/.+\.(?:js|css|html)/);
Matteo Scandolocf060772016-05-13 15:54:44 -070035
Matteo Scandoloc9306e42016-08-04 16:24:30 -070036var 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 Scandolocf060772016-05-13 15:54:44 -070048proxy.on('upgrade', function (req, socket, head) {
49 console.log('[WS]: ', head);
50 proxy.ws(req, socket, head);
51});
52
Matteo Scandoloc9306e42016-08-04 16:24:30 -070053proxy.on('error', function (error, req, res) {
Matteo Scandolocf060772016-05-13 15:54:44 -070054 res.writeHead(500, {
55 'Content-Type': 'text/plain'
56 });
57 console.error('[Proxy]', error);
58});
59
60module.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 Scandoloc9306e42016-08-04 16:24:30 -070076 middleware: function (req, res, next) {
Matteo Scandolocf060772016-05-13 15:54:44 -070077
78 var viewName = viewNameMatcher.exec(req.url);
Matteo Scandoloc9306e42016-08-04 16:24:30 -070079 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 Scandolofa0548b2016-05-20 09:49:36 -070083 // so we redirect the request to the app folder in case of .js, .css, .html
Matteo Scandolocf060772016-05-13 15:54:44 -070084 req.url = req.url.replace('/onos/ui/', '/apps/' + viewName[1] + '/app/src/main/resources/');
85 proxy.web(req, res);
86 }
Matteo Scandoloc9306e42016-08-04 16:24:30 -070087 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 Scandolofa0548b2016-05-20 09:49:36 -070093 // NOTE onos.js and index.html should not be proxied (require server side injection)
Matteo Scandoloc9306e42016-08-04 16:24:30 -070094 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 Scandolocf060772016-05-13 15:54:44 -070096 // redirect onos base js files to the source folder
97 req.url = req.url.replace('/onos/ui/', '/web/gui/src/main/webapp/');
Matteo Scandoloc9306e42016-08-04 16:24:30 -070098 proxy.web(req, res);
Matteo Scandolocf060772016-05-13 15:54:44 -070099 }
Matteo Scandolofa0548b2016-05-20 09:49:36 -0700100 else {
Matteo Scandolocf060772016-05-13 15:54:44 -0700101 return next();
102 }
103 }
104 },
105 'port': 3000,
106 'open': false
107};