Exemplo n.º 1
0
	return function(opts) {

		// Verify and initialize opts
		debug.assert(opts).ignore(undefined).is('object');
		if(!opts) {
			opts = {};
		}

		// Copy spec.params as options
		if(is.object(spec.params)) {
			ARRAY(Object.keys(spec.params)).forEach(function(key) {
				if(!opts.hasOwnProperty(key)) {
					opts[key] = copy(spec.params[key]);
				}
			});
		}

		// Build routes
		var tmp = {};
		if(spec.routes) {
			ARRAY(Object.keys(spec.routes)).forEach(function(key) {
				if(is.func(modules[key])) {
					tmp[key] = modules[key](opts);
				} else if(is.obj(modules[key])) {
					tmp[key] = modules[key];
				}
			});
		}
		return tmp;
	};
Exemplo n.º 2
0
/** Update cookie cache
 * @params url {object} The parsed URL object
 * @params cookies {array|string} The array of cookies or single cookie
 */
function set_cookies(url, cookies) {
	if(is.string(cookies)) {
		cookies = [cookies];
	}
	debug.assert(url).is('object');
	debug.assert(url.host).is('string');
	debug.assert(cookies).is('array');

	//debug.log('Saving cookies for host = ', url.host);

	if(!is.array(_cookies[url.host])) {
		_cookies[url.host] = cookies;
		//debug.log('Saved new cookies as: ', _cookies[url.host]);
		return;
	}

	var tmp = {};

	function save_cookie(cookie) {
		var i = cookie.indexOf('=');
		var name = cookie.substr(0, ((i >= 1) ? i : cookie.length));
		tmp[name] = cookie;
	}

	ARRAY(_cookies[url.host]).forEach(save_cookie);
	ARRAY(cookies).forEach(save_cookie);

	_cookies[url.host] = ARRAY(Object.keys(tmp)).map(function(key) { return tmp[key]; }).valueOf();

	//debug.log('Saved new cookies as: ', _cookies[url.host]);
}
Exemplo n.º 3
0
		return function () {
			var args = Array.prototype.slice.call(arguments);
			//var cols = [];
			print_info( chop_long_paths(get_prefix(prefix)) + ': ' );
			ARRAY( ARRAY(args).map(get_stack).map(inspect_and_trim).join(' ').split("\n") ).map(chop_and_convert).forEach(function(line) {
				print_info( ''+ timestamp + ' > ' + chop_long_paths(line) );
			});
		};
Exemplo n.º 4
0
	obj[method] = function() {
		var x = (debug.inspectMethod._id += 1);
		var args = Array.prototype.slice.call(arguments);
		var stack = [].concat(debug.__stack);
		debug.log('#' + x + ': Call to ' + method + ' (' + ARRAY(args).map(inspect_values).join(', ') + ') ...');
		// FIXME: files could be printed relative to previous stack item, so it would not take that much space.
		debug.log('#' + x + ": stack = ", ARRAY(stack).map(function(x) { return print_path(x.getFileName()) + ':' + x.getLineNumber(); }).join(' -> ') );
		var ret = FUNCTION(orig).apply(obj, args);
		debug.log('#' + x + ': returned: ', ret);
		return ret;
	};
Exemplo n.º 5
0
		return function () {
			try {
				if(ansi) { debug.defaults.cursors.log(stdout_cursor); }
				var args = Array.prototype.slice.call(arguments);
				_print_log( chop_long_paths(get_prefix(prefix)) + ': ');
				ARRAY( ARRAY(args).map(inspect_and_trim).join(' ').split("\n") ).map(chop_and_convert).forEach(function(line) {
					_print_log( ''+ timestamp + ' > ' + chop_long_paths(line) );
				});
			} finally {
				if(ansi) { stdout_cursor.reset(); }
			}
		};
Exemplo n.º 6
0
			ARRAY(content).forEach(function(obj) {
				var result = {};
				var paths = norUtils.getPathsFromData(obj);
				ARRAY(paths).forEach(function(path) {
					var key = path.join('.');
					var value = norUtils.getDataFromPath(obj, path);
					result[key] = value;
				});

				data.push( ARRAY(keys).map(function(key) {
					return ''+result[key];
				}).valueOf() );
			});
Exemplo n.º 7
0
	ARRAY(Object.keys(routes.$use)).forEach(function app_builder_routes_foreach(route) {
		ARRAY(['$get', '$post', '$del']).forEach(function(method) {
			if(routes[method].hasOwnProperty(route)) {
				delete routes[method][route];
			}
		});
	});
Exemplo n.º 8
0
	ARRAY(Object.keys(routes)).forEach(function app_builder_foreach_route_keys(type) {

		debug.log('Method ' + type + ' has ' + Object.keys(routes[type]).length + ' routes');

		ARRAY(Object.keys(routes[type])).forEach(function app_builder_foreach_type_keys(route) {
			var path = '/';
			if(route !== "index") {
				path += route;
			}
			if(type === '$use') {
				debug.log('Added USE route for '+ path);
				app.use(path, urlencoded_parser, json_parser, route_builder(routes.$use[route](opts)) );
				return;
			}
			if(type === '$get') {
				debug.log('Added GET route for '+ path);
				app.get(path, route_builder(routes.$get[route](opts)) );
				return;
			}
			if(type === '$post') {
				debug.log('Added POST route for '+ path);
				app.post(path, urlencoded_parser, json_parser, route_builder(routes.$post[route](opts)) );
				return;
			}
			if(type === '$del') {
				debug.log('Added DELETE route for '+ path);
				app.delete(path, urlencoded_parser, json_parser, route_builder(routes.$del[route](opts)) );
				return;
			}
		});
	});
Exemplo n.º 9
0
/** Exception type for HTTP errors */
function HTTPError() {
	var args = Array.prototype.slice.call(arguments);
	if(!(this instanceof HTTPError)) {
		var self = new HTTPError();
		return FUNCTION(self).apply(self, args);
	}

	var headers, msg, code;
	ARRAY(args).forEach(function HTTPError_foreach(arg) {
		if(typeof arg === 'object') {
			headers = arg;
		}
		if(typeof arg === 'string') {
			msg = arg;
		}
		if(typeof arg === 'number') {
			code = arg;
		}
	});

	code = code || 500;
	msg = msg || (''+code+' '+require('http').STATUS_CODES[code]);
	headers = headers || {};

	Error.call(this);
	Error.captureStackTrace(this, this);
	this.code = code;
	this.message = msg;
	this.headers = headers;
}
Exemplo n.º 10
0
/** */
function index_builder(spec, opts) {
	debug.assert(spec).is('object');
	debug.assert(spec.type).ignore(undefined).is('string');

	debug.assert(opts).ignore(undefined).is('object');

	opts = opts || {};

	spec.type = spec.type || 'route';

	spec.path = opts.path;

	if(opts) {
		if(!spec.params) {
			spec.params = {};
		}
		ARRAY(Object.keys(opts)).forEach(function(key) {
			if(!spec.params.hasOwnProperty(key)) {
				spec.params[key] = opts[key];
			}
		});
	}

	//debug.log('spec = ', JSON.stringify(spec, null, 2));

	return types[spec.type](spec);
}
Exemplo n.º 11
0
	ARRAY(paths).forEach(function get_routes_foreach_paths(path) {
		//debug.log('step');
		var d = require_if_exists(path +'/_default.js');

		if(d) {
			//debug.log('Detected: ', path +'/_default.js');
		}

		ARRAY(routes).forEach(function get_routes_foreach_routes(route) {
			//debug.log('step');
			var r = require_if_exists(path +'/' + route + '.js');

			if(r) {
				//debug.log('Detected: ', path +'/' + route + '.js');
			}

			// Skip if no handlers found
			if((d===undefined) && (r===undefined)) {
				return;
			}

			if(d) { register_raw_func(_raw, route, d); }
			if(r) { register_raw_func(_raw, route, r, d); }

		});

	});
