<commit_before>#!/usr/bin/env node

var extend = require('xtend'),
	argv = require('optimist').argv,
	fs = require('fs'),
	express = require('express'),
	//io = require('socket.io')(),
	api = require('./api'),
	allow = require('./allow'),
	accepts = require('./accepts'),
	authenticated = require('./authenticated'),
	cookieName = 'sid',
	mongoose = new (require('mongoose').Mongoose)(),
	models = require('./models')(mongoose);


mongoose.connect('mongodb://localhost/cmpt-474');

var app = express(),
	config = JSON.parse(fs.readFileSync(__dirname+'/../config.default.json')),
	manifest = JSON.parse(fs.readFileSync(__dirname+'/../package.json'));

var versioning = (function() {
	var branch = fs.readFileSync(__dirname+'/../.git/HEAD').toString('utf8').match(/ref:\s*refs\/heads\/(.*)/)[1],
		commit = fs.readFileSync(__dirname+'/../.git/refs/heads/'+branch);
	return {
		package: manifest.version,
		branch: branch,
		commit: commit,
		headers: function() { 
			return function(req, res, next) {
				res
					.set('X-Active-Commit', commit)
					.set('X-Active-Branch', branch);
				next();
			}
		}
	}
})()

//Try to load the JSON configuration file for the system.
try {
	//Either use the user supplied --config file.json option
	//from the command line or the default.
	var file = argv['config'] || './config/index.json';
	//And try and read the JSON data from it
	config = extend(config, JSON.parse(fs.readFileSync(file)));
}
catch (err) {
	//If we're here, reading the config file has failed so
	//let the user know about it and exit the program.
	if (argv['config']) {
		process.stderr.write('unable to load configuration file '+file+': '+err+'\n');
		return process.exit(1);
	}
}

//for (var entry in config.paths)
//	config.paths[entry] = fs.realpathSync(config.paths[entry]);

if (argv['version']) {
	process.stdout.write(''+versioning.package+"\n");
	return process.exit(0);
}

if (argv['show-config']) {
	process.stdout.write(JSON.stringify(config, null, '\t')+'\n');
	return process.exit(0);
}

app.set('mongoose', mongoose);

// Set the base URL
app.set('base', '');

app.set('view engine', 'jade');
app.locals.basedir = app.get('views');

function error(verbose) {
	
	return function(err, req, res, next) {
		//Since only the developers are going to see this error
		//just pipe the data right back to them.
		
		var types = [
			'application/json', 
			'application/xml+xhtml', 
			'text/html', 
			'text/plain'
		];

		var code = err.statusCode || 500;

		console.log('HTTP',code,req.path,err);
		if (err.stack) console.log(err.stack);

		switch(req.accepts(types)) {
		case 'application/json':
			return res.send(code, verbose ? err : { error: 'INTERNAL_ERROR' });
		case 'application/xml+xhtml':
		case 'text/html':
			return res.status(code).render('errors/'+code, { data: verbose ? err : undefined, statusCode: code });
		case 'text/plain':
		default:
			return res.send(code, 'HTTP '+code+' error: '+(verbose ? err : ''));
		}
	}
}

app.configure('production', 'development', function() {


	var redis = require('redis').createClient(), RedisStore = require('connect-redis')(express);
	app.set('redis', redis);
	app.set('session store', new RedisStore({ client: redis }));
	app.set('secret key', config.secretKey)

	app.set('authentication delay', 1000);
})

//Production configuration settings
app.configure('production', function() {
	//Allow for compressed responses to save on bandwidth
	app.use(express.compress());

	
})



//Development/testing configuration settings
app.configure('development', function() {
	//Log requests
	app.use(express.logger('dev'))
});



//Testing configuration settings
app.configure('test', function() {
	//Use a mock instead of the real thing for testing
	app.set('redis', require('redis-mock').createClient());
	app.set('secret key', null);
	app.set('authentication delay', 1);
	app.use(express.logger('dev'))

	
});


//Export application version for debugging
app.use(versioning.headers());

//Export static content
app.use('/styles', express.static(__dirname+'/assets/styles'));
app.use('/scripts', express.static(__dirname+'/assets/scripts'));
app.use('/images', express.static(__dirname+'/assets/images'));
app.use('/fonts', express.static(__dirname+'/assets/fonts'));
<commit_msg>Fixed hardcoded path slightly.<commit_after>#!/usr/bin/env node

var extend = require('xtend'),
	argv = require('optimist').argv,
	fs = require('fs'),
	express = require('express'),
	//io = require('socket.io')(),
	api = require('./api'),
	allow = require('./allow'),
	accepts = require('./accepts'),
	authenticated = require('./authenticated'),
	cookieName = 'sid',
	mongoose = new (require('mongoose').Mongoose)(),
	models = require('./models')(mongoose);


mongoose.connect('mongodb://localhost/cmpt-474');

var app = express(),
	path = fs.realpathSync(__dirname+'/../'),
	config = JSON.parse(fs.readFileSync(path+'/config.default.json')),
	manifest = JSON.parse(fs.readFileSync(path+'/package.json'));

var versioning = (function() {
	var branch = fs.readFileSync(path+'/.git/HEAD').toString('utf8').match(/ref:\s*refs\/heads\/(.*)/)[1],
		commit = fs.readFileSync(path+'/.git/refs/heads/'+branch);
	return {
		package: manifest.version,
		branch: branch,
		commit: commit,
		headers: function() { 
			return function(req, res, next) {
				res
					.set('X-Active-Commit', commit)
					.set('X-Active-Branch', branch);
				next();
			}
		}
	}
})()

//Try to load the JSON configuration file for the system.
try {
	//Either use the user supplied --config file.json option
	//from the command line or the default.
	var file = argv['config'] || './config/index.json';
	//And try and read the JSON data from it
	config = extend(config, JSON.parse(fs.readFileSync(file)));
}
catch (err) {
	//If we're here, reading the config file has failed so
	//let the user know about it and exit the program.
	if (argv['config']) {
		process.stderr.write('unable to load configuration file '+file+': '+err+'\n');
		return process.exit(1);
	}
}

//for (var entry in config.paths)
//	config.paths[entry] = fs.realpathSync(config.paths[entry]);

if (argv['version']) {
	process.stdout.write(''+versioning.package+"\n");
	return process.exit(0);
}

if (argv['show-config']) {
	process.stdout.write(JSON.stringify(config, null, '\t')+'\n');
	return process.exit(0);
}

app.set('mongoose', mongoose);

// Set the base URL
app.set('base', '');

app.set('view engine', 'jade');
app.locals.basedir = app.get('views');

function error(verbose) {
	
	return function(err, req, res, next) {
		//Since only the developers are going to see this error
		//just pipe the data right back to them.
		
		var types = [
			'application/json', 
			'application/xml+xhtml', 
			'text/html', 
			'text/plain'
		];

		var code = err.statusCode || 500;

		console.log('HTTP',code,req.path,err);
		if (err.stack) console.log(err.stack);

		switch(req.accepts(types)) {
		case 'application/json':
			return res.send(code, verbose ? err : { error: 'INTERNAL_ERROR' });
		case 'application/xml+xhtml':
		case 'text/html':
			return res.status(code).render('errors/'+code, { data: verbose ? err : undefined, statusCode: code });
		case 'text/plain':
		default:
			return res.send(code, 'HTTP '+code+' error: '+(verbose ? err : ''));
		}
	}
}

app.configure('production', 'development', function() {


	var redis = require('redis').createClient(), RedisStore = require('connect-redis')(express);
	app.set('redis', redis);
	app.set('session store', new RedisStore({ client: redis }));
	app.set('secret key', config.secretKey)

	app.set('authentication delay', 1000);
})

//Production configuration settings
app.configure('production', function() {
	//Allow for compressed responses to save on bandwidth
	app.use(express.compress());

	
})



//Development/testing configuration settings
app.configure('development', function() {
	//Log requests
	app.use(express.logger('dev'))
});



//Testing configuration settings
app.configure('test', function() {
	//Use a mock instead of the real thing for testing
	app.set('redis', require('redis-mock').createClient());
	app.set('secret key', null);
	app.set('authentication delay', 1);
	app.use(express.logger('dev'))

	
});


//Export application version for debugging
app.use(versioning.headers());

//Export static content
app.use('/styles', express.static(path+'/assets/styles'));
app.use('/scripts', express.static(path+'/assets/scripts'));
app.use('/images', express.static(path+'/assets/images'));
app.use('/fonts', express.static(path+'/assets/fonts'));

{'fraction_non_alphanumeric': 0.19080648953679755, 'fraction_numerical': 0.0037620503174229957, 'mean_word_length': 4.357052896725441, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6453816', 'n_tokens_mistral': 2939, 'n_tokens_neox': 2735, 'n_words': 653}