Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

allow vhoster to run as module or stand-alone and with connect 2.x

  • Loading branch information...
commit 87831b1fdb246f9e945b5a37bd4b294b736fccc6 1 parent c9d7c1d
@coolaj86 authored
View
4 package.json
@@ -2,7 +2,7 @@
"author": "AJ ONeal <coolaj86@gmail.com> (http://coolaj86.info)",
"name": "connect-vhoster",
"description": "Throw your Node.JS apps in /var/webapps/vhosts and let 'er rip!",
- "version": "1.1.1",
+ "version": "1.2.0",
"repository": {
"type": "git",
"url": "git://github.com/coolaj86/connect-vhoster.git"
@@ -11,7 +11,7 @@
"node": ">= 0.4.0"
},
"dependencies": {
- "connect": "1.x"
+ "connect": "2.x"
, "nowww": "1.x"
, "github-hook": "1.x"
, "mime": "1.x"
View
17 run-server.js
@@ -1,17 +0,0 @@
-(function () {
- "use strict";
-
- var config = require('./config')
- , server = require('./server')
- ;
-
- function logPort() {
- console.log("Serving on " + server.address().address + ":" + server.address().port);
- }
-
- if (config.port) {
- server.listen(config.port, logPort);
- } else {
- server.listen(logPort);
- }
-}());
View
1  run-server.js
View
187 server.js
@@ -7,102 +7,153 @@
, nowww = require('nowww')
, fs = require('fs')
, path = require('path')
- , apps = []
- , servers = []
- , dirs = fs.readdirSync(__dirname + "/vhosts")
;
- function sortByHostnameLength(a, b) {
- return b.hostname.length - a.hostname.length;
- }
-
- function eachHost(hostname) {
- var serverPath = __dirname + '/vhosts/' + hostname
- , aliases = serverPath + '/aliases.js'
- , server = serverPath + '/server.js'
- , app = serverPath + '/app.js'
- , hostnames
- , stats
+ function create(dirname) {
+ var dirs
+ // ignored when this module isn't main
+ , apps = []
+ , servers = []
+ , server
+ , app
;
- function eachHostname(hostname) {
- apps.push({
- serverPath: serverPath
- , hostname: hostname
- , server: server
- });
+ dirs = fs.readdirSync(dirname);
+
+ if (!config.defaultDomain) {
+ config.defaultDomain = '.local';
+ }
+ if (!/^\./.exec(config.defaultDomain)) {
+ config.defaultDomain = '.' + config.defaultDomain;
}
- if (/^\./.exec(hostname)) {
- console.log('ignoring', hostname);
- return;
+ function sortByHostnameLength(a, b) {
+ return b.hostname.length - a.hostname.length;
}
- try {
- if (!fs.statSync(serverPath).isDirectory()) {
- throw new Error('IGNORED not a directory');
+ function eachHost(hostname) {
+ var serverPath = path.join(dirname, hostname)
+ , aliases = serverPath + '/aliases.js'
+ , server = serverPath + '/server.js'
+ , app = serverPath + '/app.js'
+ , hostnames
+ , stats
+ ;
+
+ function eachHostname(hostname) {
+ var vhost = {
+ serverPath: serverPath
+ , hostname: hostname
+ , server: server
+ };
+
+ apps.push(vhost);
+ }
+
+ if (/^\./.exec(hostname)) {
+ console.warn('ignoring', hostname);
+ return;
+ }
+
+ if (!/\./.exec(hostname)) {
+ hostname += (config.defaultDomain)
+ }
+
+ try {
+ if (!fs.statSync(serverPath).isDirectory()) {
+ throw new Error('IGNORED not a directory');
+ }
+ } catch(e) {
+ server += '.js';
+ return;
+ }
+
+ try {
+ stats = fs.statSync(server);
+ } catch(e) {
+ server = undefined;
+ try {
+ stats = fs.statSync(app);
+ } catch(e) {
+ app = undefined;
+ }
+ }
+
+ server = server || app;
+
+ if (!server) {
+ console.warn('[WARN] "' + serverPath + '" doesn\'t have a working server, but maybe that\'s okay.');
+ return;
}
- } catch(e) {
- server += '.js';
- return;
- }
- try {
- stats = fs.statSync(server);
- } catch(e) {
- server = undefined;
try {
- stats = fs.statSync(app);
+ hostnames = require(aliases);
} catch(e) {
- app = undefined;
+ hostnames = [];
}
+ hostnames.push(hostname)
+
+ hostnames.forEach(eachHostname);
}
- server = server || app;
- if (!server) {
- console.warn('[WARN] "' + serverPath + '" doesn\'t have a working server, but maybe that\'s okay.');
- return;
+ if (config.githookAuth || config.githubAuth) {
+ console.info('loaded with githookAuth');
+ servers.push(githubHook(config.githookAuth || config.githubAuth, dirname + '/githook.sh'));
}
- try {
- hostnames = require(aliases);
- } catch(e) {
- hostnames = [];
+ if (!config.yeswww) {
+ servers.push(nowww());
}
- hostnames.push(hostname)
- hostnames.forEach(eachHostname);
- }
+ dirs.forEach(eachHost);
+ apps.sort(sortByHostnameLength);
+ apps.forEach(function (app) {
+ var server;
+ try {
+ server = require(app.server);
+ } catch(e) {
+ console.error('ERROR: [', app.hostname, '] failed to load [', '.' + app.server.substr(dirname.length), ']', e);
+ return;
+ }
- if (config.githookAuth || config.githubAuth) {
- console.log('loaded with githookAuth');
- servers.push(githubHook(config.githookAuth || config.githubAuth, __dirname + '/githook.sh'));
- }
+ servers.push(connect.vhost('*.' + app.hostname, server));
+ console.info('Loading', '*.' + app.hostname);
+ servers.push(connect.vhost(app.hostname, server));
+ console.info('Loading', app.hostname);
+ });
+
+ app = connect();
+ servers.forEach(function (server) {
+ app.use(server);
+ });
- if (!config.yeswww) {
- servers.push(nowww());
+ return app;
}
- dirs.forEach(eachHost);
+ module.exports.create = create;
- apps.sort(sortByHostnameLength);
- apps.forEach(function (app) {
- var server;
+ function run() {
+ var app = create(__dirname + '/vhosts')
+ , port = process.argv[2] || config.port || 4080
+ , server
+ ;
- try {
- server = require(app.server);
- } catch(e) {
- console.error('ERROR: [', app.hostname, '] failed to load [', '.' + app.server.substr(__dirname.length), ']', e);
- return;
- }
+ function listening() {
+ console.info('Listening on ' + server.address().address + ':' + server.address().port);
+ }
- console.log('Loaded', app.hostname);
- servers.push(connect.vhost('*.' + app.hostname, server));
- servers.push(connect.vhost(app.hostname, server));
- });
+ if (port) {
+ server = app.listen(port, listening);
+ } else {
+ server = app.listen(listening);
+ }
+ }
- module.exports = connect.createServer.apply(connect, servers);
+ if (require.main === module) {
+ console.info("\n=== connect-vhoster running in stand-alone mode ===\n");
+ run();
+ }
}());
View
4 webapps.conf
@@ -1,5 +1,5 @@
description "vhosts via NodeJS, Spark, and Connect"
-version "1.1"
+version "2.0"
author "AJ ONeal"
# Upstart has nothing in $PATH by default
@@ -14,5 +14,5 @@ script
DATE=`date '+%F_%H-%M-%S'`
cd /var/webapps
mkdir -p logs
- exec node run-server > "./logs/access.${DATE}.log" 2> "./logs/error.${DATE}.log"
+ exec node server > "./logs/access.${DATE}.log" 2> "./logs/error.${DATE}.log"
end script
Please sign in to comment.
Something went wrong with that request. Please try again.