|
|
|
@ -13,6 +13,7 @@ var MODELS = {}; |
|
|
|
var CACHE = new LRU({ |
|
|
|
var CACHE = new LRU({ |
|
|
|
max: 50, |
|
|
|
max: 50, |
|
|
|
dispose: key => { |
|
|
|
dispose: key => { |
|
|
|
|
|
|
|
log("disposing", key, "from cache"); |
|
|
|
var model = MODELS[key]; |
|
|
|
var model = MODELS[key]; |
|
|
|
model && model.save(); |
|
|
|
model && model.save(); |
|
|
|
delete MODELS[key]; |
|
|
|
delete MODELS[key]; |
|
|
|
@ -27,7 +28,18 @@ var getTimeStamp = () => { |
|
|
|
|
|
|
|
|
|
|
|
app.use(express.static(__dirname + '/resources/public')); |
|
|
|
app.use(express.static(__dirname + '/resources/public')); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var log = function () { |
|
|
|
|
|
|
|
var date = new Date(); |
|
|
|
|
|
|
|
var timestamp = date.getDate() + "/" + date.getMonth() + " " + date.getHours() + ":" + |
|
|
|
|
|
|
|
date.getMinutes() + ":" + date.getSeconds() + "." + date.getMilliseconds(); |
|
|
|
|
|
|
|
var message = Array.prototype.slice.call(arguments); |
|
|
|
|
|
|
|
message.unshift("--"); |
|
|
|
|
|
|
|
message.unshift(timestamp); |
|
|
|
|
|
|
|
console.log.apply(console, message); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
app.get('/new', function (req, res) { |
|
|
|
app.get('/new', function (req, res) { |
|
|
|
|
|
|
|
log(req.ip, "opens /new"); |
|
|
|
res.send(view.newNotePage(getTimeStamp() + md5(Math.random()))); |
|
|
|
res.send(view.newNotePage(getTimeStamp() + md5(Math.random()))); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
@ -35,18 +47,20 @@ app.post('/note', function (req, res) { |
|
|
|
var body = req.body, |
|
|
|
var body = req.body, |
|
|
|
session = body.session, |
|
|
|
session = body.session, |
|
|
|
note = body.note, |
|
|
|
note = body.note, |
|
|
|
password = body.password; |
|
|
|
password = body.password, |
|
|
|
|
|
|
|
action = body.action, |
|
|
|
|
|
|
|
id = body.id; |
|
|
|
|
|
|
|
log(req.ip, "calls /note to", action, id); |
|
|
|
var goToNote = note => res.redirect("/" + note.id); |
|
|
|
var goToNote = note => res.redirect("/" + note.id); |
|
|
|
if (session.indexOf(getTimeStamp()) != 0) |
|
|
|
if (session.indexOf(getTimeStamp()) != 0) |
|
|
|
return sendResponse(res, 400, "Session expired"); |
|
|
|
return sendResponse(res, 400, "Session expired"); |
|
|
|
var expectedSignature = md5(session + note.replace(/[\n\r]/g, "")); |
|
|
|
var expectedSignature = md5(session + note.replace(/[\n\r]/g, "")); |
|
|
|
if (expectedSignature != body.signature) |
|
|
|
if (expectedSignature != body.signature) |
|
|
|
return sendResponse(res, 400, "Signature mismatch"); |
|
|
|
return sendResponse(res, 400, "Signature mismatch"); |
|
|
|
console.log(body) |
|
|
|
if (action == "POST") |
|
|
|
if (body.action == "POST") |
|
|
|
|
|
|
|
storage.addNote(note, password).then(goToNote); |
|
|
|
storage.addNote(note, password).then(goToNote); |
|
|
|
else |
|
|
|
else |
|
|
|
storage.updateNote(body.id, password, note).then(note => { |
|
|
|
storage.updateNote(id, password, note).then(note => { |
|
|
|
CACHE.del(note.id); |
|
|
|
CACHE.del(note.id); |
|
|
|
goToNote(note); |
|
|
|
goToNote(note); |
|
|
|
}, |
|
|
|
}, |
|
|
|
@ -55,11 +69,13 @@ app.post('/note', function (req, res) { |
|
|
|
|
|
|
|
|
|
|
|
app.get("/:year/:month/:day/:title", function (req, res) { |
|
|
|
app.get("/:year/:month/:day/:title", function (req, res) { |
|
|
|
var P = req.params, url = P.year + "/" + P.month + "/" + P.day + "/" + P.title; |
|
|
|
var P = req.params, url = P.year + "/" + P.month + "/" + P.day + "/" + P.title; |
|
|
|
|
|
|
|
log(req.ip, "resolves deprecated id", url); |
|
|
|
storage.getNoteId(url).then(note => note ? res.redirect("/" + note.id) : notFound(res)); |
|
|
|
storage.getNoteId(url).then(note => note ? res.redirect("/" + note.id) : notFound(res)); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
app.get(/\/([a-z0-9]+\/edit)/, function (req, res) { |
|
|
|
app.get(/\/([a-z0-9]+\/edit)/, function (req, res) { |
|
|
|
var link = req.params["0"].replace("/edit", ""); |
|
|
|
var link = req.params["0"].replace("/edit", ""); |
|
|
|
|
|
|
|
log(req.ip, "calls /edit on", link); |
|
|
|
storage.getNote(link).then(note => res.send(note |
|
|
|
storage.getNote(link).then(note => res.send(note |
|
|
|
? view.editNotePage(getTimeStamp() + md5(Math.random()), note) |
|
|
|
? view.editNotePage(getTimeStamp() + md5(Math.random()), note) |
|
|
|
: notFound(res))); |
|
|
|
: notFound(res))); |
|
|
|
@ -67,6 +83,7 @@ app.get(/\/([a-z0-9]+\/edit)/, function (req, res) { |
|
|
|
|
|
|
|
|
|
|
|
app.get(/\/([a-z0-9]+\/export)/, function (req, res) { |
|
|
|
app.get(/\/([a-z0-9]+\/export)/, function (req, res) { |
|
|
|
var link = req.params["0"].replace("/export", ""); |
|
|
|
var link = req.params["0"].replace("/export", ""); |
|
|
|
|
|
|
|
log(req.ip, "calls /export on", link); |
|
|
|
res.set({ 'Content-Type': 'text/plain', 'Charset': 'utf-8' }); |
|
|
|
res.set({ 'Content-Type': 'text/plain', 'Charset': 'utf-8' }); |
|
|
|
storage.getNote(link).then(note => note |
|
|
|
storage.getNote(link).then(note => note |
|
|
|
? res.send(note.text) |
|
|
|
? res.send(note.text) |
|
|
|
@ -75,6 +92,7 @@ app.get(/\/([a-z0-9]+\/export)/, function (req, res) { |
|
|
|
|
|
|
|
|
|
|
|
app.get(/\/([a-z0-9]+\/stats)/, function (req, res) { |
|
|
|
app.get(/\/([a-z0-9]+\/stats)/, function (req, res) { |
|
|
|
var link = req.params["0"].replace("/stats", ""); |
|
|
|
var link = req.params["0"].replace("/stats", ""); |
|
|
|
|
|
|
|
log(req.ip, "calls /stats on", link); |
|
|
|
var promise = link in MODELS |
|
|
|
var promise = link in MODELS |
|
|
|
? new Promise(resolve => resolve(MODELS[link])) |
|
|
|
? new Promise(resolve => resolve(MODELS[link])) |
|
|
|
: storage.getNote(link); |
|
|
|
: storage.getNote(link); |
|
|
|
@ -85,12 +103,15 @@ app.get(/\/([a-z0-9]+\/stats)/, function (req, res) { |
|
|
|
|
|
|
|
|
|
|
|
app.get(/\/([a-z0-9]+)/, function (req, res) { |
|
|
|
app.get(/\/([a-z0-9]+)/, function (req, res) { |
|
|
|
var link = req.params["0"]; |
|
|
|
var link = req.params["0"]; |
|
|
|
|
|
|
|
log(req.ip, "open note", link); |
|
|
|
if (CACHE.has(link)) { |
|
|
|
if (CACHE.has(link)) { |
|
|
|
|
|
|
|
log(link, "is cached!"); |
|
|
|
var model = MODELS[link]; |
|
|
|
var model = MODELS[link]; |
|
|
|
if (!model) return notFound(res); |
|
|
|
if (!model) return notFound(res); |
|
|
|
model.views++; |
|
|
|
model.views++; |
|
|
|
res.send(CACHE.get(link)); |
|
|
|
res.send(CACHE.get(link)); |
|
|
|
} else storage.getNote(link).then(note => { |
|
|
|
} else storage.getNote(link).then(note => { |
|
|
|
|
|
|
|
log(link, "is not cached, resolving..."); |
|
|
|
if (!note) { |
|
|
|
if (!note) { |
|
|
|
CACHE.set(link, null); |
|
|
|
CACHE.set(link, null); |
|
|
|
return notFound(res); |
|
|
|
return notFound(res); |
|
|
|
@ -102,16 +123,19 @@ app.get(/\/([a-z0-9]+)/, function (req, res) { |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
var sendResponse = (res, code, message) => |
|
|
|
var sendResponse = (res, code, message) => { |
|
|
|
|
|
|
|
log("sending response", code, message); |
|
|
|
res.status(code).send(view.renderPage(message, "<h1>" + message + "</h1>", "")); |
|
|
|
res.status(code).send(view.renderPage(message, "<h1>" + message + "</h1>", "")); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var notFound = res => sendResponse(res, 404, "Not found"); |
|
|
|
var notFound = res => sendResponse(res, 404, "Not found"); |
|
|
|
|
|
|
|
|
|
|
|
var server = app.listen(process.env.PORT || 3000, function () { |
|
|
|
var server = app.listen(process.env.PORT || 3000, function () { |
|
|
|
console.log('NoteHub server listening on port %s', server.address().port); |
|
|
|
log('NoteHub server listening on port %s', server.address().port); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
setInterval(() => { |
|
|
|
setInterval(() => { |
|
|
|
console.log("saving stats..."); |
|
|
|
var keys = Object.keys(MODELS); |
|
|
|
Object.keys(MODELS).forEach(id => MODELS[id].save()) |
|
|
|
log("saving stats for", keys.length, "models..."); |
|
|
|
|
|
|
|
keys.forEach(id => MODELS[id].save()) |
|
|
|
}, 60 * 5 * 1000); |
|
|
|
}, 60 * 5 * 1000); |