Exemplo n.º 12
0
// Initialize route handler array if doesn't exist
function register_raw_func(_raw, route, f, d) {
	//debug.log('step');
	debug.assert(_raw).is('object');
	debug.assert(route).is('string');
	debug.assert(f).is('defined');

	if(!_raw.hasOwnProperty(route)) {
		_raw[route] = [];
	}

	if(f && _raw[route].indexOf(f) < 0) {
		_raw[route].push(f);
	}

	// Childs
	if(is.obj(f)) {
		ARRAY(Object.keys(f)).filter(other_than_methods).forEach(function register_raw_func_foreach(key) {
			debug.log('key = ', key);
			if(d) {
				register_raw_func(_raw, route+'/'+key, d);
			}
			register_raw_func(_raw, route+'/'+key, f[key], d);
		});
	}

}
Exemplo n.º 13
0
		return _Q.when(get_docs(req, res)).then(function export_docs_handler__(body) {
			debug.log('body = ', body);

			debug.assert(body).is('object');

			var type_obj = body.type;
			debug.assert(type_obj).is('object');

			var content = body.content;
			debug.assert(content).is('array');

			var view = (!export_all) && type_obj && type_obj.views &&
				type_obj.views.byName &&
				type_obj.views.byName[export_view];

			var keys = (view && view.listFields) || norUtils.getKeys(content) || [];

			var data = [];

			// Add header
			data.push( ARRAY(keys).map(function(key) {
				var title = norUtils.getTitleFromPath(type_obj, key);
				return ''+title;
			}).valueOf() );

			// Add data
			ARRAY(content).forEach(function(obj) {
				var result = {};
				var paths = norUtils.getPathsFromData(obj);
				ARRAY(paths).forEach(function(path) {
					var key = path.join('.');
					var value = norUtils.getDataFromPath(obj, path);
					result[key] = value;
				});

				data.push( ARRAY(keys).map(function(key) {
					return ''+result[key];
				}).valueOf() );
			});

			if(Object.keys(export_handlers).indexOf(export_type) >= 0) {
				return export_handlers[export_type](req, res, data, type_obj, view);
			}

			throw new HTTPError(500, "Unsupported export type: " + export_type);
		});
Exemplo n.º 14
0
	ARRAY(results).forEach(function merge_settled_results_foreach(result) {
		//debug.log('step');
		//debug.log('result = ', result);
		debug.assert(result).is('object');
		if(result.state === "fulfilled") {

			// Ignore undefined
			if(result.value === undefined) {
				no_reply = true;
				return;
			}

			debug.assert(result.value).is('object');
			//debug.log('result.value = ', result.value);
			ARRAY(Object.keys(result.value)).forEach(function merge_settled_results_foreach_2(key) {
				//debug.log('step');

				// Target does not have property yet
				if(!body.hasOwnProperty(key)) {
					//debug.log('result.value['+key+'] = ', result.value[key], ' as ', typeof result.value[key]);
					body[key] = result.value[key];
					//debug.log('merged body['+key+'] = ', body[key], ' as ', typeof body[key]);
					return;
				}

				// Target property is an array
				if(is.array(result.value[key])) {
					if(is.array(body[key])) {
						body[key] = body[key].concat(result.value[key]);
					} else {
						body[key] = result.value[key];
					}
					//debug.log('merged body['+key+'] = ', body[key], ' as ', typeof body[key]);
					return;
				}

				// Target property is an object
				if(is.object(result.value[key])) {
					//debug.log('body['+key+'] = ', body[key]);
					//debug.log('result.value['+key+'] = ', result.value[key]);
					body[key] = merge(body[key], result.value[key]);
					//debug.log('merged body['+key+'] = ', body[key], ' as ', typeof body[key]);
					return;
				}

				// Target property is string, number, boolean, etc
				body[key] = result.value[key];
				//debug.log('merged body['+key+'] = ', body[key], ' as ', typeof body[key]);

			});
		} else {
			if(!body.errors) {
				body.errors = [];
			}
			body.errors.push(result.reason);
		}
	});
