Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
feat: add bin for running proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Coe committed Jul 7, 2016
1 parent 5870a75 commit 5aa60ca
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
33 changes: 33 additions & 0 deletions bin/couch-url-rewrite-proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env node
const proxy = require('../server')

require('yargs')
.command(
'start',
'start the registry URL rewrite proxy',
function (yargs) {
return yargs
.option('port', {
alias: 'p',
default: 8080,
describe: 'what port should the proxy run on'
})
.option('upstream', {
alias: 'u',
default: 'http://127.0.0.1:9999',
describe: 'what upstream should the proxy connect to'
})
.option('front-door-host', {
alias: 'f',
default: process.env.FRONT_DOOR_HOST || 'https://registry.example.com',
describe: 'what external URL should tarball URLs be rewritten to'
})
},
function (argv) {
proxy(argv)
}
)
.help()
.alias('help', 'h')
.demand(1, 'please provide a command to run')
.argv
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "a proxy that rewrites URLs on their way out of couch, making registry migrations easier",
"main": "server.js",
"bin": "bin/couch-url-rewrite-proxy.js",
"scripts": {
"coverage": "nyc report --reporter=text-lcov | coveralls",
"pretest": "standard",
Expand Down Expand Up @@ -37,6 +38,7 @@
"dependencies": {
"@npm/npm-urls": "^1.0.1",
"express": "^4.14.0",
"request": "^2.72.0"
"request": "^2.72.0",
"yargs": "^4.7.1"
}
}
9 changes: 7 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ function CouchUrlRewriteProxy (opts) {
function proxy (req, res, next) {
var payload = {
method: req.method,
url: url.resolve(opts.frontDoorHost, req.path),
url: url.resolve(opts.upstream, req.path),
headers: req.headers,
qs: req.query
qs: req.query,
gzip: true,
strictSSL: false
}
req.headers.host = 'registry.npmjs.org'

Expand Down Expand Up @@ -58,7 +60,10 @@ function rewriteUrls (res, status, body, frontDoorHost) {
}

module.exports = function (opts, cb) {
cb = cb || function () {}
CouchUrlRewriteProxy(opts)
console.info('routing', opts.port, 'to', opts.upstream)
console.info('rewriting to FRONT_DOOR_HOST =', opts.frontDoorHost)
var server = app.listen(opts.port, function () {
console.info('listening on ', opts.port)
return cb(undefined, server)
Expand Down
1 change: 1 addition & 0 deletions test/couch-url-rewrite-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('couch-url-rewrite-proxy', function () {
before(function (done) {
proxy({
port: 9999,
upstream: 'http://www.example.com',
frontDoorHost: 'http://www.example.com'
}, function (err, _server) {
if (err) return done(err)
Expand Down

0 comments on commit 5aa60ca

Please sign in to comment.