Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 41 additions & 44 deletions appengine/mongodb/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,57 +39,54 @@ if (nconf.get('mongoDatabase')) {
uri = `${uri}/${nconf.get('mongoDatabase')}`;
}

mongodb.MongoClient.connect(
uri,
(err, client) => {
if (err) {
throw err;
}
mongodb.MongoClient.connect(uri, (err, client) => {
if (err) {
throw err;
}

// Create a simple little server.
http
.createServer((req, res) => {
if (req.url === '/_ah/health') {
res.writeHead(200, {
'Content-Type': 'text/plain',
});
res.write('OK');
res.end();
return;
}
// Track every IP that has visited this site
const db = client.db(nconf.get('mongoDatabase'));
const collection = db.collection('IPs');
// Create a simple little server.
http
.createServer((req, res) => {
if (req.url === '/_ah/health') {
res.writeHead(200, {
'Content-Type': 'text/plain',
});
res.write('OK');
res.end();
return;
}
// Track every IP that has visited this site
const db = client.db(nconf.get('mongoDatabase'));
const collection = db.collection('IPs');

const ip = {
address: req.connection.remoteAddress,
};
const ip = {
address: req.connection.remoteAddress,
};

collection.insert(ip, err => {
collection.insert(ip, err => {
if (err) {
throw err;
}

// push out a range
let iplist = '';
collection.find().toArray((err, data) => {
if (err) {
throw err;
}
data.forEach(ip => {
iplist += `${ip.address}; `;
});

// push out a range
let iplist = '';
collection.find().toArray((err, data) => {
if (err) {
throw err;
}
data.forEach(ip => {
iplist += `${ip.address}; `;
});

res.writeHead(200, {
'Content-Type': 'text/plain',
});
res.write('IPs:\n');
res.end(iplist);
res.writeHead(200, {
'Content-Type': 'text/plain',
});
res.write('IPs:\n');
res.end(iplist);
});
})
.listen(process.env.PORT || 8080, () => {
console.log('started web process');
});
}
);
})
.listen(process.env.PORT || 8080, () => {
console.log('started web process');
});
});