<commit_before>'use strict';

var router = require('router')();
var reqlog = require('reqlog');
var responseBuilder = require('apier-responsebuilder');
var dataParser = require('apier-dataparser');
var bodyParser = require('body-parser');

var jsonParser = bodyParser.json();

var middlewares = [test, jsonParser, dataParser, responseBuilder.init];
var self = this;

exports.init = function() {
	reqlog.info('apier initialized!!');
	var app = function(req, res) {
		router(req, res, function() {});
		router.all('*', middlewares, function(req, res) {
			reqlog.info('inside all endpoint', req.url);
			responseBuilder.error(req, res, 'NOT_FOUND');
		});
	};

	return app;
};

exports.endpoint = function(methods, url, callback) {
	// create an object to apply as this in the callback
	// its used to be able to call the send function inside the callback
	// without having to also pass the req and res
	for (var i = 0, length = methods.length; i < length; i++) {
		var method = methods[i].toLowerCase();
		reqlog.info('setup endpoint', method + ' ' + url);
		router[method](url, middlewares, function(req, res) {
			routerCallback(req, res, callback);
		});
	}
};

exports.send = function(data) {
	reqlog.info('inside api send');
	responseBuilder.send(this.req, this.res, data);
};

/**
 * Defines the endpoint callback passed into the router
 * @method routerCallback
 * @param  {object}       req      A request object
 * @param  {object}       res      A response object
 * @param  {Function}     callback The user defined endpoint callback
 */
function routerCallback(req, res, callback) {
	var innerSelf = {
		req: req,
		res: res,
		send: function(data) {
			self.send.call(this, data);
		}
	};
	reqlog.info('inside routerCallback for endpoint: ', req.url);
	callback.call(innerSelf, req, res);
}

/**
 * Dummy middleware
 * @method test
 * @param  {[type]}   req  [description]
 * @param  {[type]}   res  [description]
 * @param  {Function} next [description]
 */
function test(req, res, next) {
	reqlog.log('inside test');

	next();
}
<commit_msg>Major refactoring to make it work properly<commit_after>'use strict';

var router = require('router')();
var reqlog = require('reqlog');
var responseBuilder = require('apier-responsebuilder');
var dataParser = require('apier-dataparser');
var bodyParser = require('body-parser');

var jsonParser = bodyParser.json();

module.exports = apier;

/**
 * Create an apier app
 * @method apier
 * @return {Function} The app to use as server
 */
function apier() {
	reqlog.info('apier initialized!!');

	var app = function(req, res) {
		// this si the final handler if no match found
		router(req, res, function() {
			reqlog.info('NOT_FOUND:', req.method + ' ' + req.url);
			responseBuilder.error(req, res, 'NOT_FOUND');
		});
	};

	app.endpoint = endpoint;

	return app;
}

// just logs the request
router.use(function(req, res, next) {
	reqlog.error('REQUEST:', req.method + ' ' + req.url);
	next();
});
router.use(test);
router.use(jsonParser);
router.use(dataParser);
router.use(responseBuilder.init);
// when its an options request, just respond with 200
router.options('*', function(req, res) {
	reqlog.info('inside options');
	res.statusCode = 200;
	res.end();
});

/**
 * Create an endpoint
 * @method endpoint
 * @param  {array}   methods  The endpoint methods e.g. ['get'. 'post']
 * @param  {string}   url      The endpoint path e.g. '/users'
 * @param  {Function} callback The user defined callback
 */
function endpoint(methods, url, callback) {
	for (var i = 0, length = methods.length; i < length; i++) {
		var method = methods[i].toLowerCase();
		reqlog.info('setup endpoint', method + ' ' + url);
		router[method](url, function(req, res) {
			routerCallback(req, res, callback);
		});
	}
}

/**
 * Just sends the response using this
 * @method send
 * @param  {any} data The data to return
 */
function send(data) {
	reqlog.info('inside api send');
	responseBuilder.send(this.req, this.res, data);
}

/**
 * Defines the endpoint callback passed into the router
 *
 * create an object to apply as this in the callback
 * its used to be able to call the send function inside the callback
 * without having to also pass the req and res
 * @method routerCallback
 * @param  {object}       req      A request object
 * @param  {object}       res      A response object
 * @param  {Function}     callback The user defined endpoint callback
 */
function routerCallback(req, res, callback) {
	var innerSelf = {
		req: req,
		res: res,
		send: function(data) {
			send.call(this, data);
		}
	};
	reqlog.info('inside routerCallback for endpoint: ', req.url);
	callback.call(innerSelf, req, res);
}

/**
 * Dummy middleware
 * @method test
 * @param  {[type]}   req  [description]
 * @param  {[type]}   res  [description]
 * @param  {Function} next [description]
 */
function test(req, res, next) {
	reqlog.log('inside test');

	next();
}

{'fraction_non_alphanumeric': 0.14793624846751124, 'fraction_numerical': 0.0016346546791990192, 'mean_word_length': 3.9295065458207454, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '6007449', 'n_tokens_mistral': 1600, 'n_tokens_neox': 1536, 'n_words': 484}