Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

more inline comments and jsdocs

  • Loading branch information...
commit 7a75b4585c3cc6c84884dde0f0892a7cb4653f13 1 parent 03bec07
@pwlin authored
Showing with 54 additions and 12 deletions.
  1. +54 −12 lib/ananas/index.js
View
66 lib/ananas/index.js
@@ -10,35 +10,34 @@ var http = require('http'),
utils = require('./utils'),
mime = require('./mime');
-
/**
* Holds the path to current config file
- * @type {string}
+ * @type {String}
*/
var configuration_file = '';
/**
* Holds the unparsed config data
- * @type {string}
+ * @type {String}
*/
var configuration_data = '';
/**
* Hold a list of current virtual hosts
- * @type {object}
+ * @type {Object}
*/
var vhosts = {};
/**
* Ananas main library
- * @type {object}
+ * @type {Object}
*/
var ananas = {
/**
* Starting point
- * @param {string} config_file the path to a valid configuration file
- * @returns void
+ * @param {String} config_file the path to a valid configuration file
+ * @returns {Boolean}
*/
init : function(config_file) {
// cache this locally
@@ -48,7 +47,7 @@ var ananas = {
// if there is any error, return a message and stop processing further
if(err) {
console.error("\nAnanas Error:\n", "Sorry, the configuration file you passed to Ananas (" + config_file + ") does not exists.\n",
- "Please check the file name and try again.\n", "Exiting...\n"
+ "Please check the file name/path and try again.\n", "Exiting...\n"
);
process.exit();
return false;
@@ -66,7 +65,7 @@ var ananas = {
/**
* Start the server
- * @returns void
+ * @returns {Void}
*/
create_server : function() {
// cache this locally
@@ -88,75 +87,118 @@ var ananas = {
/**
* Callback function for accepting connections
- * @param {object} request
- * @param {object} response
+ * @param {Object} request
+ * @param {Object} response
* @returns {Boolean}
*/
on_connection : function(request, response) {
- // define the requested host name.
+ // define the requested host name
var requested_host_name = request.headers.host.match(/\:/) ? request.headers.host.match(/(.*)\:(.*)/)[1].toLowerCase() : request.headers.host.toLowerCase();
+ // get the configuration for the requested host name
var config = this.find_vhost_config(requested_host_name);
+ // add the uri to the configuration object for the requested host name
config['uri'] = utils.url_decode(url.parse(request.url).pathname);
+ // check if this is a dynamic uri, otherwise fallback to handle staic files
return this.handle_dynamic(request, response, config) || this.handle_static(request, response, config);
},
+ /**
+ * Find the configuration for the requested host name
+ * @param {String} requested_host_name get the configuration for this host name
+ * @param {Boolean} fresh may get the fresh configuration instead of the memory cache
+ * @returns {Object}
+ */
find_vhost_config : function(requested_host_name, fresh) {
+ // if fresh is not defined, set it to false
fresh = fresh || false;
+ // if vhosts[requested_host_name] is defined and we don't want a fresh configuration, just return the results
if (vhosts[requested_host_name] && fresh === false) {
return vhosts[requested_host_name];
}
+ // temporary variable to check if the requested host name has been found
var config_vhost_found = false;
+ // define the configuration object
var config = {};
+ // if we don't want a fresh configuration, parse the configuration data
if (fresh === false) {
config = JSON.parse(utils.remove_comments(configuration_data));
}
+ // if we want a fresh configuration, read the configuration file and parse it
else {
config = fs.readFileSync(configuration_file, 'binary');
config = JSON.parse(utils.remove_comments(config));
}
+ // define the configuration object for the requested host name
var config_vhost = {};
+ // loop through all configurations
for(var vhost in config['vhosts']) {
if(config['vhosts'].hasOwnProperty(vhost)) {
+ // if the current vhost is the same as the requested host name
if(vhost.toLowerCase() == requested_host_name) {
+ // if the vhost has a 'mirror' key, we need to get the configuration for the original vhost
+ //and merge it with the configuration of the current vhost
if(config['vhosts'][vhost]['mirror']) {
config_vhost = utils.merge_objects(config['vhosts'][config['vhosts'][vhost]['mirror']], config['vhosts'][vhost]);
+ // delete the 'mirror' key, as we don't need it anymore
delete config_vhost['mirror'];
}
+ // if there is no 'mirror' key, this the vhost we were looking for
else {
config_vhost = config['vhosts'][vhost];
}
+ // vhost configurations were found, so we set the temporary varialbe to true
config_vhost_found = true;
+ // break from the loop, as we don't need to loop further
break;
}
}
}
+ // if after looping through all the vhosts, we still have no configuration, set it to the 'default' vhost
if (config_vhost_found === false) {
config_vhost = config['vhosts']['default'];
}
+ // add some useful stuff to the configuration object for the requested host name
config_vhost['app_status'] = config['app_status'];
config_vhost['vhost_name'] = requested_host_name;
config_vhost['server_name'] = config['server_name'];
config_vhost['bind_port'] = config['bind_port'];
vhosts[requested_host_name] = config_vhost;
+ // return the configuration object for the requested host name
return config_vhost;
},
+ /**
+ * Handle a dynamic uri
+ * @param {Object} request
+ * @param {Object} response
+ * @param {Object} config the configuration for the requested host name
+ * @returns {Boolean} || {Void}
+ */
handle_dynamic : function(request, response, config) {
+ // if there are no routes defined, stop processing the configuration and return false
if(!config['routes']) {
return false;
}
+ // default HTTP status code
var status_code = 200;
+ // default HTTP content type
var content_type = 'text/plain';
+
var data = {} || '';
var content = '';
var self = this;
+ // a temporary variable to see if route is found, default to false
var route_found = false;
+ // if app_status = dev, we reload the config on every request so there is no need to restart
+ // the script after every change to config file
if(config['app_status'].toLowerCase() == 'dev') {
config = this.find_vhost_config(config['vhost_name'], true);
config['uri'] = utils.url_decode(url.parse(request.url).pathname);
}
+ // loop through the routes
for(var route in config['routes']) {
if(config['routes'].hasOwnProperty(route)) {
+ // if route == uri
if((route.replace(/\/$/,'')).toLowerCase() == (config['uri'].replace(/\/$/,'')).toLowerCase()) {
try {
var my_route_lib = require(config['routes'][route]['map_to']);
Please sign in to comment.
Something went wrong with that request. Please try again.