mirror of
https://github.com/openstf/stf
synced 2025-10-04 18:29:17 +02:00
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.
This commit is contained in:
parent
cd1f9108ce
commit
6d88a28a2c
3 changed files with 140 additions and 54 deletions
105
lib/cli.js
105
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>'
|
||||
, 'port (or $PORT)'
|
||||
, Number
|
||||
, process.env.PORT || 7100)
|
||||
.option('-s, --secret <secret>'
|
||||
, 'secret (or $SECRET)'
|
||||
, String
|
||||
, process.env.SECRET)
|
||||
.option('-i, --ssid <ssid>'
|
||||
, 'session SSID (or $SSID)'
|
||||
, String
|
||||
, process.env.SSID || 'ssid')
|
||||
.option('-u, --app-url <url>'
|
||||
, 'URL to app'
|
||||
, String)
|
||||
.option('-a, --auth-url <url>'
|
||||
, 'URL to auth client'
|
||||
, String)
|
||||
|
@ -437,11 +432,9 @@ program
|
|||
.option('--storage-plugin-apk-url <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>'
|
||||
, 'port (or $PORT)'
|
||||
, Number
|
||||
, process.env.PORT || 7105)
|
||||
.option('-s, --secret <secret>'
|
||||
, 'secret (or $SECRET)'
|
||||
, String
|
||||
, process.env.SECRET)
|
||||
.option('-i, --ssid <ssid>'
|
||||
, 'session SSID (or $SSID)'
|
||||
, String
|
||||
, process.env.SSID || 'ssid')
|
||||
.option('-a, --auth-url <url>'
|
||||
, 'URL to auth client'
|
||||
, String)
|
||||
.option('-w, --websocket-url <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 <port>'
|
||||
, 'poorxy port'
|
||||
, Number
|
||||
, 7100)
|
||||
.option('--app-port <port>'
|
||||
, 'app port'
|
||||
, Number
|
||||
, 7100)
|
||||
, 7105)
|
||||
.option('--websocket-port <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() {
|
||||
|
|
|
@ -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())
|
||||
|
|
62
lib/units/poorxy/index.js
Normal file
62
lib/units/poorxy/index.js
Normal file
|
@ -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)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue