Created
November 11, 2014 03:14
-
-
Save meandavejustice/11bc3cdaaf8b4256957a to your computer and use it in GitHub Desktop.
A mega simple linkblog using with leveldb and nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var basic = require('basic'); | |
var auth = basic(function(user, pass, cb) { | |
if (user === 'meandave' && pass === 'badpassword') { | |
return cb(null); | |
} | |
cb(new Error('Access Denied')); | |
}); | |
function sendError(req, res) { | |
res.statusCode = 401 | |
res.setHeader("WWW-Authenticate", "Basic realm=\"Secure Area\"") | |
res.end("Unauthorized") | |
} | |
module.exports = { | |
auth: auth, | |
sendError: sendError | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var h = require('hyperscript'); | |
function genLink(obj) { | |
return h('li', obj.date + ' ', | |
h('a', obj.link, {'href': obj.link})); | |
} | |
function getList(links) { | |
return links.map(function(obj) { | |
return genLink(obj); | |
}); | |
} | |
module.exports = function(links) { | |
return h('html', | |
h('head', | |
h('link', {'rel': 'stylesheet', 'href': 'style.css'})), | |
h('body', | |
h('div.main', | |
h('h3', "Welcome to meandave's linkblog"), | |
h('ul', | |
getList(links))))) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require("http"); | |
var inline = require('html-inline'); | |
var formidable = require('formidable'); | |
var Router = require("routes-router"); | |
var levelUp = require('level'); | |
var util = require('util'); | |
var html = require('./html'); | |
var db = levelUp('./linkdb', {valueEncoding: 'json'}); | |
var auth = require('./auth'); | |
var app = Router() | |
var port = 8000; | |
function sendError(req, res) { | |
res.statusCode = 401 | |
res.setHeader("WWW-Authenticate", "Basic realm=\"Secure Area\"") | |
res.end("Unauthorized") | |
} | |
function putLink(key, value) { | |
db.put(key, value, function (err) { | |
if (err) return console.log('Ooops!', err); | |
console.log('put: ', value); | |
}); | |
} | |
function newLink(req, res) { | |
if (req.method.toLowerCase() == 'post') { | |
// parse a file upload | |
var form = new formidable.IncomingForm(); | |
form.parse(req, function(err, fields) { | |
var key = new Date().toString(); | |
putLink(key, { | |
link: fields.link, | |
date: key | |
}); | |
res.writeHead(200, {'content-type': 'text/plain'}); | |
res.write('dat link was posted! \n\n'); | |
res.end(util.inspect({fields: fields})); | |
}); | |
return; | |
} else { | |
res.writeHead(200, {'content-type': 'text/html'}); | |
res.end( | |
'<form action="/newlink" enctype="multipart/form-data" method="post" style="width: 600px; margin: 80px auto;">'+ | |
'<h2 style="text-align: center;color: #868686;">Feedback</h2>'+ | |
'<input type="text" placeholder="link" name="link" style="width: 100%;height: 50px;font-size: 20px;padding: 10px; margin-bottom: 5px;"><br>'+ | |
'<input type="submit" value="Send">'+ | |
'</form>' | |
); | |
} | |
} | |
app.addRoute("/newlink", function (req, res) { | |
auth.auth(req, res, function(err) { | |
if (err) auth.sendError(req, res); | |
newLink(req, res); | |
}); | |
}); | |
app.addRoute("*", function(req, res) { | |
var demLinks = []; | |
db.createValueStream() | |
.on('data', function (data) { | |
demLinks.push(data); | |
}) | |
.on('end', function() { | |
res.writeHead(200, {'content-type': 'text/html'}); | |
console.log(inline(html(demLinks).outerHTML)); | |
res.end(html(demLinks).outerHTML); | |
}) | |
}); | |
var server = http.createServer(app); | |
server.listen(port); | |
console.log("Server listening on port: ", port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment