Skip to content

Commit

Permalink
Separate hostname and displayed name of servers, use punycode hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanbeevers committed Aug 7, 2017
1 parent 9264e93 commit a6a7c22
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"once": "^1.3.2",
"please-upgrade-node": "^3.0.0",
"pug": "^2.0.0-beta11",
"punycode": "^2.1.0",
"respawn": "^2.4.1",
"server-ready": "^0.3.1",
"strip-ansi": "^4.0.0",
Expand Down
30 changes: 17 additions & 13 deletions src/daemon/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const afterAll = require('after-all')
const httpProxy = require('http-proxy')
const serverReady = require('server-ready')
const log = require('./log')
const normalize = require('./normalize')
const tcpProxy = require('./tcp-proxy')
const daemonConf = require('../conf')
const getCmd = require('../get-cmd')
Expand Down Expand Up @@ -53,19 +54,23 @@ class Group extends EventEmitter {
return this._list
}

find(id) {
return this._list[id]
find(name) {
return this._list[normalize(name)]
}

add(id, conf) {
add(name, conf) {
conf.name = name
const id = normalize(name)

if (conf.target) {
log(`Add target ${id}`)
log(`Add target ${name}`)
this._list[id] = conf

this._change()
return
}

log(`Add server ${id}`)
log(`Add server ${name}`)

const HTTP_PROXY = `http://127.0.0.1:${daemonConf.port}/proxy.pac`

Expand Down Expand Up @@ -93,7 +98,8 @@ class Group extends EventEmitter {

const mon = respawn(command, {
...conf,
maxRestarts: 0
maxRestarts: 0,
name
})

this._list[id] = mon
Expand Down Expand Up @@ -141,10 +147,10 @@ class Group extends EventEmitter {
this._change()
}

remove(id, cb) {
const item = this.find(id)
remove(name, cb) {
const item = this.find(name)
if (item) {
delete this._list[id]
delete this._list[normalize(name)]
this._change()

if (item.stop) {
Expand Down Expand Up @@ -197,10 +203,8 @@ class Group extends EventEmitter {
exists(req, res, next) {
// Resolve using either hostname `app.tld`
// or id param `http://localhost:2000/app`
const tld = new RegExp(`.${daemonConf.tld}$`)
const id = req.params.id
? this.resolve(req.params.id)
: this.resolve(req.hostname.replace(tld, ''))
const tld = new RegExp(`\\.${daemonConf.tld}$`)
const id = this.resolve(req.params.id || req.hostname.replace(tld, ''))

// Find item
const item = this.find(id)
Expand Down
12 changes: 12 additions & 0 deletions src/daemon/normalize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const punycode = require('punycode') // eslint-disable-line node/no-deprecated-api

module.exports = function normalize(id) {
return id
? punycode
.encode(id)
.toLowerCase()
.replace(/[^a-z0-9-.]/g, '-')
.replace(/-+/g, '-')
.replace(/-$/, '')
: id
}
2 changes: 1 addition & 1 deletion src/front/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<a
:href="href(id)"
:title="title(id)"
:target="target">{{ id }}</a>
:target="target">{{ item.name }}</a>
<br>
<small @click="select(id)">
{{item.status}}
Expand Down
49 changes: 49 additions & 0 deletions test/daemon/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,55 @@ test('group.resolve should find the correct server or target id', t => {
t.is(group.resolve('baz.foo.app'), 'foo.app')
})

test('group.exists should 404 on missing id', t => {
const group = Group()
const conf = { target: 'http://example.com' }

group.add('app', conf)

const request = { params: { id: 'bar' }, hostname: 'localhost' }

const sendSpy = sinon.stub()
const statusSpy = sinon.stub().returns({ send: sendSpy })
const response = { status: statusSpy }

group.exists(request, response)

t.is(statusSpy.callCount, 1)
t.is(statusSpy.lastCall.args[0], 404)
t.is(sendSpy.callCount, 1)
})

test.cb('group.exists should find group by param', t => {
const group = Group()
const conf = { target: 'http://example.com' }

group.add('App Foo', conf)

const request = { params: { id: 'app-foo' }, hostname: 'localhost' }
const response = {}

group.exists(request, response, () => {
t.is(request.hotel.id, 'app-foo')
t.end()
})
})

test.cb('group.exists should find group by hostname', t => {
const group = Group()
const conf = { target: 'http://example.com' }

group.add('bar-baz-∫', conf)

const request = { params: {}, hostname: 'bar-baz-tl7d.dev' }
const response = {}

group.exists(request, response, () => {
t.is(request.hotel.id, 'bar-baz-tl7d')
t.end()
})
})

test('group.handleUpgrade with proxy', t => {
const group = Group()
const target = 'example.com'
Expand Down

0 comments on commit a6a7c22

Please sign in to comment.