diff --git a/server.js b/server.js index e64f2ee..6c400aa 100644 --- a/server.js +++ b/server.js @@ -13,6 +13,7 @@ var MODELS = {}; var CACHE = new LRU({ max: 50, dispose: key => { + log("disposing", key, "from cache"); var model = MODELS[key]; model && model.save(); delete MODELS[key]; @@ -27,7 +28,18 @@ var getTimeStamp = () => { 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) { + log(req.ip, "opens /new"); res.send(view.newNotePage(getTimeStamp() + md5(Math.random()))); }); @@ -35,18 +47,20 @@ app.post('/note', function (req, res) { var body = req.body, session = body.session, 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); if (session.indexOf(getTimeStamp()) != 0) return sendResponse(res, 400, "Session expired"); var expectedSignature = md5(session + note.replace(/[\n\r]/g, "")); if (expectedSignature != body.signature) return sendResponse(res, 400, "Signature mismatch"); - console.log(body) - if (body.action == "POST") + if (action == "POST") storage.addNote(note, password).then(goToNote); else - storage.updateNote(body.id, password, note).then(note => { + storage.updateNote(id, password, note).then(note => { CACHE.del(note.id); goToNote(note); }, @@ -55,11 +69,13 @@ app.post('/note', 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; + log(req.ip, "resolves deprecated id", url); storage.getNoteId(url).then(note => note ? res.redirect("/" + note.id) : notFound(res)); }); app.get(/\/([a-z0-9]+\/edit)/, function (req, res) { var link = req.params["0"].replace("/edit", ""); + log(req.ip, "calls /edit on", link); storage.getNote(link).then(note => res.send(note ? view.editNotePage(getTimeStamp() + md5(Math.random()), note) : notFound(res))); @@ -67,6 +83,7 @@ app.get(/\/([a-z0-9]+\/edit)/, function (req, res) { app.get(/\/([a-z0-9]+\/export)/, function (req, res) { var link = req.params["0"].replace("/export", ""); + log(req.ip, "calls /export on", link); res.set({ 'Content-Type': 'text/plain', 'Charset': 'utf-8' }); storage.getNote(link).then(note => note ? 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) { var link = req.params["0"].replace("/stats", ""); + log(req.ip, "calls /stats on", link); var promise = link in MODELS ? new Promise(resolve => resolve(MODELS[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) { var link = req.params["0"]; + log(req.ip, "open note", link); if (CACHE.has(link)) { + log(link, "is cached!"); var model = MODELS[link]; if (!model) return notFound(res); model.views++; res.send(CACHE.get(link)); } else storage.getNote(link).then(note => { + log(link, "is not cached, resolving..."); if (!note) { CACHE.set(link, null); 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, "

" + message + "

", "")); +} var notFound = res => sendResponse(res, 404, "Not found"); 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(() => { - console.log("saving stats..."); - Object.keys(MODELS).forEach(id => MODELS[id].save()) + var keys = Object.keys(MODELS); + log("saving stats for", keys.length, "models..."); + keys.forEach(id => MODELS[id].save()) }, 60 * 5 * 1000); \ No newline at end of file diff --git a/src/storage.js b/src/storage.js index cd45f6a..c3d5baa 100644 --- a/src/storage.js +++ b/src/storage.js @@ -19,17 +19,11 @@ var Note = sequelize.define('Note', { views: { type: Sequelize.INTEGER, defaultValue: 0 } }); -module.exports.getNote = id => { - console.log("resolving note", id); - return Note.findById(id); -} +module.exports.getNote = id => Note.findById(id); -module.exports.getNoteId = deprecatedId => { - console.log("resolving deprecated Id", deprecatedId); - return Note.findOne({ +module.exports.getNoteId = deprecatedId => Note.findOne({ where: { deprecatedId: deprecatedId } }); -} var generateId = () => [1, 1, 1, 1, 1] .map(() => {