<commit_before>
// server.js
// for QI Labs
// Set up a server.

if (require.main === module) {
	throw new Error("Wrong usage of server.js. Call the master file on root.");
}

// module.exports.ga = require('universal-analytics')(nconf.get('GA_ID'));

/*---------------------------------------------------------------------------**/
/*---------------------------------------------------------------------------**/
// server.js specifics below

// Utils
var
	cluster = require('cluster')
,	path = require('path')
, nconf = require('nconf')
;

/**
 * Nodetime stats.
 */
if (nconf.get('NODETIME_ACCOUNT_KEY')) {
	require('nodetime').profile({
		accountKey: nconf.get('NODETIME_ACCOUNT_KEY'),
		appName: 'QI LABS',
	});
}

// Server-related libraries
var
	bParser	= require('body-parser')
, mongoose= require('mongoose')
,	passport= require('passport')
,	express = require('express')
,	helmet 	= require('helmet')
, http 		= require('http')
, st 			= require('st')
;

var app = express();

// var toobusy = require('toobusy');
// app.use(function(req, res, next) {
//   if (toobusy()) res.status(503).send('Servidor ocupado. Tente novamente.');
//   else next();
// });

if (!global.logger) {
	throw new Error("Global logger object not found.");
}
app.set('logger', logger);
app.use(function (req, res, next) {
	req.logger = logger;
	next();
});

require('./config/s3')(app);
require('./config/passport')(app);

/**
 * Template engines and static files.
 */
var swig = require('./config/swig')
app.engine('html', swig.renderFile);
app.set('view engine', 'html'); 			// make '.html' the default
app.set('views', nconf.get('viewsRoot')); 	// set views for error and 404 pages

if (nconf.get('env') === 'development') {
	app.set('view cache', false);
	swig.setDefaults({ cache: false });
}

app.use(require('compression')());
app.use('/robots.txt', express.static(path.join(nconf.get('staticRoot'), 'robots.txt')));
app.use('/humans.txt', express.static(path.join(nconf.get('staticRoot'), 'humans.txt')));
app.use(require('serve-favicon')(path.join(nconf.get('staticRoot'), 'favicon.ico')));
app.use(st({
	path: nconf.get('staticRoot'),
	url: nconf.get('staticUrl'),
	cache: nconf.get('env') === 'production',
	passthrough: false,
}));

/*---------------------------------------------------------------------------**/
/* BEGINNING of a DO_NOT_TOUCH_ZONE -----------------------------------------**/
app.use(helmet.defaults());
app.use(bParser.urlencoded({ extended: true }));
app.use(bParser.json());
app.use(require('method-override')());
app.use(require('express-validator')());
app.use(require('cookie-parser')());
/** END of a DO_NOT_TOUCH_ZONE ----------------------------------------------**/
/**--------------------------------------------------------------------------**/


/*---------------------------------------------------------------------------**/
/** BEGINNING of a SHOULD_NOT_TOUCH_ZONE ------------------------------------**/
var session = require('express-session');
app.use(session({
	store: new (require('connect-mongo')(session))({ mongooseConnection: mongoose.connection }),
	// store: new (require('connect-redis')(session))({ url: nconf.get('REDISCLOUD_URL') || '' }),
	secret: nconf.get('SESSION_SECRET') || 'mysecretes',
	cookie: {
		httpOnly: true,
		secure: false,
		maxAge: 24*60*60*1000,
	},
	rolling: true,
	resave: true,
	saveUninitialized: true,
}));
app.use(require('csurf')());
app.use(function(req, res, next){
	res.locals.token = req.csrfToken();	// Add csrf token to views's locals.
	console.log(req.csrfToken())
	next();
});
app.use(require('connect-flash')()); 	// Flash messages middleware
app.use(passport.initialize());
app.use(passport.session());
/** END of a SHOULD_NOT_TOUCH_ZONE ------------------------------------------**/
/**--------------------------------------------------------------------------**/

app.use(require('./middlewares/flash_messages'));
app.use(require('./middlewares/local_user'));
app.use(require('express-domain-middleware'));
app.use(require('./middlewares/reqExtender'));
app.use(require('./middlewares/resExtender'));
require('./middlewares/locals.js')(app);

// Install app, guides and api controllers. The app must be kept for last,
// because it works on / so its middlewares would match every 404 call passing
// through.
app.use('/api', require('./controllers/api')(app));
app.use('/guias', require('./controllers/guides')(app));
app.use('/', require('./controllers')(app));

app.use(require('./middlewares/handle_404')); // Handle 404, in case no one catched it<commit_msg>Ops. Don't log csrf tokens for each request.<commit_after>
// server.js
// for QI Labs
// Set up a server.

if (require.main === module) {
	throw new Error("Wrong usage of server.js. Call the master file on root.");
}

// module.exports.ga = require('universal-analytics')(nconf.get('GA_ID'));

/*---------------------------------------------------------------------------**/
/*---------------------------------------------------------------------------**/
// server.js specifics below

// Utils
var
	cluster = require('cluster')
,	path = require('path')
, nconf = require('nconf')
;

/**
 * Nodetime stats.
 */
if (nconf.get('NODETIME_ACCOUNT_KEY')) {
	require('nodetime').profile({
		accountKey: nconf.get('NODETIME_ACCOUNT_KEY'),
		appName: 'QI LABS',
	});
}

// Server-related libraries
var
	bParser	= require('body-parser')
, mongoose= require('mongoose')
,	passport= require('passport')
,	express = require('express')
,	helmet 	= require('helmet')
, http 		= require('http')
, st 			= require('st')
;

var app = express();

// var toobusy = require('toobusy');
// app.use(function(req, res, next) {
//   if (toobusy()) res.status(503).send('Servidor ocupado. Tente novamente.');
//   else next();
// });

if (!global.logger) {
	throw new Error("Global logger object not found.");
}
app.set('logger', logger);
app.use(function (req, res, next) {
	req.logger = logger;
	next();
});

require('./config/s3')(app);
require('./config/passport')(app);

/**
 * Template engines and static files.
 */
var swig = require('./config/swig')
app.engine('html', swig.renderFile);
app.set('view engine', 'html'); 			// make '.html' the default
app.set('views', nconf.get('viewsRoot')); 	// set views for error and 404 pages

if (nconf.get('env') === 'development') {
	app.set('view cache', false);
	swig.setDefaults({ cache: false });
}

app.use(require('compression')());
app.use('/robots.txt', express.static(path.join(nconf.get('staticRoot'), 'robots.txt')));
app.use('/humans.txt', express.static(path.join(nconf.get('staticRoot'), 'humans.txt')));
app.use(require('serve-favicon')(path.join(nconf.get('staticRoot'), 'favicon.ico')));
app.use(st({
	path: nconf.get('staticRoot'),
	url: nconf.get('staticUrl'),
	cache: nconf.get('env') === 'production',
	passthrough: false,
}));

/*---------------------------------------------------------------------------**/
/* BEGINNING of a DO_NOT_TOUCH_ZONE -----------------------------------------**/
app.use(helmet.defaults());
app.use(bParser.urlencoded({ extended: true }));
app.use(bParser.json());
app.use(require('method-override')());
app.use(require('express-validator')());
app.use(require('cookie-parser')());
/** END of a DO_NOT_TOUCH_ZONE ----------------------------------------------**/
/**--------------------------------------------------------------------------**/


/*---------------------------------------------------------------------------**/
/** BEGINNING of a SHOULD_NOT_TOUCH_ZONE ------------------------------------**/
var session = require('express-session');
app.use(session({
	store: new (require('connect-mongo')(session))({ mongooseConnection: mongoose.connection }),
	// store: new (require('connect-redis')(session))({ url: nconf.get('REDISCLOUD_URL') || '' }),
	secret: nconf.get('SESSION_SECRET') || 'mysecretes',
	cookie: {
		httpOnly: true,
		secure: false,
		maxAge: 24*60*60*1000,
	},
	rolling: true,
	resave: true,
	saveUninitialized: true,
}));
app.use(require('csurf')());
app.use(function(req, res, next){
	res.locals.token = req.csrfToken();	// Add csrf token to views's locals.
	next();
});
app.use(require('connect-flash')()); 	// Flash messages middleware
app.use(passport.initialize());
app.use(passport.session());
/** END of a SHOULD_NOT_TOUCH_ZONE ------------------------------------------**/
/**--------------------------------------------------------------------------**/

app.use(require('./middlewares/flash_messages'));
app.use(require('./middlewares/local_user'));
app.use(require('express-domain-middleware'));
app.use(require('./middlewares/reqExtender'));
app.use(require('./middlewares/resExtender'));
require('./middlewares/locals.js')(app);

// Install app, guides and api controllers. The app must be kept for last,
// because it works on / so its middlewares would match every 404 call passing
// through.
app.use('/api', require('./controllers/api')(app));
app.use('/guias', require('./controllers/guides')(app));
app.use('/', require('./controllers')(app));

app.use(require('./middlewares/handle_404')); // Handle 404, in case no one catched it
{'fraction_non_alphanumeric': 0.32307354072771244, 'fraction_numerical': 0.005716170165988787, 'mean_word_length': 2.5734485467399844, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1400009', 'n_tokens_mistral': 2860, 'n_tokens_neox': 2823, 'n_words': 559}