Committed by
Gerrit Code Review
WEB GUI: Live-reloading development environment
Change-Id: Ibe4a80905e7c140ff70f359d43a49e6430cc6838
Showing
8 changed files
with
167 additions
and
3 deletions
web/gui/src/main/webapp/README.md
0 → 100644
1 | +# ONOS UI | ||
2 | + | ||
3 | +## Development environment | ||
4 | + | ||
5 | +To help with UI development we provide a dedicated environment that introduce an auto reload feature and allow you to change your javascript files without recompiling the application. | ||
6 | + | ||
7 | +To get started: | ||
8 | +- Be sure to have `Node Js` installed | ||
9 | +- Enter `web/gui/src/main/webapp/` folder | ||
10 | +- Run `npm install` to install required dependency | ||
11 | +- Run `npm start` to open start the development environment | ||
12 | + | ||
13 | +In the console you should see something like: | ||
14 | + | ||
15 | +``` | ||
16 | +Dev server is up and listening on http://localhost: 8182 | ||
17 | +[BS] Proxying: http://localhost:8181 | ||
18 | +[BS] Access URLs: | ||
19 | + ---------------------------------- | ||
20 | + Local: http://localhost:3000 | ||
21 | + External: http://10.1.8.46:3000 | ||
22 | + ---------------------------------- | ||
23 | + UI: http://localhost:3002 | ||
24 | + UI External: http://10.1.8.46:3002 | ||
25 | + ---------------------------------- | ||
26 | +[BS] Watching files... | ||
27 | +``` | ||
28 | + | ||
29 | +To open ONOS visit the local URL (eg: `http://localhost:3000`) plus `/onos/ui` (eg: `http://localhost:3000/onos/ui`) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
... | @@ -85,7 +85,13 @@ | ... | @@ -85,7 +85,13 @@ |
85 | fs = _fs_; | 85 | fs = _fs_; |
86 | wss = _wss_; | 86 | wss = _wss_; |
87 | 87 | ||
88 | - cache = userPrefs; | 88 | + try { |
89 | + cache = angular.isDefined(userPrefs) ? userPrefs : {}; | ||
90 | + } | ||
91 | + catch(e){ | ||
92 | + // browser throws error for non-existing globals | ||
93 | + cache = {} | ||
94 | + } | ||
89 | 95 | ||
90 | wss.bindHandlers({ | 96 | wss.bindHandlers({ |
91 | updatePrefs: updatePrefs | 97 | updatePrefs: updatePrefs | ... | ... |
... | @@ -96,7 +96,6 @@ | ... | @@ -96,7 +96,6 @@ |
96 | 96 | ||
97 | function init(_api_) { | 97 | function init(_api_) { |
98 | api = _api_; | 98 | api = _api_; |
99 | - | ||
100 | // retrieve initial toggle button settings from user prefs | 99 | // retrieve initial toggle button settings from user prefs |
101 | setInitToggleState(); | 100 | setInitToggleState(); |
102 | } | 101 | } | ... | ... |
web/gui/src/main/webapp/bs-config.js
0 → 100644
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 | +}; | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
web/gui/src/main/webapp/dev_server.js
0 → 100644
1 | +'use strict'; | ||
2 | + | ||
3 | +var http = require('http'); | ||
4 | +// var httpProxy = require('http-proxy'); | ||
5 | +var connect = require('connect'); | ||
6 | +var serveStatic = require('serve-static'); | ||
7 | +var path = require('path'); | ||
8 | + | ||
9 | +var conf = { | ||
10 | + paths: { | ||
11 | + root: '../../../../../' | ||
12 | + }, | ||
13 | + port: '8182' | ||
14 | +} | ||
15 | + | ||
16 | +var httpProxyInit = function (baseDir) { | ||
17 | + | ||
18 | + var app = connect(); | ||
19 | + | ||
20 | + app.use(serveStatic(path.join(__dirname, baseDir))); | ||
21 | + | ||
22 | + var server = http.createServer(app); | ||
23 | + | ||
24 | + server.listen(conf.port, function(){ | ||
25 | + console.log('Dev server is up and listening on http://localhost:', conf.port); | ||
26 | + }); | ||
27 | +}; | ||
28 | + | ||
29 | +httpProxyInit(conf.paths.root); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
web/gui/src/main/webapp/package.json
0 → 100644
1 | +{ | ||
2 | + "name": "onos-gui", | ||
3 | + "version": "1.0.0", | ||
4 | + "description": "The GUI interface for ONOS", | ||
5 | + "main": "index.html", | ||
6 | + "directories": { | ||
7 | + "test": "tests" | ||
8 | + }, | ||
9 | + "scripts": { | ||
10 | + "bs": "browser-sync start --config bs-config.js", | ||
11 | + "dev-server": "node dev_server.js", | ||
12 | + "start": "parallelshell \"npm run dev-server\" \"npm run bs\"" | ||
13 | + }, | ||
14 | + "author": "ON.Lab", | ||
15 | + "license": "Apache 2.0", | ||
16 | + "devDependencies": { | ||
17 | + "browser-sync": "^2.12.8", | ||
18 | + "connect": "^3.4.1", | ||
19 | + "http-proxy": "^1.13.2", | ||
20 | + "parallelshell": "^2.0.0", | ||
21 | + "serve-static": "^1.10.2" | ||
22 | + } | ||
23 | +} |
-
Please register or login to post a comment