-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
147 lines (130 loc) · 4.13 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
147
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
var argv = require('minimist')(process.argv.slice(2));
argv['c'] = argv['c'] || false
if (argv['c'] && parseInt(argv['c']) <= numCPUs) {
numCPUs = argv['c'] || numCPUs
}
var fs = require('fs');
var localConfig = JSON.parse(fs.readFileSync('config.json', 'utf8'));
var database = localConfig.database;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function (worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died, restarting');
cluster.fork();
});
} else {
var express = require('express')
var app = express()
var storageInit = function () {
if (typeof global.mongoose == 'undefined') {
global.mongoose = mongoose = require('mongoose');
global.mongoose.connect('mongodb://localhost/' + database);
global.Tt = global.mongoose.model('Tt', {
id: {type: String, index: true},
data: Object,
date: { type: Date, default: Date.now }
});
}
}
var save = function (id, data) {
storageInit()
var Ti = global.Tt
var geo = new Ti({ id: id, data: data, date: global.timeNow });
geo.save(function (err) {
if (err) {
console.log(err);
}
console.log('Id saved ' + id);
});
}
var retrive = function (id) {
storageInit()
var Ti = global.Tt
var allEntries = new Array();
Ti.find({id: id}, function (err, entries) {
if (err) return res.send(err);
for (var x = 0; x < entries.length; x++) {
entry = {
id: entries[x].id,
data: entries[x].data,
date: entries[x].date
}
allEntries.push(entry);
}
global.res.send(allEntries)
}, function () {
global.res.send({})
})
}
var trong = function (id) {
return retrive(id);
}
var tring = function (id, async) {
var req = global.req;
var res = global.res;
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
// var ip = '122.161.114.15'
if (!async) {
//Declaring it twice for performance reasons
var geoip = require('geoip-lite');
var geo = geoip.lookup(ip);
setTimeout(function () {
console.log('Direct Lookup')
save(id, {
'geo': geo
})
}, 0);
return ({id: id, data: {geo: geo}, date: global.timeNow});
} else {
setTimeout(function () {
console.log('Delayed Lookup')
//Declaring it twice for performance reasons
var geoip = require('geoip-lite');
var geo = geoip.lookup(ip);
save(id, {
'geo': geo
})
}, 0);
}
return {};
}
app.get('/', function (req, res) {
res.send('Alive!')
})
var post = function (req, res) {
global.timeNow = new Date().toISOString();
global.req = req;
global.res = res;
var async = req.query['async'] == "false" ? false : true
tring(req.params["id"], async)
}
var get = function (req, res) {
global.req = req;
global.res = res;
trong(req.params["id"])
}
// JSONP Based
app.get('/set/:id', function (req, res) {
res.send(post(req, res));
})
app.get('/get/:id', function (req, res) {
get(req, res);
})
// REST Based
app.get('/geo/:id', function (req, res) {
get(req, res);
})
app.post('/geo/:id', function (req, res) {
res.send(post(req, res));
})
var server = app.listen(3000, function () {
var host = server.address().address
var port = server.address().port
console.log('App listening at http://%s:%s', host, port)
})
}