Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes scope working under a path #984

Merged
merged 1 commit into from
Feb 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions client/app/scripts/components/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Sidebar from './sidebar.js';
import Status from './status.js';
import Topologies from './topologies.js';
import TopologyOptions from './topology-options.js';
import { getApiDetails, getTopologies } from '../utils/web-api-utils';
import { getApiDetails, getTopologies, basePathSlash } from '../utils/web-api-utils';
import { hitEsc } from '../actions/app-actions';
import Details from './details';
import Nodes from './nodes';
Expand Down Expand Up @@ -51,7 +51,7 @@ export default class App extends React.Component {
AppStore.addListener(this.onChange);
window.addEventListener('keyup', this.onKeyPress);

getRouter(this.props.base).start({hashbang: true});
getRouter().start({hashbang: true});
if (!AppStore.isRouteSet()) {
// dont request topologies when already done via router
getTopologies(AppStore.getActiveTopologyOptions());
Expand All @@ -75,9 +75,9 @@ export default class App extends React.Component {
// width of details panel blocking a view
const detailsWidth = showingDetails ? 450 : 0;
const topMargin = 100;
const contrastMode = this.props.base.indexOf('contrast') > -1;
const contrastMode = window.location.pathname.indexOf('contrast') > -1;
// link url to switch contrast with current UI state
const otherContrastModeUrl = contrastMode ? '/' : '/contrast.html';
const otherContrastModeUrl = contrastMode ? basePathSlash(window.location.pathname) : 'contrast.html';
const otherContrastModeTitle = contrastMode ? 'Switch to normal contrast' : 'Switch to high contrast';

return (
Expand Down
2 changes: 1 addition & 1 deletion client/app/scripts/contrast-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ import ReactDOM from 'react-dom';

import App from './components/app.js';

ReactDOM.render(<App base="/contrast.html" />, document.getElementById('app'));
ReactDOM.render(<App />, document.getElementById('app'));
2 changes: 1 addition & 1 deletion client/app/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ import ReactDOM from 'react-dom';

import App from './components/app.js';

ReactDOM.render(<App base="" />, document.getElementById('app'));
ReactDOM.render(<App />, document.getElementById('app'));
5 changes: 3 additions & 2 deletions client/app/scripts/utils/router-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ page('/state/:state', function(ctx) {
route(state);
});

export function getRouter(base) {
page.base(base);
export function getRouter() {
// strip any trailing '/'s.
page.base(window.location.pathname.replace(/\/$/, ''));
return page;
}
10 changes: 10 additions & 0 deletions client/app/scripts/utils/web-api-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ export function basePath(urlPath) {
return parts.join('/').replace(/\/$/, '');
}

export function basePathSlash(urlPath) {
//
// "/scope/terminal.html" -> "/scope/"
// "/scope/" -> "/scope/"
// "/scope" -> "/scope/"
// "/" -> "/"
//
return basePath(urlPath) + '/';
}

const wsProto = location.protocol === 'https:' ? 'wss' : 'ws';
const wsUrl = wsProto + '://' + location.host + basePath(location.pathname);

Expand Down
1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"eslint-plugin-jasmine": "1.6.0",
"eslint-plugin-react": "3.8.0",
"file-loader": "0.8.4",
"http-proxy-rules": "^1.0.1",
"jest-cli": "~0.7.1",
"json-loader": "0.5.3",
"less": "~2.5.1",
Expand Down
38 changes: 38 additions & 0 deletions client/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var express = require('express');
var http = require('http');
var httpProxy = require('http-proxy');
var HttpProxyRules = require('http-proxy-rules');
var url = require('url');

var app = express();
Expand Down Expand Up @@ -90,3 +92,39 @@ var server = app.listen(port, function () {
});

server.on('upgrade', proxy.ws.bind(proxy));


/*************************************************************
*
* path proxy server
*
*************************************************************/

var proxyRules = new HttpProxyRules({
rules: {
'/scoped/': 'http://localhost:' + port
}
});

var pathProxy = httpProxy.createProxy({ws: true});
var pathProxyPort = port + 1;
const proxyPathServer = http.createServer(function(req, res) {
var target = proxyRules.match(req);
if (!target) {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('No rules matched! Check out /scoped/');
return;
}
return pathProxy.web(req, res, {target: target});
}).listen(pathProxyPort, function() {
var pathProxyHost = proxyPathServer.address().address;
console.log('Scope Proxy Path UI listening at http://%s:%s/scoped/',
pathProxyHost, pathProxyPort);
});

proxyPathServer.on('upgrade', function(req, socket, head) {
var target = proxyRules.match(req);
if (target) {
return pathProxy.ws(req, socket, head, {target: target});
}
});