forked from dannegm/Mejorando.la-Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
128 lines (112 loc) · 3.71 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* Module dependencies.
*/
//Importamos express y require
var express = require('express'),
request = require('request'),
//toobusy = require('toobusy'),
// Database Mongodb
mongoose = require('mongoose'),
// Autenticacion Facebook Twitter
passport = require('passport'),
TwitterStrategy = require('passport-twitter').Strategy,
FacebookStrategy = require('passport-facebook').Strategy;
//Mongoose para express
require('express-mongoose');
require('datejs/lib/date-es-ES');
module.exports = function (config) {
/**
* Configuracion de la database
*/
mongoose.connect('mongodb://localhost/' + config.db.name );
// modelos
require('./models/user');
require('./models/message');
require('./models/feedback');
require('./models/report');
/**
* Inicio Configuracion Auth0
*/
var auth = require('./auth'),
sche = config.loginsecure ? 'https' : 'http';
passport.use(new TwitterStrategy({
consumerKey: config.twitter.consumerKey,
consumerSecret: config.twitter.consumerSecret,
callbackURL: sche+"://"+config.host+"/auth/twitter/callback"
}, auth.twitter));
passport.use(new FacebookStrategy({
clientID: config.facebook.appId,
clientSecret: config.facebook.appSecret,
callbackURL: sche+"://"+config.host+"/auth/facebook/callback"
}, auth.facebook));
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(auth.user);
/**
* Fin Configuracion Auth0
*/
/*
* Configuracion de sesion
*/
var sessionStore = new (require('connect-mongo')(express))({ db: config.db.name });
/*
* Configuracion de la app
*/
var app = express();
app.configure(function(){
app.set('port', config.port);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
/*app.use(function (req, res, next) {
if(toobusy()) res.status(500).sendfile(__dirname+'/views/overload.html');
else next();
});*/
//Directorio estatico y favicon
app.use(express.static(__dirname + '/public'));
app.use(express.favicon(__dirname + '/public/images/favicon.ico'));
//Peticiones POST para Express
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser(config.cookie.secret));
app.use(express.session({secret: config.session.secret, key: config.session.key, store: sessionStore }));
//Importamos passport para autenticacion
app.use(passport.initialize());
app.use(passport.session());
//Establece apartir de la ip su localizacion y la guarda en la informacion del usuario
app.use(function (req, res, next) {
if(req.user && !req.user.ip) {
request({ url: 'https://mejorando.la/locateme?ip='+req.ip },
function (err, response, body) {
if(err) return next();
req.user.ip = req.ip;
req.user.pais = body;
req.user.save();
next();
});
} else {
next();
}
});
//Importa router de la app
app.use(app.router);
});
//Permite que varios procesos de nodejs compartan la conexion tcp
app.configure('production', function () {
app.use(require('raven').middleware.express(config.sentry_dsn));
app.use(function (err, req, res, next) {
res.status(500).sendfile(__dirname+'/views/500.html');
});
});
//Permite establecer parametros de configuracion para entorno de desarrollo y produccion
app.configure('development', function(){
app.use(express.logger('dev'));
app.use(express.errorHandler());
});
/*
* Routes
*/
require('./routes')(app, passport);
// Retorna la app y los datos de la sesion
return { app: app, sessionStore: sessionStore };
};