Created
April 3, 2011 15:05
-
-
Save quickredfox/900468 to your computer and use it in GitHub Desktop.
Initializes a bunch of connect apps (vhost server)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Require necessary modules | |
*/ | |
var FS,RootServer,VHOSTS,VHOSTS_DIRECTORY,connect; | |
FS = require('fs'); | |
connect = require('connect'); | |
/* | |
Declare some "constants" | |
*/ | |
VHOSTS_DIRECTORY = '/srv/connect/apps'; | |
VHOSTS = (function() { | |
var found, | |
vhosts; | |
found = {}; | |
vhosts = FS.readdirSync(VHOSTS_DIRECTORY); | |
vhosts.forEach(function(vhost, index) { | |
var indexpath, | |
isVhost, | |
module, | |
path,sdirname,sdirstats, | |
stats; | |
path = "" + VHOSTS_DIRECTORY + "/" + vhost; | |
stats = FS.statSync(VHOSTS_DIRECTORY); | |
isVhost = /\w.\w/.test(vhost); | |
if (stats.isDirectory() && isVhost) { | |
indexpath = "" + path + "/index"; | |
try { | |
found[ vhost ] = require( indexpath ); | |
if( found[ vhost ].use ){ | |
sdirname = "" + path + "/static" | |
try{ | |
sdirstats = FS.statSync(sdirname); | |
}catch(E){ | |
sdirstats = null | |
}; | |
if(sdirstats){ | |
found[ vhost ].use( connect.static( sdirname , { maxAge: 1000 * 60 * 60 * 24 } ) ); | |
console.log( "\twith static dir: "+ sdirname ); | |
} | |
}; | |
} catch(E) { | |
console.log('Module error:'+E); | |
if( typeof found[vhost] !== "undefined" ){ | |
delete found[vhost] | |
} | |
} | |
} | |
}); | |
return found; | |
})(); | |
/* | |
Initialize Root Server | |
*/ | |
ServerOptions = [ | |
connect.logger(), | |
connect.favicon( '/srv/connect/static/favicon.ico' ) | |
]; | |
Object.keys(VHOSTS).forEach(function(vhost) { | |
console.log('Using vhost: '+vhost); | |
return ServerOptions.push( connect.vhost( vhost, VHOSTS[vhost] ) ); | |
}); | |
ServerOptions[ServerOptions.length] = connect.static( '/srv/connect/static', { maxAge: 1000 * 60 * 60 * 24 } ) | |
/* | |
Main handler, displays list of domains on "/" if accessed by IP else responds with a 404 error. | |
*/ | |
ServerOptions[ServerOptions.length] = function(req, res) { | |
var names; | |
if( req.originalUrl && req.originalUrl === '/' ){ | |
res.writeHead(200, { | |
'Content-Type': 'text/html' | |
}); | |
names = Object.keys(VHOSTS).map(function(vhost) { | |
return "<li><a href=\"http://" + vhost + "\">" + vhost + "</a></li>"; | |
}); | |
return res.end("<h1>Domains Served</h1><ul>" + (names.join('')) + "</ul>", 'utf8'); | |
}else{ | |
res.writeHead(404); | |
res.end('404 NOT FOUND'); | |
} | |
} | |
/* | |
Start listening | |
*/ | |
connect.apply( this, ServerOptions ).listen( 8080 ); | |
console.log( "Listening on port 8080" ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment