Skip to content

Commit

Permalink
Added more complete event hooks to support dynamic content/frameworks
Browse files Browse the repository at this point in the history
  • Loading branch information
Davis committed Jul 14, 2010
1 parent 1c77ade commit fa2f415
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
44 changes: 32 additions & 12 deletions lib/antinode.js
Expand Up @@ -25,13 +25,23 @@ var server;

/* require() all the scripts at the start, so there's no
* waiting for in the middle of a request */
function load_hostspecific_scripts() {
function load_hostspecific_handlers() {
for (var host in settings.hosts) {
var script = settings.hosts[host]['script'];
var script = settings.hosts[host]['handler'];
if (script !== undefined) {
/* remove filename extension */
var require_name = script.match(/(.*)\..*/)[1];
settings.hosts[host].handle = require(require_name).handle;
settings.hosts[host].handler = require(require_name);
}
}
}

/* call the module_init method if exists on each registered module.
allows modules to hook in to the server startup event. */
function handlers_init() {
for(var host in settings.hosts) {
if(settings.hosts[host].handler && settings.hosts[host].handler.handler_init) {
settings.hosts[host].handler.handler_init();
}
}
}
Expand All @@ -40,7 +50,7 @@ exports.start = function(custom_settings, callback) {
settings = custom_settings || {};
settings.__proto__ = exports.default_settings;

load_hostspecific_scripts();
load_hostspecific_handlers();

log.level = settings.log_level;
log.info( "Starting server on port", settings.port);
Expand All @@ -65,18 +75,28 @@ exports.start = function(custom_settings, callback) {
}
}
var vhost = select_vhost(req.headers.host);
if (vhost['handle'] !== undefined) {
vhost.handle(req,resp);
if (vhost.handler && vhost.handler.handle) {
var action = vhost.handler.handle(req,resp);
if(action && typeof action === 'function') {
if(vhost.handler.handler_environment){
action.apply(vhost.handler.handler_environment(req, resp))
}
else {
action(req, resp);
}
return;
}
}
var path = pathlib.join(vhost.root, clean_pathname);
if (path.match(/\.sjs$/)) {
execute_sjs(path, req, resp);
} else {
var path = pathlib.join(vhost.root, clean_pathname);
if (path.match(/\.sjs$/)) {
execute_sjs(path, req, resp);
} else {
serve_static_file(path, req, resp);
}
serve_static_file(path, req, resp);
}

});
server.listen(settings.port);
handlers_init();
server.addListener('listening', function() {
if (callback) callback();
});
Expand Down
2 changes: 1 addition & 1 deletion tests/common.js
Expand Up @@ -18,7 +18,7 @@ exports.settings = {
"root" : path.join(fixturesDir,"examplevirtualhost.com")
},
"scripthost.com" : {
"script" : path.join(fixturesDir,"scripthost.js")
"handler" : path.join(fixturesDir,"scripthost.js")
}
},
"default_host" : {
Expand Down

0 comments on commit fa2f415

Please sign in to comment.