-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
120 lines (103 loc) · 3.58 KB
/
server.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
/**
* Module dependencies.
*/
var express = require('express');
var jsonreq = require('jsonreq');
var Facebook = require('./facebook');
var mongo = require('mongodb');
var server = new mongo.Server("127.0.0.1", 27017, {});
var app = module.exports = express.createServer();
var fb = new Facebook({appId:'124829930933291',
key:'c2f33b41ee0f6d7dc3b65ba70b92ef6e',
secret:'9e39d0ddc888b9e08c418debf14cf3b4'
});
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.methodOverride());
app.use(express.session({secret:'bitcoinpriceonfacebook'}));
app.use(express.static(__dirname + '/public'));
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.set('db-name', 'btcfb-devel');
});
app.configure('production', function(){
app.use(express.errorHandler());
app.set('db-name', 'btcfb-production');
});
var db = new mongo.Db(app.set('db-name'), server, {});
var checkUser = function(req, res, next){
if (req.session.fbInfo){
req.session.recargas++;
} else {
console.log('Nuevo Visitante');
req.session.recargas = 1;
}
next();
}
// Routes
app.get('/', checkUser, function(req, res){
var locals = {};
// Traemos los valores de bitcoincharts.com
jsonreq.get('http://bitcoincharts.com/t/weighted_prices.json', function(err, data) {
// Inicializamos los locales a pasar a la vista
locals.dolares = {valor: data.USD['24h']};
locals.euros = {valor: data.EUR['24h']};
locals.libras = {valor: data.GBP['24h']};
db.open(function(err,client){
if (err) throw err;
// Iniciamos la guardada de la informacion.
db.collection('precios', function(err, precios){
var timestamp = new Date();
precios.insert({time:timestamp, precios:data}, {safe:true},
function(err,objs){if (err) console.warn(err.message)});
});
// Revisamos la tendencia con el nuevo precio
db.collection('precios', function (err, precios) {
var ultimos = precios.find({}, {sort:{time:-1}});
var nuevo = {};
ultimos.nextObject(function(err,doc){
nuevo = doc;
});
ultimos.nextObject(function(err,doc){
var checkTendencia = function (nuevo, viejo) {
var ret;
if (nuevo === viejo) ret = '=';
if (nuevo > viejo) ret = '↑';
if (nuevo < viejo) ret = '↓';
return ret;
}
locals.dolares.tendencia = checkTendencia(nuevo.precios.USD['24h'], doc.precios.USD['24h']);
locals.euros.tendencia = checkTendencia(nuevo.precios.EUR['24h'], doc.precios.EUR['24h']);
locals.libras.tendencia = checkTendencia(nuevo.precios.GBP['24h'], doc.precios.GBP['24h']);
res.render('index', {
title:'Bitcoin Price on Facebook',
locals: locals
});
});
});
db.close();
});
});
});
app.post('/', function(req, res){
if (req.body.signed_request){
// Es una peticion desde facebook
var mensaje = fb.request(req.body.signed_request);
console.log(mensaje);
if (mensaje.user_id){
req.session.fbInfo = mensaje;
console.log('El usuario '+mensaje.user_id+' ha autorizado la app');
res.redirect('/');
} else {
fb.auth_dialog(res,'http://apps.facebook.com/bitcoin_price/');
}
}
});
app.listen(3000);
console.log("Express server listening on port %d", app.address().port);