Permalink
Browse files

Merge branch 'master' of github.com:mhansen/antinode

  • Loading branch information...
mhansen committed Jul 26, 2010
2 parents b4b1177 + 3fd90e3 commit 03435ac49ded26f4e143286ef242da6fafaa6656
Showing with 35 additions and 15 deletions.
  1. +33 −13 lib/antinode.js
  2. +1 −1 tests/common.js
  3. BIN tests/fixtures/default-host/oranga_thumb.jpg
  4. +1 −1 tests/test-200.js
View
@@ -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'];
- if (script !== undefined) {
+ var script = settings.hosts[host]['handler'];
+ if (script !== undefined && typeof script === 'string') {
/* 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();
}
}
}
@@ -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);
@@ -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();
});
View
@@ -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" : {
Binary file not shown.
View
@@ -1,6 +1,6 @@
require('./common');
-var files = ["smalltext", "dictionary", "cat.jpg"];
+var files = ["smalltext", "dictionary", "cat.jpg", "oranga_thumb.jpg"];
files.forEach(function (file) {
var disc_filename = path.join(settings.default_host.root, file);

0 comments on commit 03435ac

Please sign in to comment.