Skip to content

Instantly share code, notes, and snippets.

@adrianseeley
Created November 24, 2012 02:55
Show Gist options
  • Save adrianseeley/4138182 to your computer and use it in GitHub Desktop.
Save adrianseeley/4138182 to your computer and use it in GitHub Desktop.
Let The Ghosts In (www.lettheghostsin.com) :: a NodeJS Snippit Containing The Current LTGI Source
var fs = require("fs");
var db_url = 'lettheghostsin';
var db = require('mongojs').connect(db_url, ['wisdom']);
var http = require('http');
var url = require('url');
var html_header = '<!doctype html><html><head><meta charset="UTF-8"><title>Let the Ghosts in</title><link href="http://fonts.googleapis.com/css?family=Abel" rel="stylesheet" type="text/css"><style rel="stylesheet" type="text/css">html {margin: 0; padding: 0 10%;}body {font-family: "Abel", Helvetica, sans-serif; font-size: 18px; text-align: center; background-color: #0B0B0B; color: #FFF;}header {margin-top: 5em;}form { border-bottom: 1px solid #999; padding-bottom: 2em;}input { width: 80%; margin: 3em auto 1em; font-size: 1.2em; padding: 0.5em; display: block; }button {width: 40%; padding: 0.5em; background-color: #575757; border: none; color: #fff; font-size: 1.2em; cursor: pointer; }button:hover { background-color: #969696;}.result { width: 63%; margin: 1em auto; padding: 0.5em 40px; border: 2px solid #262626; border-radius: 4px; position: relative; }.result img { width: 28px; height: 28px; background-color: #575757; position: absolute; top: 10%; right: 8px; cursor: pointer;}.result img:hover {background-color: #969696;}.result img:last-child {top: 55%;}footer {border-top: 1px solid #999; font-size: 0.7em;}a:link{color:#6f6e6a; text-decoration:none; font-size: 16px;} a:visited {color:#6f6e6a; text-decoration:none;font-size: 16px;} a:hover {color:#ada99f; text-decoration:none;font-size: 16px;} a:active {color:#ada99f; text-decoration:none;font-size: 16px;}</style></head><body><header><h1>LetTheGhostsIn</h1></header>';
var html_footer = '<footer><p>Designed and developed by the <b>Dream</b>Team.</p></footer></body></html>';
var html_learn_form = '<form formenctype="text/plain" name="addreq" action="420?" method="get"><input type="text" name="e" placeholder="ShareYourWisdom" maxlength="140"><button>Share</button></form>';
function onConnection(request, response)
{
console.log("[blip]" + new Date().toUTCString());
if(request.url.slice(0, 7) == "/420?e=")
{
try
{
var to_add = decodeURIComponent(request.url.slice(7)).replace(/\+/g, ' ');
}
catch(err)
{
console.log('uri: ' + err);
serve_page(response);
return;
}
console.log(new Date().toUTCString() + ": [+]" + to_add);
// attempt to find in the db
db.wisdom.findOne({note: to_add},
function (err, doc)
{
if(err) console.log('db: ' + err);
if(doc) { doc.floater++; db.wisdom.save(doc); }
else db.wisdom.save({note: to_add, floater: 0});
serve_page(response);
}
);
}
else if(request.url.slice(0, 7) == "/666?e=")
{
try
{
var to_del = decodeURIComponent(request.url.slice(7)).replace(/\+/g, ' ');
}
catch(err)
{
console.log('uri: ' + err);
serve_page(response);
return;
}
console.log(new Date().toUTCString() + ": [-]" + to_del);
// attempt to find in the db
db.wisdom.findOne({note: to_del},
function (err, doc)
{
if(err) console.log('db: ' + err);
if(doc) { if(doc.floater <= -666) {db.wisdom.remove(doc);} else {doc.floater--; db.wisdom.save(doc);} }
serve_page(response);
}
);
}
else serve_page(response);
}
function serve_page(response)
{
db.wisdom.find().limit(1000).sort({floater:-1},
function(err, docs)
{
var html_list = '';
if(err) console.log('db: ' + err);
else if(docs != null)
{
for(var d = 0; d < docs.length; d++)
{
html_list += '<div class="result"><blockquote>' + docs[d].note + '</blockquote>';
html_list += '<a href="/420?e=' + docs[d].note + '">ThisIsWise</a></div>';
//html_list += '[ <a href="/666?e=' + docs[d].note + '">-</a> | ';
//html_list += '<a href="/420?e=' + docs[d].note + '">+</a> ] ';
}
}
response.writeHead(200, {'Content-Type': 'text/html'});
response.end(html_header + html_learn_form + '<section>' + html_list + '</section>' + html_footer, 'utf-8');
}
);
}
var port = 80;
http.createServer(onConnection).listen(port);
console.log("Serving port:" + port);
console.log("DB: " + db_url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment