From 6d88a28a2cc517af511fa28feabbf8d269598116 Mon Sep 17 00:00:00 2001 From: Simo Kinnunen Date: Thu, 23 Oct 2014 13:28:18 +0900 Subject: [PATCH] Add a poor reverse proxy for local development, the main difference being that everything except websockets now goes through the same port. Makes it easier to understand the production url layout. --- lib/cli.js | 105 ++++++++++++++++++++++++++++---------- lib/units/app/index.js | 27 ---------- lib/units/poorxy/index.js | 62 ++++++++++++++++++++++ 3 files changed, 140 insertions(+), 54 deletions(-) create mode 100644 lib/units/poorxy/index.js diff --git a/lib/cli.js b/lib/cli.js index 5a18a00a..4052bd60 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -408,20 +408,15 @@ program }) program - .command('app') - .description('start app') + .command('poorxy') + .description('start a poor reverse proxy for local development') .option('-p, --port ' , 'port (or $PORT)' , Number , process.env.PORT || 7100) - .option('-s, --secret ' - , 'secret (or $SECRET)' - , String - , process.env.SECRET) - .option('-i, --ssid ' - , 'session SSID (or $SSID)' - , String - , process.env.SSID || 'ssid') + .option('-u, --app-url ' + , 'URL to app' + , String) .option('-a, --auth-url ' , 'URL to auth client' , String) @@ -437,11 +432,9 @@ program .option('--storage-plugin-apk-url ' , 'URL to apk storage plugin' , String) - .option('-d, --disable-watch' - , 'disable watching resources') .action(function(options) { - if (!options.secret) { - this.missingArgument('--secret') + if (!options.appUrl) { + this.missingArgument('--app-url') } if (!options.authUrl) { this.missingArgument('--auth-url') @@ -459,15 +452,57 @@ program this.missingArgument('--storage-plugin-apk-url') } + require('./units/poorxy')({ + port: options.port + , appUrl: options.appUrl + , authUrl: options.authUrl + , websocketUrl: options.websocketUrl + , storageUrl: options.storageUrl + , storagePluginImageUrl: options.storagePluginImageUrl + , storagePluginApkUrl: options.storagePluginApkUrl + }) + }) + +program + .command('app') + .description('start app') + .option('-p, --port ' + , 'port (or $PORT)' + , Number + , process.env.PORT || 7105) + .option('-s, --secret ' + , 'secret (or $SECRET)' + , String + , process.env.SECRET) + .option('-i, --ssid ' + , 'session SSID (or $SSID)' + , String + , process.env.SSID || 'ssid') + .option('-a, --auth-url ' + , 'URL to auth client' + , String) + .option('-w, --websocket-url ' + , 'URL to websocket client' + , String) + .option('-d, --disable-watch' + , 'disable watching resources') + .action(function(options) { + if (!options.secret) { + this.missingArgument('--secret') + } + if (!options.authUrl) { + this.missingArgument('--auth-url') + } + if (!options.websocketUrl) { + this.missingArgument('--websocket-url') + } + require('./units/app')({ port: options.port , secret: options.secret , ssid: options.ssid , authUrl: options.authUrl , websocketUrl: options.websocketUrl - , storageUrl: options.storageUrl - , storagePluginImageUrl: options.storagePluginImageUrl - , storagePluginApkUrl: options.storagePluginApkUrl , disableWatch: options.disableWatch }) }) @@ -681,10 +716,14 @@ program , 'auth secret' , String , 'kute kittykat') + .option('--poorxy-port ' + , 'poorxy port' + , Number + , 7100) .option('--app-port ' , 'app port' , Number - , 7100) + , 7105) .option('--websocket-port ' , 'websocket port' , Number @@ -793,7 +832,7 @@ program , '--app-url', util.format( 'http://%s:%d/' , options.publicIp - , options.appPort + , options.poorxyPort ) ]) @@ -803,21 +842,15 @@ program , '--port', options.appPort , '--secret', options.authSecret , '--auth-url', util.format( - 'http://%s:%d/' + 'http://%s:%d/auth/mock/' , options.publicIp - , options.authPort + , options.poorxyPort ) , '--websocket-url', util.format( 'http://%s:%d/' , options.publicIp , options.websocketPort ) - , '--storage-url' - , util.format('http://localhost:%d/', options.storagePort) - , '--storage-plugin-image-url' - , util.format('http://localhost:%d/', options.storagePluginImagePort) - , '--storage-plugin-apk-url' - , util.format('http://localhost:%d/', options.storagePluginApkPort) ].concat((function() { var extra = [] if (options.disableWatch) { @@ -858,6 +891,24 @@ program , '--storage-url' , util.format('http://localhost:%d/', options.storagePort) ]) + + // poorxy + , procutil.fork(__filename, [ + 'poorxy' + , '--port', options.poorxyPort + , '--app-url' + , util.format('http://localhost:%d/', options.appPort) + , '--auth-url' + , util.format('http://localhost:%d/', options.authPort) + , '--websocket-url' + , util.format('http://localhost:%d/', options.websocketPort) + , '--storage-url' + , util.format('http://localhost:%d/', options.storagePort) + , '--storage-plugin-image-url' + , util.format('http://localhost:%d/', options.storagePluginImagePort) + , '--storage-plugin-apk-url' + , util.format('http://localhost:%d/', options.storagePluginApkPort) + ]) ] function shutdown() { diff --git a/lib/units/app/index.js b/lib/units/app/index.js index 3bbfc53f..4f71b0af 100644 --- a/lib/units/app/index.js +++ b/lib/units/app/index.js @@ -9,7 +9,6 @@ var serveFavicon = require('serve-favicon') var serveStatic = require('serve-static') var csrf = require('csurf') var Promise = require('bluebird') -var httpProxy = require('http-proxy') var compression = require('compression') var logger = require('../../util/logger') @@ -29,11 +28,6 @@ module.exports = function(options) { var log = logger.createLogger('app') , app = express() , server = http.createServer(app) - , proxy = httpProxy.createProxyServer() - - proxy.on('error', function(err) { - log.error('Proxy had an error', err.stack) - }) app.use('/static/docs', markdownServe.middleware({ rootDirectory: pathutil.root('node_modules/stf-docs') @@ -89,27 +83,6 @@ module.exports = function(options) { , authUrl: options.authUrl })) - // Proxied requests must come before any body parsers. These proxies are - // here mainly for convenience, they should be replaced with proper reverse - // proxies in production. - app.all('/api/v1/s/image/*', function(req, res) { - proxy.web(req, res, { - target: options.storagePluginImageUrl - }) - }) - - app.all('/api/v1/s/apk/*', function(req, res) { - proxy.web(req, res, { - target: options.storagePluginApkUrl - }) - }) - - app.all('/api/v1/s/*', function(req, res) { - proxy.web(req, res, { - target: options.storageUrl - }) - }) - app.use(bodyParser.json()) app.use(csrf()) app.use(validator()) diff --git a/lib/units/poorxy/index.js b/lib/units/poorxy/index.js new file mode 100644 index 00000000..95885116 --- /dev/null +++ b/lib/units/poorxy/index.js @@ -0,0 +1,62 @@ +var http = require('http') + +var express = require('express') +var httpProxy = require('http-proxy') + +var logger = require('../../util/logger') + +module.exports = function(options) { + var log = logger.createLogger('poorxy') + , app = express() + , server = http.createServer(app) + , proxy = httpProxy.createProxyServer() + + proxy.on('error', function(err) { + log.error('Proxy had an error', err.stack) + }) + + app.set('strict routing', true) + app.set('case sensitive routing', true) + app.set('trust proxy', true) + + ;['/api/v1/auth/*', '/static/auth/*', '/auth/*'].forEach(function(route) { + app.all(route, function(req, res) { + proxy.web(req, res, { + target: options.authUrl + }) + }) + }) + + ;['/api/v1/s/image/*'].forEach(function(route) { + app.all(route, function(req, res) { + proxy.web(req, res, { + target: options.storagePluginImageUrl + }) + }) + }) + + ;['/api/v1/s/apk/*'].forEach(function(route) { + app.all(route, function(req, res) { + proxy.web(req, res, { + target: options.storagePluginApkUrl + }) + }) + }) + + ;['/api/v1/s*'].forEach(function(route) { + app.all(route, function(req, res) { + proxy.web(req, res, { + target: options.storageUrl + }) + }) + }) + + app.use(function(req, res) { + proxy.web(req, res, { + target: options.appUrl + }) + }) + + server.listen(options.port) + log.info('Listening on port %d', options.port) +}