forked from tracking-exposed/facebook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
190 lines (172 loc) · 6.99 KB
/
app.js
File metadata and controls
190 lines (172 loc) · 6.99 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
var express = require('express');
var app = express();
var server = require('http').Server(app);
var _ = require('lodash');
var io = require('socket.io')(server);
var moment = require('moment');
var bodyParser = require('body-parser');
var Promise = require('bluebird');
var mongodb = Promise.promisifyAll(require('mongodb'));
var debug = require('debug')('fbtrex');
var nconf = require('nconf');
var jade = require('jade');
var utils = require('./lib/utils');
var escviAPI = require('./lib/allversions');
var cfgFile = "config/settings.json";
var redOn = "\033[31m";
var redOff = "\033[0m";
nconf.argv()
.env()
.file({ file: cfgFile });
console.log(redOn + "ઉ nconf loaded, using " + cfgFile + redOff);
var wrapError = function(where, v, fn, nfo) {
var str = redOn + " " + where + " Developer mistake v(" +
v + ") " + fn + "\n" +
JSON.stringify(nfo, undefined, 2) + redOff;
console.log(str);
};
/* This function wraps all the API call, checking the verionNumber
* managing error in 4XX/5XX messages and making all these asyncronous
* I/O with DB, inside this Bluebird */
var dispatchPromise = function(funcName, req, res) {
var apiV = _.parseInt(_.get(req.params, 'version'));
if(_.isNaN(apiV) || (apiV).constructor !== Number )
apiV = escviAPI.lastVersion;
var func = _.get(escviAPI.implementations['version' + apiV], funcName);
if(_.isUndefined(func)) {
debug("Developer mistake with version %d %s ", apiV, funcName);
wrapError("pre-exec", apiV, funcName, req.params, res);
res.header(500);
return false;
}
req.randomUnicode = String.fromCharCode(_.random(0x0391, 0x085e));
debug("%s %s Dispatching request to %s",
req.randomUnicode, moment().format("HH:mm:ss"), req.url);
/* in theory here we can keep track of time */
return new Promise.resolve(func(req))
.then(function(httpresult) {
if(!_.isUndefined(httpresult.json)) {
debug("%s API %s success, returning JSON (%d bytes)",
req.randomUnicode, funcName,
_.size(JSON.stringify(httpresult.json)) );
res.json(httpresult.json)
} else if(!_.isUndefined(httpresult.text)) {
debug("%s API %s success, returning text (size %d)",
req.randomUnicode, funcName, _.size(httpresult.text));
res.send(httpresult.text)
} else if(!_.isUndefined(httpresult.file)) {
/* this is used for special files, beside the css/js below */
debug("%s API %s success, returning file (%s)",
req.randomUnicode, funcName, httpresult.file);
res.sendFile(__dirname + "/html/" + httpresult.file);
} else {
wrapError("post-exec", apiV, funcName, req.params);
res.header(500);
return false;
}
return true;
})
};
/* everything begin here, welcome */
server.listen(nconf.get('port'));
console.log(" Port " + nconf.get('port') + " listening");
/* configuration of express4 */
app.use(bodyParser.json({limit: '3mb'}));
app.use(bodyParser.urlencoded({limit: '3mb', extended: true}));
app.get('/node/info/:version', function(req, res) {
return dispatchPromise('nodeInfo', req, res);
});
app.get('/node/export/:version/:shard', function(req, res) {
return dispatchPromise('nodeExport', req, res);
});
app.get('/node/activity/:version/:format', function(req, res) {
return dispatchPromise('byDayActivity', req, res);
});
app.get('/node/countries/:version/:format', function(req, res) {
return dispatchPromise('countriesStats', req, res);
});
app.get('/node/country/:version/:countryCode/:format', function(req, res) {
return dispatchPromise('countryStatsByDay', req, res);
});
app.get('/post/top/:version', function(req, res) {
return dispatchPromise('topPosts', req, res);
});
app.get('/post/reality/:version/:postId', function(req, res) {
return dispatchPromise('postReality', req, res);
});
app.get('/post/perceived/:version/:postId/:userId', function(req, res){
return dispatchPromise('postLife', req, res);
});
app.get('/user/:version/timeline/:userId/:past/:R/:P', function(req, res) {
return dispatchPromise('userTimeLine', req, res);
});
app.get('/user/:version/analysis/:kind/:cpn/:userId/:format', function(req, res) {
return dispatchPromise('userAnalysis', req, res);
});
app.post('/F/:version', function(req, res) {
return dispatchPromise('postFeed', req, res);
});
app.post('/D/:version', function(req, res) {
return dispatchPromise('postDebug', req, res);
});
app.post('/contrib/:version/:which', function(req, res) {
return dispatchPromise('writeContrib', req, res);
});
/* Only the *last version* is imply in the API below */
app.get('/', function(req, res) {
return dispatchPromise('getIndex', req, res);
});
app.get('/page-:name', function(req, res) {
return dispatchPromise('getPage', req, res);
});
app.get('/realitycheck/random', function(req, res) {
return dispatchPromise('getRandom', req, res);
});
app.get('/realitycheck/:userId', function(req, res) {
return dispatchPromise('getPersonal', req, res);
});
app.get('/overseer', function(req, res) {
return dispatchPromise('getOverseer', req, res);
});
app.get('/realitymeter', function(req, res) {
return dispatchPromise('getRealityMeter', req, res);
});
app.get('/realitymeter/:postId', function(req, res) {
return dispatchPromise('getRealityMeter', req, res);
});
app.get('/impact', function(req, res) {
return dispatchPromise('getImpact', req, res);
});
/* static files, independent by the API versioning */
app.get('/favicon.ico', function(req, res) {
res.sendFile(__dirname + '/dist/favicon.ico');
});
app.get('/facebook.tracking.exposed.user.js', function (req, res) {
var ipaddr = _.get(req.headers, "x-forwarded-for") || "127.0.0.1";
debug("ScriptLastVersion requested in %j", utils.getGeoIP(ipaddr));
res.sendFile(__dirname + '/scriptlastversion');
});
app.use('/css', express.static(__dirname + '/dist/css'));
app.use('/images', express.static(__dirname + '/dist/images'));
app.use('/lib/font/league-gothic', express.static(__dirname + '/dist/css'));
app.use('/js/vendor', express.static(__dirname + '/dist/js/vendor'));
/* development: the local JS are pick w/out "npm run build" every time */
if(nconf.get('development') === 'true') {
console.log(redOn + "ઉ DEVELOPMENT = serving JS from src" + redOff);
app.use('/js/local', express.static(__dirname + '/sections/webscripts'));
} else {
app.use('/js/local', express.static(__dirname + '/dist/js/local'));
}
/* websocket configuration and definition of the routes */
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
debug("socket.io 'my other event': %j", data);
return { some: true };
});
socket.on('AAA', function(stuff) {
debug("My AAA");
console.log(JSON.stringify(stuff, undefined, 2));
return { some: false };
});
});