<commit_before>
/**
 * Module dependencies
 */

var routes = require('./routes'),
  routesApi = require('./routes/api'),
  routesChat = require('./routes/chat'),
  http = require('http'),
  path = require('path'),
  longjohn = require('longjohn');

var config = require('./config');
var express = config.express;
var app = config.app;
var server = config.server;
var io = config.io;
var statistics = require('./statistics');

var db = require('./db');
var Banned = db.Banned;

/**
 * Configuration
 */

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
if (app.get('env') === 'development') {
  app.use(require('less-middleware')({ src : __dirname + '/public', enable: ['less']}));
}
app.use(determineEnv());
app.use(isBanned());
app.use(express.cookieParser(config.secretKey));
app.use(express.session({
  store: config.redisStore,
  secret: config.secretKey,
  prefix: config.sessionPrefix,
  cookie: { expires: false },
}));
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use('/static', express.static(path.join(__dirname, 'public')));
//app.use(statistics());
var appPages = ['/chat', '/api/user-data'];
app.use(authenticate(appPages));
//app.use(express.favicon(path.join(__dirname, 'public/img/fav.gif')));
app.use(app.router);

// development only
if (app.get('env') === 'development') {
  app.use(express.errorHandler());
}

// production only
if (app.get('env') === 'production') {
  // TODO
}


/**
 * Routes
 */

// serve index and view partials
app.get('/', routes.index);
app.post('/auth', routes.auth);
app.get('/exit', routes.exit);
app.get('/chat', routesChat.chat);
app.get('/partials/:name', routes.partials);
app.get('/rules', routes.rules);
app.get('/about', routes.about);

// JSON API
app.get('/api/version', routesApi.version);
app.get('/api/user-data', routesApi.userData);

// redirect all others to the index (HTML5 history)
app.get('*', routes.index);

// Socket.io Communication
io.sockets.on('connection', require('./routes/socket'));

/**
 * Start Server
 */

server.listen(app.get('port'), function () {
  console.log('Express server listening on port ' + app.get('port'));
});

function isBanned() {
  return function (req, res, next) {
    var ip;
    if (req.headers['x-nginx-proxy']) {
      ip = req.headers['x-real-ip'];
    } else {
      ip = req.connection.remoteAddress;
    }
    Banned.findOne({ip: ip}, function (err, banned) {
      if (err || !banned) {
        next();
      } else {
        if (banned.expires <= new Date(Date.now())) {
          banned.remove(function (err) {
            if (err) {
              console.error('[Middleware] Could not remove banned from db.');
            }
          });
          next();
        } else {
          res.render('banned', {expireDate: banned.expires.toDateString()});
        }
      }
    });
  };
}

function authenticate(appPages) {
  return function (req, res, next) {
    if (appPages.indexOf(req.path) == -1) {
      next();
    } else {
      if (!req.session.loggedIn) {
        res.redirect('/');
      } else {
        next();
      }
    }
  };
}

function determineEnv() {
  return function (req, res, next) {
    req.development = false;
    if (app.get('env') === 'development') {
      req.development = true;
    }
    next();
  };
}
<commit_msg>Accept static requests for banneds.<commit_after>
/**
 * Module dependencies
 */

var routes = require('./routes'),
  routesApi = require('./routes/api'),
  routesChat = require('./routes/chat'),
  http = require('http'),
  path = require('path'),
  longjohn = require('longjohn');

var config = require('./config');
var express = config.express;
var app = config.app;
var server = config.server;
var io = config.io;
var statistics = require('./statistics');

var db = require('./db');
var Banned = db.Banned;

/**
 * Configuration
 */

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
if (app.get('env') === 'development') {
  app.use(require('less-middleware')({ src : __dirname + '/public', enable: ['less']}));
}
app.use(determineEnv());
app.use(isBanned());
app.use(express.cookieParser(config.secretKey));
app.use(express.session({
  store: config.redisStore,
  secret: config.secretKey,
  prefix: config.sessionPrefix,
  cookie: { expires: false },
}));
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use('/static', express.static(path.join(__dirname, 'public')));
//app.use(statistics());
var appPages = ['/chat', '/api/user-data'];
app.use(authenticate(appPages));
//app.use(express.favicon(path.join(__dirname, 'public/img/fav.gif')));
app.use(app.router);

// development only
if (app.get('env') === 'development') {
  app.use(express.errorHandler());
}

// production only
if (app.get('env') === 'production') {
  // TODO
}


/**
 * Routes
 */

// serve index and view partials
app.get('/', routes.index);
app.post('/auth', routes.auth);
app.get('/exit', routes.exit);
app.get('/chat', routesChat.chat);
app.get('/partials/:name', routes.partials);
app.get('/rules', routes.rules);
app.get('/about', routes.about);

// JSON API
app.get('/api/version', routesApi.version);
app.get('/api/user-data', routesApi.userData);

// redirect all others to the index (HTML5 history)
app.get('*', routes.index);

// Socket.io Communication
io.sockets.on('connection', require('./routes/socket'));

/**
 * Start Server
 */

server.listen(app.get('port'), function () {
  console.log('Express server listening on port ' + app.get('port'));
});

function isBanned() {
  return function (req, res, next) {
    if (req.path.indexOf('/static') == 0) {
      next();
    }
    var ip;
    if (req.headers['x-nginx-proxy']) {
      ip = req.headers['x-real-ip'];
    } else {
      ip = req.connection.remoteAddress;
    }
    Banned.findOne({ip: ip}, function (err, banned) {
      if (err || !banned) {
        next();
      } else {
        if (banned.expires <= new Date(Date.now())) {
          banned.remove(function (err) {
            if (err) {
              console.error('[Middleware] Could not remove banned from db.');
            }
          });
          next();
        } else {
          res.render('banned', {expireDate: banned.expires.toDateString()});
        }
      }
    });
  };
}

function authenticate(appPages) {
  return function (req, res, next) {
    if (appPages.indexOf(req.path) == -1) {
      next();
    } else {
      if (!req.session.loggedIn) {
        res.redirect('/');
      } else {
        next();
      }
    }
  };
}

function determineEnv() {
  return function (req, res, next) {
    req.development = false;
    if (app.get('env') === 'development') {
      req.development = true;
    }
    next();
  };
}

{'fraction_non_alphanumeric': 0.20484901277584205, 'fraction_numerical': 0.0018873403019744484, 'mean_word_length': 3.2263803680981593, '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': '4058648', 'n_tokens_mistral': 2270, 'n_tokens_neox': 2254, 'n_words': 466}