Comparing version 0.3.4 to 0.3.5
'use strict'; | ||
var net = require('net'); | ||
var util = require('util'); | ||
var once = require('once'); | ||
var express = require('express'); | ||
var util = require('util'); | ||
var onPortOpen = require('../utils/on-port-open'); | ||
var conf = require('../conf'); | ||
var pkg = require('../../package.json'); | ||
@@ -12,9 +12,2 @@ module.exports = function (servers) { | ||
function index(req, res) { | ||
res.render('index', { | ||
pkg: pkg, | ||
monitors: servers.list() | ||
}); | ||
} | ||
function kill(req, res) { | ||
@@ -39,15 +32,20 @@ res.end(); | ||
// Redirect when server is reachable | ||
// Redirect when server is available | ||
var port = servers.get(id).env.PORT; | ||
var hostname = req.hostname; | ||
var timeout = conf.timeout; | ||
var start = new Date(); | ||
function forward() { | ||
// On connect, destroy client | ||
// and redirect | ||
function handleConnect() { | ||
clearInterval(intervalId); | ||
client.removeListener('error', handleError); | ||
client.destroy(); | ||
var forward = function forward(err) { | ||
if (err) { | ||
var msg = 'Can\'t connect to server on port ' + port; | ||
msg += 'server crashed or timeout of ' + timeout + 'ms exceeded. Retry or check logs.'; | ||
msg += '<pre><code>'; | ||
msg += servers.get(id).command.join(' '); | ||
msg += '\n\n'; | ||
msg += servers.get(id).tail; | ||
msg += '</code></pre>'; | ||
res.status(502).send(msg); | ||
} else { | ||
var url = 'http://' + hostname + ':' + port; | ||
@@ -57,31 +55,19 @@ util.log('Redirect to ' + url); | ||
} | ||
}; | ||
// On error, give up on timeout | ||
function handleError() { | ||
if (new Date() - start > timeout) { | ||
clearInterval(intervalId); | ||
// Make sure to send only one response | ||
forward = once(forward); | ||
var msg = 'Can\'t connect to server on port ' + port + ', ' + ('timeout of ' + timeout + 'ms exceeded. Retry or check logs.'); | ||
// If server stops, no need to wait for timeout | ||
servers.get(id).once('stop', function () { | ||
return forward(new Error('Server stopped')); | ||
}); | ||
msg += '<pre><code>'; | ||
msg += servers.get(id).command.join(' '); | ||
msg += '\n\n'; | ||
msg += servers.get(id).tail; | ||
msg += '</code></pre>'; | ||
res.status(502).send(msg); | ||
} | ||
} | ||
// Try to connect | ||
var client = net.connect({ port: port }, handleConnect).on('error', handleError); | ||
} | ||
var intervalId = setInterval(forward, 1000); | ||
forward(); | ||
// When port is open, forward | ||
onPortOpen(port, timeout, forward); | ||
} | ||
router.get('/', index).get('/:id', redirect).post('/kill', kill); | ||
router.get('/:id', redirect).post('/kill', kill); | ||
return router; | ||
}; |
{ | ||
"name": "hotel", | ||
"version": "0.3.4", | ||
"version": "0.3.5", | ||
"description": "Easily start, stop and access your servers from the browser", | ||
@@ -45,2 +45,3 @@ "main": "lib", | ||
"mkdirp": "^0.5.0", | ||
"once": "^1.3.2", | ||
"respawn-group": "^1.0.1", | ||
@@ -47,0 +48,0 @@ "socket.io": "^1.3.5", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
168624
33
816
16
+ Addedonce@^1.3.2