<commit_before>/**
 * Module dependencies.
 */

// global: sails

var _		= require( 'lodash' ),
	util	= require( '../util');

/**
 * Expose `Router` constructor.
 */

module.exports = Router;



/**
 * Initialize a new `Router`
 *
 * @param {Object} options
 * @api private
 */

function Router ( options ) {

	// Maintain a reference to our Express app
	// (oh man, AMD would really make sense here.)
	this.app = sails.express.app;

	// Maintain a reference to the static route config
	this.staticRoutes = sails.config.routes;

	// Reference sails logger
	this.log = sails.log;
}


/**
 * Bind new route(s)
 *
 * @param {String|RegExp} path
 * @param {String|Object|Array|Function} bindTo
 * @param {String} verb
 * @api private
 */

Router.prototype.bind = require( './bind' );


/**
 * Unbind all routes currently attached to the router
 *
 * @param {String} verb
 * @api private
 */
Router.prototype.reset = function ( verb ) {
	if (!verb) {

		// Reset the routes for each HTTP method
		return _.each(this.app.routes, function ( routes, method ) {
			this.reset(method);
		}, this);
	}
	this.app.routes[verb] = [];
};


/**
 * Wipe everything and bind routes, in order of descending preference:
 *
 * -> static routes (routes config)
 * -> resourceful/view routes (config + middleware table)
 * -> fire afterRoute blueprint routes (blueprint config + middleware table)
 * -> default 404 handler
 * -> default 500 handler
 *
 * @api private
 */

Router.prototype.flush = function ( ) {

	this.reset();

	// Use specified path to bind static routes
	_.each(this.staticRoutes, function (target, path) {
		this.bind(path, target);
	}, this);

	////////////////////////////////////////////////////////////////////////
	// TODO: pull this into a plugin
	////////////////////////////////////////////////////////////////////////
	
	// Start iterating through controllers
	_.each(sails.controllers, function (controller, controllerId) {

		// Instead of using the actual controller definition,
		// look up the version in the middleware registry, 
		// since it might have policies attached
		controller = sails.middleware[controllerId];
		
		// Build path to bind to dynamic routes
		_.each(controller, function (target, actionId) {
			
			// If this isn't a valid target, bail out
			if (! (_.isFunction(target) || _.isArray(target)) ) {
				this.log.warn('Action ('+actionId+') in "'+controllerId+'" could not be dynamically routed because it isn\'t an array or a function.');
			}

			// Check for verb in actionId
			var detectedVerb = util.detectVerb(actionId);
			actionId = detectedVerb.original;
			var verb = detectedVerb.verb;

			// If a verb is set, the prefix looks like `get /`
			// otherwise, it's just a trailing slash
			var prefix = verb ? verb + ' /' : '/';

			// Bind dynamic routes
			if (actionId === 'index') {
				this.bind(prefix + controllerId, target);<commit_msg>Allow controllers to be functions or arrays as well as objects.<commit_after>/**
 * Module dependencies.
 */

// global: sails

var _		= require( 'lodash' ),
	util	= require( '../util');

/**
 * Expose `Router` constructor.
 */

module.exports = Router;



/**
 * Initialize a new `Router`
 *
 * @param {Object} options
 * @api private
 */

function Router ( options ) {

	// Maintain a reference to our Express app
	// (oh man, AMD would really make sense here.)
	this.app = sails.express.app;

	// Maintain a reference to the static route config
	this.staticRoutes = sails.config.routes;

	// Reference sails logger
	this.log = sails.log;
}


/**
 * Bind new route(s)
 *
 * @param {String|RegExp} path
 * @param {String|Object|Array|Function} bindTo
 * @param {String} verb
 * @api private
 */

Router.prototype.bind = require( './bind' );


/**
 * Unbind all routes currently attached to the router
 *
 * @param {String} verb
 * @api private
 */
Router.prototype.reset = function ( verb ) {
	if (!verb) {

		// Reset the routes for each HTTP method
		return _.each(this.app.routes, function ( routes, method ) {
			this.reset(method);
		}, this);
	}
	this.app.routes[verb] = [];
};


/**
 * Wipe everything and bind routes, in order of descending preference:
 *
 * -> static routes (routes config)
 * -> resourceful/view routes (config + middleware table)
 * -> fire afterRoute blueprint routes (blueprint config + middleware table)
 * -> default 404 handler
 * -> default 500 handler
 *
 * @api private
 */

Router.prototype.flush = function ( ) {

	this.reset();

	// Use specified path to bind static routes
	_.each(this.staticRoutes, function (target, path) {
		this.bind(path, target);
	}, this);

	////////////////////////////////////////////////////////////////////////
	// TODO: pull this into a plugin
	////////////////////////////////////////////////////////////////////////

	// Start iterating through controllers
	_.each(sails.controllers, function (controller, controllerId) {

		// Instead of using the actual controller definition,
		// look up the version in the middleware registry, 
		// since it might have policies attached
		controller = sails.middleware[controllerId];

		// If a controller is the middleware itself, 
		// create a route for it directly, then bail out
		if (_.isFunction(controller) || _.isArray(controller) ) {
			this.bind('/' + controllerId, controller);
			return;
		}
		
		// Build routes for each action
		_.each(controller, function (target, actionId) {
			
			// If this isn't a valid target, bail out
			if (! (_.isFunction(target) || _.isArray(target)) ) {
				this.log.warn('Action ('+actionId+') in "'+controllerId+'" could not be dynamically routed because it isn\'t an array or a function.');
				return;
			}

			// Check for verb in actionId
			var detectedVerb = util.detectVerb(actionId);
			actionId = detectedVerb.original;
			var verb = detectedVerb.verb;

			// If a verb is set, the prefix looks like `get /`
			// otherwise, it's just a trailing slash
			var prefix = verb ? verb + ' /' : '/';

			// Bind dynamic routes
			if (actionId === 'index') {
				this.bind(prefix + controllerId, target);
{'fraction_non_alphanumeric': 0.1734390485629336, 'fraction_numerical': 0.0019821605550049554, 'mean_word_length': 2.625748502994012, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6569843', 'n_tokens_mistral': 1901, 'n_tokens_neox': 1726, 'n_words': 591}