Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added more complete event hooks to support dynamic content/frameworks

  • Loading branch information...
commit fa2f415ba29dbc81f3dc75d8b6039f67d1009cd9 1 parent 1c77ade
Davis authored
Showing with 33 additions and 13 deletions.
  1. +32 −12 lib/antinode.js
  2. +1 −1  tests/common.js
View
44 lib/antinode.js
@@ -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();
}
}
}
@@ -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
2  tests/common.js
@@ -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" : {
Please sign in to comment.
Something went wrong with that request. Please try again.