Skip to content

Instantly share code, notes, and snippets.

@quickredfox
Created March 31, 2011 01:55
Show Gist options
  • Save quickredfox/895685 to your computer and use it in GitHub Desktop.
Save quickredfox/895685 to your computer and use it in GitHub Desktop.
Runs a bunch of connect apps according to some private specs
var fs = require('fs');
var connect = require('connect');
var VHOSTS_DIR = '/srv/connect/apps';
var VHOSTS = {};
var hostsDirectory = function( req, res) {
res.writeHead( 200, {"Content-Type":"text/html"} );
names = [];
for( var vhost in VHOSTS ){
names.push("<a href=\"http://"+vhost+"\">"+vhost+"</a>");
};
res.end( "Domains:<br />"+names.join("<br/>") );
};
function readhosts() {
/*
NB: not async, use only out of the listening loop, at boot.
*/
var vhosts = fs.readdirSync(VHOSTS_DIR);
for( var i =0,vhost; vhost = vhosts[i++];){
var path = VHOSTS_DIR+'/'+vhost;
var stats = fs.statSync( path );
/*
highly <cough> likely that a domain name MUST have a dot in in with a character in front of the dot and after...
and our convetions make it that this is always true.
*/
var isVhost = /\w.\w/.test(vhost);
if( stats.isDirectory() && isVhost ){
var indexpath = path+'/index.js'
var module;
try{
module = require( indexpath )
if( module && module.app ) module = module.app
}catch( E ){
// console.log( E )
module = null
};
if( !module ){
return false
}else{
VHOSTS[vhost] = module
};
};
};
return true;
};
if(readhosts()){
var server = connect.createServer()
server.use( connect.logger() );
server.use( connect.static( '/srv/connect/static', { maxAge: 1000*60*60*24 } ) );
server.use( connect.favicon( '/srv/connect/static/favicon/ico' ) );
for(var vhost in VHOSTS){
console.log("using vhost: "+vhost)
server.use( connect.vhost( vhost, VHOSTS[vhost] ) );
}
server.use( hostsDirectory )
server.listen(8080);
console.log("Listening on port 8080");
}else{
console.log('could not find hosts')
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment