stuppid_bot

Untitled

Aug 14th, 2013
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Module dependencies.
  3.  */
  4. exports.server = function(config) {
  5.  
  6.     var port = config && config.hasOwnProperty('port') ? config.port : 3007;
  7.     var dbName = config && config.hasOwnProperty('dbName') ? config.dbName : 'full';
  8.     var sessionDb = config && config.hasOwnProperty('sessionDb') ? config.sessionDb : 'adminSession';
  9.     var dbHost = config && config.hasOwnProperty('dbHost') ? config.dbHost : 'mongodb://localhost/';
  10.  
  11.     var express = require('express');
  12.  
  13.     //database connection
  14.     var mongoose = require('mongoose');
  15.  
  16.     require("./app.gen/schema/metadataIndex");
  17.  
  18.     var __ = require('underscore');
  19.     __.mixin(require('underscore.string'));
  20.  
  21.     app = module.exports = express.createServer();
  22.     mongoStore = require('connect-mongo');
  23.     // Configuration
  24.     app.configure(function() {
  25.         // app.use(express.logger({ format: '\x1b[32m:method\x1b[0m \x1b[33m:url\x1b[0m :response-time ms' }))
  26.         app.set('views', __dirname + '/app');
  27.         app.set('view engine', 'ejs');
  28.  
  29.         app.register('.js', require('ejs'));
  30.         app.register('.html', require('ejs'));
  31.  
  32.         app.use(express.bodyParser());
  33.         app.use(express.methodOverride());
  34.         app.use(express.cookieParser());
  35.         app.use(express.session({
  36.             secret: 'your secret here',
  37.             store: new mongoStore({
  38.                 db: sessionDb
  39.             }),
  40.             cookie: {
  41.                 maxAge: 1 * 24 * 3600 * 1000
  42.             }
  43.         }));
  44.         app.use(function(req, res, next) {
  45.             var date = new Date();
  46.             if (!req.session.user_id) {
  47.                 if (req.session.last_logout_time) {
  48.                     console.log("*", "\x1b[32m" + 'last_logout_time', ":", req.session.last_logout_time, "\x1b[0m");
  49.                 } else {
  50.                     console.log("*", "\x1b[31m" + "Session user_id was lost by undefined reason!" + "\x1b[0m");
  51.                 }
  52.             }
  53.  
  54.             console.log(date.toGMTString() + " [user_id=" + req.session.user_id + "]:", "\x1b[32m" + req.method, "\x1b[33m" + req.url + "\x1b[0m");
  55.  
  56.             next();
  57.         });
  58.         app.use(app.router);
  59.         app.use(express.static(__dirname + '/public'));
  60.         app.set('view options', {
  61.             layout: false
  62.         });
  63.     });
  64.  
  65.     process.on('uncaughtException', function(err) {
  66.         console.log('-----|SERVER ERROR|---------------------------------------');
  67.         console.log('Exception: ' + err);
  68.         console.log('Stack', err.stack);
  69.         console.log('----------------------------------------------------------');
  70.     });
  71.  
  72.     app.configure('development', function() {
  73.         app.use(express.errorHandler({
  74.             dumpExceptions: true,
  75.             showStack: true
  76.         }));
  77.     });
  78.  
  79.     app.configure('production', function() {
  80.         app.use(express.errorHandler());
  81.     });
  82.  
  83.     // h2e4
  84.     var h2e4 = require(__dirname + '/lib/h2e4.js');
  85.  
  86.     h2e4.init(app, 'Application.html', function(req, res, next) {
  87.         console.log('* Username: ' + req.session.user);
  88.         console.log('* Profile: ' + req.session.profile);
  89.         if (req.session.user_id && req.session.profile) {
  90.             req.session.last_logout_time = undefined;
  91.             next();
  92.         } else {
  93.             res.redirect('/logout');
  94.         }
  95.     });
  96.  
  97.     // Routes
  98.     app.get('/login', function(req, res) {
  99.         res.render('Login.html');
  100.     });
  101.  
  102.     app.get('/logout', function(req, res) {
  103.         console.log('logout');
  104.         delete req.session.user_id;
  105.         delete req.session.profile;
  106.         delete req.session.user;
  107.  
  108.         req.session.last_logout_time = (new Date()).toGMTString();
  109.  
  110.         res.redirect('/login');
  111.     });
  112.  
  113.     var twoStep = require('twostep');
  114.     RelationCache = {};
  115.     ThingsChilds = {};
  116.     ThingsParents = {};
  117.     ThingsForQB = {};
  118.     ThingsSourceTree = {};
  119.     ThingsCollections = {};
  120.     $prop_etalon_4_codegen = null;
  121.  
  122.     twoStep(
  123.  
  124.     function initDb() {
  125.         this.pass(mongoose.createConnection('mongodb://localhost/full'));
  126.     }, twoStep.throwIfError(function InitInheritanceTree(err, db) {
  127.         this.pass(db);
  128.         var cb = this.slot();
  129.         console.log('Inheritance Tree ... ');
  130.         require('./lib/inheritanceTree').getInheritanceTree(db, function(err, cacheChilds, cacheParents, sourceTree, collections) {
  131.             ThingsChilds = cacheChilds;
  132.             ThingsParents = cacheParents;
  133.             ThingsSourceTree = sourceTree;
  134.             ThingsCollections = collections;
  135.             console.log('loaded...');
  136.             cb();
  137.         });
  138.     }), twoStep.throwIfError(function InitThingForQB(err, db) {
  139.         this.pass(db);
  140.         var cb = this.slot();
  141.         require('./lib/thingsForQBCache').getCachedThingForQB(db, function(err, cache) {
  142.             ThingsForQB = cache;
  143.             console.log('things for QB Cache loaded...');
  144.             cb();
  145.  
  146.         });
  147.     }), twoStep.throwIfError(function InitRelationCache(err, db) {
  148.         this.pass(db);
  149.         var cb = this.slot();
  150.         require('./lib/relationCache').getCachedRelations(db, function(err, cache) {
  151.             RelationCache = cache;
  152.             console.log('Relations Cache loaded...');
  153.             cb();
  154.         });
  155.     }), twoStep.throwIfError(function(err, db) {
  156.         db.close();
  157.         if (!err) {
  158.             app.listen(port);
  159.             console.log("Express server listening on port %d in %s mode", port, app.settings.env);
  160.         } else throw err;
  161.     }));
  162. };
  163. // var trycatch = require('trycatch');
  164. // trycatch(function init() {
  165. //     },
  166. //     function onerror(err) {
  167. //         debugger
  168. //         console.log("This is a scoped error handler!\n", err.stack);
  169. //     }
  170. // );
Advertisement
Add Comment
Please, Sign In to add comment