Commit 55caa6c0 authored by Anton Veshnyakov's avatar Anton Veshnyakov

worker proxy: default path

parent d7a9b363
......@@ -4,6 +4,7 @@ module.exports = {
NODE_NAME: config.get('couchbox.nodename'),
PROXY_PORT: config.get('proxy.port'),
PROXY_PATH: config.get('proxy.path'),
PROXY_DEFAULT: config.get('proxy.default'),
API_ENABLED: config.get('api.active'),
API_PORTS: config.get('api.ports'),
......
......@@ -5,7 +5,7 @@ const lib = require('../../utils/lib');
const {
PROXY_PORT, PROXY_PATH,
PROXY_PORT, PROXY_PATH, PROXY_DEFAULT,
API_ENABLED, API_PORTS,
SOCKET_ENABLED, SOCKET_PORT, SOCKET_PATH,
......@@ -15,6 +15,17 @@ const {
}
} = require('./constants');
function cleanPath(path) {
let p = (path + '').replace(/\/\/+/g, '/');
if (p[0] !== '/') {
p = '/' + p;
}
if (p.length > 1 && p[p.length - 1] !== '/') {
p = p + '/';
}
return p || '/';
}
function ProxyWorker(props = {}) {
const logger = new Logger({ prefix: 'Proxy', logger: props.logger });
const log = logger.getLog();
......@@ -28,6 +39,7 @@ function ProxyWorker(props = {}) {
let lastApiWorker = 0;
const maxApiWorker = API_PORTS.length;
const getApiWorker = () => ({
host: 'localhost',
port: API_PORTS[++lastApiWorker === maxApiWorker ? lastApiWorker = 0 : lastApiWorker]
......@@ -52,7 +64,7 @@ function ProxyWorker(props = {}) {
const proxyHTTP = httpProxy.createProxyServer({}).on('proxyReq', onProxyReq);
const proxySOCKET = new httpProxy.createProxyServer({ target: getSocketWorker() }).on('proxyReq', onProxyReq);
const proxyPath = cleanPath(PROXY_PATH);
const connections = {};
let connectionCounter = 0;
......@@ -94,19 +106,24 @@ function ProxyWorker(props = {}) {
}
if (API_ENABLED) {
if (PROXY_PATH !== '/') {
processors.push((req, res) => {
if (req.url.indexOf(PROXY_PATH) === 0) {
proxyHTTP.web(req, res, {target: getApiWorker()});
return true;
let defaultURL = null;
if (PROXY_DEFAULT && Object.isString(PROXY_DEFAULT) && PROXY_DEFAULT.length > 0) {
defaultURL = cleanPath(PROXY_DEFAULT);
if (defaultURL && defaultURL.length > 1 && defaultURL[defaultURL.length - 1] === '/') {
defaultURL = defaultURL.substring(0, defaultURL.length - 1);
}
}
processors.push((req, res) => {
if (req.url.indexOf(proxyPath) === 0) {
if (defaultURL && cleanPath(req.url) === proxyPath) {
req.url = defaultURL;
}
});
} else {
processors.push((req, res) => {
proxyHTTP.web(req, res, {target: getApiWorker()});
return true;
});
}
}
});
}
function router(req, res) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment