<commit_before>//
/**
 * Module dependencies.
 */
var express = require('express'),
    mongoose = require('mongoose'),
    MongoStore  = require('connect-mongo')(express),
    async = require('async'),
    utils = require('./utils'),
    auth = require("connect-auth");

var app = module.exports = express();


app.configure('development', function(){
    app.set('views', __dirname + '/deliver/views');
    app.set('old_views', __dirname + '/views');
    app.set('public_folder', __dirname + '/deliver/public');
    app.set('public_folder2', __dirname + '/public');
    app.set('port',80);

    app.set('facebook_app_id', '175023072601087');
    app.set('facebook_app_name','uru_dev');
    app.set('facebook_secret', '<KEY>');

    app.set('show_only_published',false);

    app.set('sendgrid_user','<EMAIL>');
    app.set('sendgrid_key','<KEY>');
    app.set('system_email','<EMAIL>');
    app.set('root_path', 'http://dev.empeeric.com');
    app.set('DB_URL','mongodb://localhost/uru');
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
    require('./tools/compile_templates');
    require('./deliver/tools/compile_dust_templates');

    // TODO REMOVE THIS BEFORE COMMIT
    //app.set('send_mails',true);

});


app.configure('avner_env', function(){
    app.set('views', __dirname + '/deliver/views');
    app.set('old_views', __dirname + '/views');
    app.set('public_folder', __dirname + '/deliver/public');
    app.set('public_folder2', __dirname + '/public');
    app.set('port',3000);
    app.set('facebook_app_id', '175023072601087');
    app.set('facebook_app_name','uru_dev');
    app.set('facebook_secret', '<KEY>');

    app.set('show_only_published',false);

//    app.set('facebook_app_id', '436675376363069');
//    app.set('facebook_app_name','uru_staging');
//    app.set('facebook_secret', '<KEY>');

    app.set('sendgrid_user','<EMAIL>');
    app.set('sendgrid_key','<KEY>');
    app.set('system_email','<EMAIL>');
    app.set('root_path', 'http://dev.empeeric.com');
    app.set('DB_URL','mongodb://localhost/uru');
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
    require('./tools/compile_templates');
    require('./deliver/tools/compile_dust_templates');
});



app.configure('staging', function(){
    app.set('views', __dirname + '/deliver/views');
    app.set('old_views', __dirname + '/views');
    app.set('public_folder', __dirname + '/deliver/public');
    app.set('public_folder2', __dirname + '/public');
    app.set("port", process.env.PORT);
    app.set('facebook_app_id', '436675376363069');
    app.set('facebook_app_name','uru_staging');
    app.set('facebook_secret', '<KEY>');

    app.set('show_only_published',false);

    app.set('sendgrid_user',process.env.SENDGRID_USERNAME || 'app<EMAIL>');
    app.set('sendgrid_key',process.env.SENDGRID_PASSWORD || '<KEY>');
    app.set('system_email','<EMAIL>');
    app.set('root_path', 'http://uru-staging.herokuapp.com');
    app.set('DB_URL',process.env.MONGOLAB_URI);
    app.use(express.errorHandler());
    require('j-forms').setAmazonCredentials({
        key: '<KEY>',
        secret: '<KEY>',
        bucket: 'uru'
    });
    require('./deliver/tools/compile_dust_templates');
    require('./tools/compile_templates');

    app.set('send_mails',true);

});

app.configure('production', function(){
    app.set('views', __dirname + '/deliver/views');
    app.set('old_views', __dirname + '/views');
    app.set('public_folder', __dirname + '/deliver/public');
    app.set('public_folder2', __dirname + '/public');
    app.set("port", process.env.PORT);
    app.set('facebook_app_id', '375874372423704');
    app.set('facebook_app_name','uru_heroku');
    app.set('facebook_secret', '<KEY>');

    app.set('show_only_published',true);

    app.set('sendgrid_user',process.env.SENDGRID_USERNAME || '<EMAIL>');
    app.set('sendgrid_key',process.env.SENDGRID_PASSWORD || '<KEY>');
    app.set('system_email','<EMAIL>');
    app.set('root_path', 'http://www.uru.org.il');
    app.set('DB_URL',process.env.MONGOLAB_URI);
    app.use(express.errorHandler());
    require('j-forms').setAmazonCredentials({
        key: '<KEY>',
        secret: '<KEY>',
        bucket: 'uru'
    });
    require('./deliver/tools/compile_dust_templates');
    require('./tools/compile_templates');

    app.set('send_mails',true);

    process.on('uncaughtException', function(err) {
        console.trace(err);
    });

});


if(!mongoose.connection.host)
    mongoose.connect(app.settings.DB_URL);

var models = require('./models');
models.setDefaultPublish(app.settings.show_only_published);

var account = require('./deliver/routes/account');

// Configuration
var confdb = {
    db: utils.split_db_url(app.settings.DB_URL),
    secret: '076ed61d63ea10a12rea879l1ve433s9'
};

app.configure(function(){
    utils.setShowOnlyPublished(app.settings.show_only_published);

    app.set('view engine', 'jade');

    app.use(express.static(app.settings.public_folder));
    if(app.settings.public_folder2)
        app.use(express.static(app.settings.public_folder2));
    require('j-forms').serve_static(app,express);

    app.use(function(req,res,next) {
        var agent = req.header('User-Agent');
        if(/facebook/.test(agent) || req.query['debug_fb_bot']) {
            require('./deliver/routes/fb_bot')(req,res,next);
        }
        else
            next()
    });

    // pause req stream in case we're uploading files
    app.use(function(req,res,next) {
        if(req.xhr && /api\/(avatar|image_upload)\/?$/.test(req.path)) {
            req.queueStream = new utils.queueStream(req);
            req.queueStream.pause();
            req.pause();
            console.log('request paused');
        }
        next();
    });

    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(express.cookieParser());

    app.set('view options', { layout: false });

    app.use(express.session({secret: confdb.secret,
        maxAge: new Date(Date.now() + (3600 * 1000 * 24)),
        store: new MongoStore(confdb.db) }));
    app.use(account.referred_by_middleware);

    var fbId = app.settings.facebook_app_id,
        fbSecret = app.settings.facebook_secret,
        fbCallbackAddress = app.settings.root_path + '/account/facebooklogin';

    app.use(auth({strategies: [
        account.SimpleAuthentication()
        ,account.FbServerAuthentication()
        ,auth.Facebook({
            appId : fbId,
            appSecret: fbSecret,
            callback: fbCallbackAddress,
            scope: 'email,publish_actions',
            failedUri: '/noauth'
        })
    ],
    trace: true,
    logoutHandler: require("connect-auth/lib/events").redirectOnLogout("/")}));

    app.use(account.auth_middleware);<commit_msg>#388 reconnect on disconnect, reinit session middleware<commit_after>//
/**
 * Module dependencies.
 */
var express = require('express'),
    mongoose = require('mongoose'),
    MongoStore  = require('connect-mongo')(express),
    async = require('async'),
    utils = require('./utils'),
    auth = require("connect-auth");

var app = module.exports = express();


app.configure('development', function(){
    app.set('views', __dirname + '/deliver/views');
    app.set('old_views', __dirname + '/views');
    app.set('public_folder', __dirname + '/deliver/public');
    app.set('public_folder2', __dirname + '/public');
    app.set('port',80);

    app.set('facebook_app_id', '175023072601087');
    app.set('facebook_app_name','uru_dev');
    app.set('facebook_secret', '<KEY>');

    app.set('show_only_published',false);

    app.set('sendgrid_user','<EMAIL>');
    app.set('sendgrid_key','<KEY>');
    app.set('system_email','<EMAIL>');
    app.set('root_path', 'http://dev.empeeric.com');
    app.set('DB_URL','mongodb://localhost/uru');
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
    require('./tools/compile_templates');
    require('./deliver/tools/compile_dust_templates');

    // TODO REMOVE THIS BEFORE COMMIT
    //app.set('send_mails',true);

});


app.configure('avner_env', function(){
    app.set('views', __dirname + '/deliver/views');
    app.set('old_views', __dirname + '/views');
    app.set('public_folder', __dirname + '/deliver/public');
    app.set('public_folder2', __dirname + '/public');
    app.set('port',3000);
    app.set('facebook_app_id', '175023072601087');
    app.set('facebook_app_name','uru_dev');
    app.set('facebook_secret', '<KEY>');

    app.set('show_only_published',false);

//    app.set('facebook_app_id', '436675376363069');
//    app.set('facebook_app_name','uru_staging');
//    app.set('facebook_secret', '<KEY>');

    app.set('sendgrid_user','<EMAIL>');
    app.set('sendgrid_key','<KEY>');
    app.set('system_email','<EMAIL>');
    app.set('root_path', 'http://dev.empeeric.com');
    app.set('DB_URL','mongodb://localhost/uru');
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
    require('./tools/compile_templates');
    require('./deliver/tools/compile_dust_templates');
});



app.configure('staging', function(){
    app.set('views', __dirname + '/deliver/views');
    app.set('old_views', __dirname + '/views');
    app.set('public_folder', __dirname + '/deliver/public');
    app.set('public_folder2', __dirname + '/public');
    app.set("port", process.env.PORT);
    app.set('facebook_app_id', '436675376363069');
    app.set('facebook_app_name','uru_staging');
    app.set('facebook_secret', '<KEY>');

    app.set('show_only_published',false);

    app.set('sendgrid_user',process.env.SENDGRID_USERNAME || '<EMAIL>');
    app.set('sendgrid_key',process.env.SENDGRID_PASSWORD || '<KEY>');
    app.set('system_email','<EMAIL>');
    app.set('root_path', 'http://uru-staging.herokuapp.com');
    app.set('DB_URL',process.env.MONGOLAB_URI);
    app.use(express.errorHandler());
    require('j-forms').setAmazonCredentials({
        key: '<KEY>',
        secret: '<KEY>',
        bucket: 'uru'
    });
    require('./deliver/tools/compile_dust_templates');
    require('./tools/compile_templates');

    app.set('send_mails',true);

});

app.configure('production', function(){
    app.set('views', __dirname + '/deliver/views');
    app.set('old_views', __dirname + '/views');
    app.set('public_folder', __dirname + '/deliver/public');
    app.set('public_folder2', __dirname + '/public');
    app.set("port", process.env.PORT);
    app.set('facebook_app_id', '375874372423704');
    app.set('facebook_app_name','uru_heroku');
    app.set('facebook_secret', '<KEY>');

    app.set('show_only_published',true);

    app.set('sendgrid_user',process.env.SENDGRID_USERNAME || '<EMAIL>');
    app.set('sendgrid_key',process.env.SENDGRID_PASSWORD || '<KEY>');
    app.set('system_email','<EMAIL>');
    app.set('root_path', 'http://www.uru.org.il');
    app.set('DB_URL',process.env.MONGOLAB_URI);
    app.use(express.errorHandler());
    require('j-forms').setAmazonCredentials({
        key: '<KEY>',
        secret: '<KEY>',
        bucket: 'uru'
    });
    require('./deliver/tools/compile_dust_templates');
    require('./tools/compile_templates');

    app.set('send_mails',true);

    process.on('uncaughtException', function(err) {
        console.trace(err);
    });

});


if(!mongoose.connection.host)
    mongoose.connect(app.settings.DB_URL);

mongoose.connection.on('error',function(err) {
    console.error('db connection error: ',err);
});

mongoose.connection.on('disconnected',function(err){
    console.error('DB disconnected',err);
    var reconnect = function(){
        mongoose.connect(app.settings.DB_URL,function(err) {
            if(err)
                console.error(err);
            else {
                session_middleware = express.session({secret: confdb.secret,
                    maxAge: new Date(Date.now() + (3600 * 1000 * 24)),
                    store: new MongoStore(confdb.db) });
            }
        });
    };
    setTimeout(reconnect,200);
});

var models = require('./models');
models.setDefaultPublish(app.settings.show_only_published);

var account = require('./deliver/routes/account');

// Configuration
var confdb = {
    db: utils.split_db_url(app.settings.DB_URL),
    secret: '076ed61d63ea10a12rea879l1ve433s9',
    auto_reconnect:true
};

var session_middleware;

app.configure(function(){
    utils.setShowOnlyPublished(app.settings.show_only_published);

    app.set('view engine', 'jade');

    app.use(express.static(app.settings.public_folder));
    if(app.settings.public_folder2)
        app.use(express.static(app.settings.public_folder2));
    require('j-forms').serve_static(app,express);

    app.use(function(req,res,next) {
        var agent = req.header('User-Agent');
        if(/facebook/.test(agent) || req.query['debug_fb_bot']) {
            require('./deliver/routes/fb_bot')(req,res,next);
        }
        else
            next()
    });

    // pause req stream in case we're uploading files
    app.use(function(req,res,next) {
        if(req.xhr && /api\/(avatar|image_upload)\/?$/.test(req.path)) {
            req.queueStream = new utils.queueStream(req);
            req.queueStream.pause();
            req.pause();
            console.log('request paused');
        }
        next();
    });

    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(express.cookieParser());

    app.set('view options', { layout: false });

    session_middleware = express.session({secret: confdb.secret,
        maxAge: new Date(Date.now() + (3600 * 1000 * 24)),
        store: new MongoStore(confdb.db) });

    app.use(function(req,res,next) {
        session_middleware(req,res,next);
    });

    app.use(account.referred_by_middleware);

    var fbId = app.settings.facebook_app_id,
        fbSecret = app.settings.facebook_secret,
        fbCallbackAddress = app.settings.root_path + '/account/facebooklogin';

    app.use(auth({strategies: [
        account.SimpleAuthentication()
        ,account.FbServerAuthentication()
        ,auth.Facebook({
            appId : fbId,
            appSecret: fbSecret,
            callback: fbCallbackAddress,
            scope: 'email,publish_actions',
            failedUri: '/noauth'
        })
    ],
    trace: true,
    logoutHandler: require("connect-auth/lib/events").redirectOnLogout("/")}));

    app.use(account.auth_middleware);
{'fraction_non_alphanumeric': 0.18816346353985985, 'fraction_numerical': 0.017206688406299867, 'mean_word_length': 3.580235144582142, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '4160862', 'n_tokens_mistral': 4901, 'n_tokens_neox': 4679, 'n_words': 605}