Exemplo n.º 15
0
/** Run array of commands */
function run_commands(commands) {
	debug.assert(commands).is('array');
	//debug.log('commands = ' , commands);
	return ARRAY(commands).map(function step_builder(command) {
		return function step() {
			return run_command(command);
		};
	}).reduce(_Q.when, _Q());
}
Exemplo n.º 16
0
/** Prepare views for publification as a REST resource */
function prepare_views_resource(req, type, views) {
	var res = {
		'content': prepare_views(req, views)
	};
	if(type && type.$name) {
		res.$ref = ref(req, 'api/database/types', type.$name, 'views');
	}

	var byName = {};
	ARRAY(res.content).forEach(function(view) {
		byName[view.$name] = view;
	});
	res.byName = byName;

	var byID = {};
	ARRAY(res.content).forEach(function(view) {
		byID[view.$id] = view;
	});
	res.byID = byID;

	return res;
}
Exemplo n.º 17
0
}).then(function() {

	return ARRAY(argv._).map(function(command) {
		var fun = commands[command];
		if(!fun) {
			throw new TypeError("unknown command: " + command);
		}
		return fun;
	}).reduce(function(a, b) {
		return a.then( FUNCTION(b).curry(context) );
	}, _Q());

}).fail(function(err) {
Exemplo n.º 18
0
module.exports = function(spec) {

	debug.assert(spec).is('object');
	debug.assert(spec.routes).ignore(undefined).is('object');
	debug.assert(spec.params).ignore(undefined).is('object');
	debug.assert(spec.path).ignore(undefined).is('string');

	var modules = {};

	if(spec.routes && spec.params.path) {
		ARRAY(Object.keys(spec.routes)).forEach(function(key) {
			var route = spec.routes[key];
			modules[key] = require(PATH.resolve(spec.path, route));
		});
	} else {
		debug.warn("No routes detected.");
	}

	return function(opts) {

		// Verify and initialize opts
		debug.assert(opts).ignore(undefined).is('object');
		if(!opts) {
			opts = {};
		}

		// Copy spec.params as options
		if(is.object(spec.params)) {
			ARRAY(Object.keys(spec.params)).forEach(function(key) {
				if(!opts.hasOwnProperty(key)) {
					opts[key] = copy(spec.params[key]);
				}
			});
		}

		// Build routes
		var tmp = {};
		if(spec.routes) {
			ARRAY(Object.keys(spec.routes)).forEach(function(key) {
				if(is.func(modules[key])) {
					tmp[key] = modules[key](opts);
				} else if(is.obj(modules[key])) {
					tmp[key] = modules[key];
				}
			});
		}
		return tmp;
	};

};
Exemplo n.º 19
0
module.exports = function nor_newrelic_deployment(opts) {
	opts = opts || {};

	// curl -H "x-api-key:REPLACE_WITH_YOUR_API_KEY" -d "deployment[app_name]=REPLACE_WITH_YOUR_APP_NAME" https://api.newrelic.com/deployments.xml

	// Check parameters
	debug.assert(opts.api_key).ignore(undefined).is('string');
	debug.assert(opts.app_name).ignore(undefined).is('string');
	debug.assert(opts.application_id).ignore(undefined).is('string');
	debug.assert(opts.description).ignore(undefined).is('string');
	debug.assert(opts.revision).ignore(undefined).is('string');
	debug.assert(opts.changelog).ignore(undefined).is('string');
	debug.assert(opts.user).ignore(undefined).is('string');

	if(!( opts.app_name || opts.application_id )) {
		throw new TypeError("opts.app_name or opts.application_id required!");
	}

	if(!opts.api_key) {
		throw new TypeError("Missing required opts.api_key");
	}
	debug.log('opts = ', opts);

	// Build body
	var data = {};
	ARRAY(['app_name', 'application_id','description','revision','changelog','user']).forEach(function(key) {
		if(opts[key] !== undefined) {
			data['deployment['+ key +']'] = opts[key];
		}
	});
	debug.log('data = ', data);

	var body = require('querystring').stringify(data);
	debug.log('body = ', body);

	// Create request
	debug.log('POSTing to https://api.newrelic.com/deployments.xml...');
	return request.plain('https://api.newrelic.com/deployments.xml', {
		'method': 'POST',
		'headers': {
			'Content-Type': 'application/x-www-form-urlencoded;charset=utf8',
			'x-api-key': opts.api_key
		},
		'body': body
	});

};
Exemplo n.º 20
0
/** Get Cookie header line for an URL
 * @params url {object} The parsed object
 */
function get_cookies(url) {
	debug.assert(url).is('object');
	debug.assert(url.host).is('string');

	if(!is.array(_cookies[url.host])) {
		//debug.log('No cookies for host ', url.host);
		return undefined;
	}

	var cookies = ARRAY(_cookies[url.host]).map(function(cookie) {
		var i = cookie.indexOf(';');
		return cookie.substr(0, ((i >= 1) ? i : cookie.length));
	}).valueOf();

	//debug.log('Cookies found (for host ', url.host ,'): ', cookies);

	return cookies;
}
Exemplo n.º 21
0
/** Returns build features as string */
function get_build_features() {
	var features = {};
	if(build_opts.is_production_build) {
		features.mode = 'production';
	} else {
		features.mode = 'development';
	}
	if(build_opts.enable_source_maps) {
		features.source_maps = true;
	}
	if(build_opts.minimize_bundle) {
		features.minimize = true;
	}
	if(build_opts.use_disc) {
		features.disc = true;
	}
	return ARRAY(Object.keys(features)).map(function(key) {
		return key + '=' + features[key];
	}).join(', ');
}
Exemplo n.º 22
0
			return tr.searchTypes({'$name':type}).then(function(tr) {
				var type_obj = tr.fetchSingle();
				var schema = type_obj.$schema || {};
				var properties = schema.properties || {};
				ARRAY(Object.keys(properties)).forEach(function(key) {
					if(data.hasOwnProperty(key)) {
						content[key] = data[key];
					}
				});

				return tr.create(type)(content).then(function(tr) {
					var obj = tr.fetch();
					return {
						'title': 'Created a document',
						'$status': 303,
						'$type': 'redirect',
						'content': prepare_doc(req, obj),
						'$ref': ref(req, 'api/database/types', obj.$type, 'documents', obj.$id)
					};
				});
			});
Exemplo n.º 23
0
/** Initialize new types */
function initialize_types(tr, docs) {
	debug.assert(tr).is('object');
	debug.assert(docs).is('object');
	return ARRAY(Object.keys(docs)).map(function(type) {
		return function step() {
			//debug.log('type = ', type);
			//debug.log('docs[type] = ', docs[type]);

			return check_if_initialized(tr, type).then(function(has_been_initialized) {
				if(has_been_initialized) {
					//debug.log(type + ': already exists');
					return;
				}
				return tr.declareType(type)(docs[type]).then(function(tr_) {
					tr_.fetch();
					debug.info(type + ': Initialized new document type');
				});
			});

		};
	}).reduce(_Q.when, _Q());
}
Exemplo n.º 24
0
/** Prepare methods for publification */
function prepare_method(req, method) {
	debug.assert(req).is('object');
	debug.assert(method).is('object').instanceOf(nopg.Method);
	var tmp;
	try {
		tmp = JSON.parse(JSON.stringify(method));
	} catch(e) {
		debug.log('Failed to copy: ', method);
		throw e;
	}
	var meta = tmp.$meta;
	delete tmp.$events;
	delete tmp.$meta;
	// FIXME: This api/database/methods should use configuration paths
	tmp.$ref = ref(req, 'api/database/types', method.$type, 'methods', tmp.$name);
	if(meta) {
		ARRAY(Object.keys(meta)).forEach(function(key) {
			tmp[key] = meta[key];
		});
	}
	return tmp;
}
Exemplo n.º 25
0
/** Assert that we have logged in
 * @param required_flags {array} Optional array of flags, which at least one must match.
 */
function assert_logged_in(req, required_flags) {
	debug.assert(req).is('object');
	debug.assert(req.flags).ignore(undefined).is('object');
	debug.assert(required_flags).ignore(undefined).is('array');

	var flags = req.flags || {};

	var logged_in = flags.authenticated ? true : false;
	if(!logged_in) {
		throw new HTTPError(401);
	}

	if(required_flags && (required_flags.length >= 0)) {
		var value = ARRAY(required_flags).some(function(flag) {
			if(flags.hasOwnProperty(flag)) {
				return flags[flag] === true;
			}
		});
		if(!value) {
			throw new HTTPError(403);
		}
	}
}
Exemplo n.º 26
0
/** Prepare document for publification */
function prepare_doc(req, doc) {
	var tmp = JSON.parse(JSON.stringify(doc));
	var content = tmp.$content;
	delete tmp.$events;
	delete tmp.$content;
	// FIXME: This api/database/types should use configuration paths
	tmp.$ref = ref(req, 'api/database/types', tmp.$type, 'documents', tmp.$id);
	if(content) {
		ARRAY(Object.keys(content)).forEach(function(key) {
			tmp[key] = content[key];
		});
	}

	var childs;
	if(tmp.hasOwnProperty('$documents')) {
		childs = tmp.$documents;
		Object.keys(childs).forEach(function(id) {
			var child = childs[id];
			childs[id] = prepare_doc(req, child);
		});
	}

	return tmp;
}
Exemplo n.º 27
0
	function logger() {
		var args = Array.prototype.slice.call(arguments);
		return fun( ARRAY(args).map(get_stack).map(inspect_and_trim).join(' ') );
	}
Exemplo n.º 28
0
Q.fcall(function() {

	// PGCONFIG
	PGCONFIG = ARGV.pg || process.env.PGCONFIG || undefined;

	if(!PGCONFIG) {
		throw new TypeError("No pg configuration!");
	}

	comname = path.basename( comname );

	// -v -- enable verbose mode
	if(ARGV.v) {
		debug.setNodeENV('development');
	} else {
		debug.setNodeENV('production');
	}

	// Other options
	var keys = ['_', 'v'];
	var opts = {};
	ARRAY(Object.keys(ARGV)).forEach(function(key) {
		if(keys.indexOf(key) !== -1) {
			return;
		}
		if (key[0] === '-') {
			opts[ '$' + key.substr(1) ] = ARGV[key];
		} else {
			opts[key] = ARGV[key];
		}
	});

	// Add admin flags
	opts.flags = {'admin': true};

	debug.log('opts = ', opts);

	if(!opts.email) {
		return do_usage();
	}

	debug.assert(opts).is('object');
	debug.assert(opts.email).is('string');
	debug.assert(opts.password).ignore(undefined).is('string');

	if(!opts.password) {
		opts.password = Math.random().toString(36).substr(2, 10);
		console.log('Created password: '******'string');

	// The action
	return crypt(opts.password).then(function(hashed_password) {
		opts.password = hashed_password;
		return NoPg.transaction(PGCONFIG, function(db) {
			return db.count("User")({'email': opts.email}).then(function(db) {
				var users = db.fetch();
				if(users !== 0) {
					throw new TypeError("Email reserved");
				}
				return db.create("User")(opts);
			}).then(function(db) {
				var u = db.fetch();
				console.log( 'User added with ' + JSON.stringify(u.$content, null, 2) );
			});
		});
	});

}).then(function() {
Exemplo n.º 29
0
/** Returns Express application instance */
function app_builder(opts) {
	//debug.log('step');
	debug.log('app_builder() started');
	debug.assert(opts).ignore(undefined).is('object');
	opts = opts || {};

	debug.assert(opts.routePaths).ignore(undefined).is('array');
	debug.assert(opts.routes).ignore(undefined).is('array');
	debug.assert(opts.documents).ignore(undefined).is('object');

	opts.routePaths = opts.routePaths || [];
	opts.routes = opts.routes || [];
	opts.routePaths.unshift(__dirname + '/routes');
	opts.documents = opts.documents || {};

	//debug.log('opts.routes = ', opts.routes);
	//debug.log('opts.routePaths = ', opts.routePaths);

	var urlencoded_parser = bodyParser.urlencoded({ extended: false });
	var json_parser = bodyParser.json();

	var app = express();

	var routes = get_routes(opts.routes, opts.routePaths);

	debug.assert(routes).is('object');
	debug.assert(routes.$get).is('object');
	debug.assert(routes.$post).is('object');
	debug.assert(routes.$del).is('object');
	debug.assert(routes.$use).is('object');

	//debug.log('routes.$get = ', routes.$get);
	//debug.log('routes.$post = ', routes.$post);
	//debug.log('routes.$del = ', routes.$del);
	//debug.log('routes.$use = ', routes.$use);

	// Remove other handlers for any $use routes
	ARRAY(Object.keys(routes.$use)).forEach(function app_builder_routes_foreach(route) {
		ARRAY(['$get', '$post', '$del']).forEach(function(method) {
			if(routes[method].hasOwnProperty(route)) {
				delete routes[method][route];
			}
		});
	});

	// 
	ARRAY(Object.keys(routes)).forEach(function app_builder_foreach_route_keys(type) {

		debug.log('Method ' + type + ' has ' + Object.keys(routes[type]).length + ' routes');

		ARRAY(Object.keys(routes[type])).forEach(function app_builder_foreach_type_keys(route) {
			var path = '/';
			if(route !== "index") {
				path += route;
			}
			if(type === '$use') {
				debug.log('Added USE route for '+ path);
				app.use(path, urlencoded_parser, json_parser, route_builder(routes.$use[route](opts)) );
				return;
			}
			if(type === '$get') {
				debug.log('Added GET route for '+ path);
				app.get(path, route_builder(routes.$get[route](opts)) );
				return;
			}
			if(type === '$post') {
				debug.log('Added POST route for '+ path);
				app.post(path, urlencoded_parser, json_parser, route_builder(routes.$post[route](opts)) );
				return;
			}
			if(type === '$del') {
				debug.log('Added DELETE route for '+ path);
				app.delete(path, urlencoded_parser, json_parser, route_builder(routes.$del[route](opts)) );
				return;
			}
		});
	});

	debug.log('app_builder() finished');

	return app;
}
Exemplo n.º 30
0
	ARRAY(Object.keys(_raw)).forEach(function get_routes_foreach_keys(route) {
		//debug.log('step');
		var funcs = _raw[route];

		var _handlers = {
			"$get": ARRAY(funcs).map(function get_routes_map_get_funcs(func) {
				if(is.func(func)) {
					return func;
				}
				if(is.obj(func) && func.hasOwnProperty('$get')) {
					return func.$get;
				}
			}).filter(function get_routes_filter_get(func) {
				return is.func(func);
			}).valueOf(),
			"$post": ARRAY(funcs).map(function get_routes_map_post_funcs(func) {
				if(is.func(func)) {
					return;
				}
				if(is.obj(func) && func.hasOwnProperty('$post')) {
					return func.$post;
				}
			}).filter(function get_routes_filter_post_funcs(func) {
				return is.func(func);
			}).valueOf(),
			"$del": ARRAY(funcs).map(function get_routes_map_del_funcs(func) {
				if(is.func(func)) {
					return;
				}
				if(is.obj(func) && func.hasOwnProperty('$del')) {
					return func.$del;
				}
			}).filter(function get_routes_filter_del_funcs(func) {
				return is.func(func);
			}).valueOf(),
			"$use": ARRAY(funcs).map(function get_routes_map_use_funcs(func) {
				if(is.func(func)) {
					return;
				}
				if(is.obj(func) && func.hasOwnProperty('$use')) {
					return func.$use;
				}
			}).filter(function get_routes_filter_use_funcs(func) {
				return is.func(func);
			}).valueOf()
		};

		debug.log(
			'There are ' + _handlers.$get.length +' GET handlers loaded for ' + route + '\n' +
			'There are ' + _handlers.$post.length +' POST handlers loaded for ' + route + '\n' +
			'There are ' + _handlers.$del.length +' DEL handlers loaded for ' + route + '\n' +
			'There are ' + _handlers.$use.length +' USE handlers loaded for '+ route + '\n'
		);

		ARRAY(Object.keys(_handlers)).forEach(function get_routes_foreach_handler_keys(method) {
			//debug.log('step');

			debug.log(route + ': Route has ' + _handlers[method].length + ' '+method+' handlers');
			if(_handlers[method].length === 0) {
				return;
			}

			result[method][route] = function get_routes_result_method_route(opts) {
				//debug.log('step');

				var handlers = _handlers[method].map(function get_routes_map_method_handlers(func) {
					//debug.log('step');
					return func(opts);
				});
				debug.log(route + ': Route has ' + handlers.length + ' '+method+' handlers');

				return function get_routes_result_handler(req, res) {
					//debug.log('step');

					var promises = handlers.map(function get_routes_map_handlers(handler) {
						return _Q.when(handler(req, res));
					});

					debug.log(route + ': Created ' + promises.length + ' promises for route '+method);
					return _Q.allSettled(promises).then( merge_settled_results );

				};
			};
		});

	});