blob: e7419acc2acfeb5a8ddc00147af12bfa42811150 [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
22var defaultViews = fs.readdirSync('./app/view/');
Matteo Scandolofa0548b2016-05-20 09:49:36 -070023var viewNameMatcher = new RegExp(/\/onos\/ui\/app\/view\/(.+)\/.+\.(?:js|css|html)/);
Matteo Scandolocf060772016-05-13 15:54:44 -070024
25proxy.on('upgrade', function (req, socket, head) {
26 console.log('[WS]: ', head);
27 proxy.ws(req, socket, head);
28});
29
30proxy.on('error', function(error, req, res) {
31 res.writeHead(500, {
32 'Content-Type': 'text/plain'
33 });
34 console.error('[Proxy]', error);
35});
36
37module.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);
Matteo Scandolocf060772016-05-13 15:54:44 -070057 if(!!viewName && defaultViews.indexOf(viewName[1]) === -1){
58 // in this case it is an external application that extend the view
Matteo Scandolofa0548b2016-05-20 09:49:36 -070059 // so we redirect the request to the app folder in case of .js, .css, .html
Matteo Scandolocf060772016-05-13 15:54:44 -070060 req.url = req.url.replace('/onos/ui/', '/apps/' + viewName[1] + '/app/src/main/resources/');
61 proxy.web(req, res);
62 }
Matteo Scandolofa0548b2016-05-20 09:49:36 -070063 // NOTE onos.js and index.html should not be proxied (require server side injection)
64 else if(req.url.match(/(?:js|css|html)/) && req.url !== '/onos/ui/onos.js' && req.url !== '/onos/ui/index.html' && req.url !== '/onos/ui/nav.html'){
Matteo Scandolocf060772016-05-13 15:54:44 -070065 // redirect onos base js files to the source folder
66 req.url = req.url.replace('/onos/ui/', '/web/gui/src/main/webapp/');
67 proxy.web(req, res);
68 }
Matteo Scandolofa0548b2016-05-20 09:49:36 -070069 else {
Matteo Scandolocf060772016-05-13 15:54:44 -070070 return next();
71 }
72 }
73 },
74 'port': 3000,
75 'open': false
76};