-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
146 lines (120 loc) · 4.03 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* Module dependencies.
*/
var express = require('express'),
passport = require('passport'),
facebookStrategy = require('passport-facebook').Strategy,
appConfig = require('./config'),
api = require('./routes/api'),
routes_filters = require('./routes/routes_filters.js'),
services = require('./lib/services.js'),
rabbit = require('./lib/rabbit'),
connection = require('./db.js');
var app = module.exports = express();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.static(__dirname + '/public'));
app.use(express.session({ secret: appConfig.session_secret }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(function(req, res, next){
res.locals.user = req.session.user;
next();
});
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// CronJob
var cronJob = require('cron').CronJob;
new cronJob('*/10 * * * * *', function(){
rabbit.analyzegame();
}, null, true, null);
// Passport
passport.use(new facebookStrategy({
clientID: appConfig.fb.key,
clientSecret: appConfig.fb.secret,
callbackURL: appConfig.fb.callbackUrl
},
function(token, tokenSecret, profile, done) {
var sql = 'SELECT * FROM users WHERE fbId = ' + connection.escape(profile.id);
connection.query(sql, function(err, result) {
if (err) throw err;
if(result.length === 0) {
var sql = 'INSERT INTO users (name, fbId) VALUES (' + connection.escape(profile.displayName) +
', ' + connection.escape(profile.id) + ');';
connection.query(sql, function(err, result) {
if (err) throw err;
var sql = 'SELECT * FROM users WHERE fbId = ' + connection.escape(profile.id);
connection.query(sql, function(err, result) {
if (err) throw err;
done(null, { id: result[0].id, name: result[0].name, fbId: result[0].fbId, credits: result[0].credits });
});
});
} else {
done(null, { id: result[0].id, name: result[0].name, fbId: result[0].fbId, credits: result[0].credits });
}
});
}
));
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
var sql = 'SELECT * FROM users WHERE id = ' + connection.escape(id) + ' LIMIT 1;';
connection.query(sql, function(err, result) {
if (err) throw err;
if(result.length === 0) {
done(err, false);
} else {
done(null, { id: result[0].id, name: result[0].name, fbId: result[0].fbId, credits: result[0].credits });
}
});
});
// Passport Routes
app.get('/auth/facebook', passport.authenticate('facebook', {scope: 'email'}));
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { successRedirect: '/home',
failureRedirect: '/auth/facebook'})
);
app.get('/logout', function(req, res) {
req.logOut();
res.redirect('/');
});
// Routes
//app.get('/partials/:name', routes.partials);
app.get('/partials/:name', function(req, res) {
res.render('partials/' + req.params.name, {
user: req.user,
brand: 'SWABS'
});
});
// JSON API
app.get('/api/games', api.games);
app.post('/api/addgame', api.addgame);
app.post('/api/addbet', routes_filters.loggedInFilter, api.addbet);
// redirect all others to the index (HTML5 history)
app.get('*', function(req, res) {
res.render('index', {
user: req.user,
brand: 'SWABS'
});
});
services.getRabbitMqConnection(function(conn) {
if (conn) {
app.listen(3000, function(){
console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});
} else {
console.log("failed to connect to rabbitmq");
}
});