forked from chmllr/notehub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
155 lines (138 loc) · 4.57 KB
/
server.js
File metadata and controls
155 lines (138 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var express = require('express');
var view = require('./src/view');
var storage = require('./src/storage');
var md5 = require('md5');
var LRU = require("lru-cache")
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
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];
}
});
var getTimeStamp = () => {
var timestamp = new Date().getTime();
timestamp = Math.floor(timestamp / 10000000);
return (timestamp).toString(16)
}
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())));
});
app.post('/note', function (req, res) {
var body = req.body,
session = body.session,
note = body.note,
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");
if (action == "POST")
storage.addNote(note, password).then(goToNote);
else {
CACHE.del(id);
storage.updateNote(id, password, note).then(goToNote,
error => sendResponse(res, 403, error.message));
}
});
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);
if (CACHE.has(url)) {
log(url, "is cached!");
var id = CACHE.get(url);
if (id) res.redirect("/" + id);
else notFound(res);
} else storage.getNoteId(url).then(note => {
log(url, "is not cached, resolving...");
if (note) {
CACHE.set(url, note.id);
res.redirect("/" + note.id)
} else {
CACHE.set(url, null);
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)));
});
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)
: notFound(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);
promise.then(note => note
? res.send(view.renderStats(note))
: notFound(res));
});
app.get(/\/([a-z0-9]+)/, function (req, res) {
var link = req.params["0"];
log(req.ip, "open note", link, "from", req.get("Referer"));
if (CACHE.has(link)) {
log(link, "is cached!");
var note = MODELS[link];
if (!note) return notFound(res);
note.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);
}
var content = view.renderNote(note);
CACHE.set(link, content);
MODELS[link] = note;
note.views++;
res.send(content);
});
});
var sendResponse = (res, code, message) => {
log("sending response", code, message);
res.status(code).send(view.renderPage(message, "<h1>" + message + "</h1>", ""));
}
var notFound = res => sendResponse(res, 404, "Not found");
var server = app.listen(process.env.PORT || 3000, function () {
log('NoteHub server listening on port %s', server.address().port);
});
setInterval(() => {
var keys = Object.keys(MODELS);
log("saving stats for", keys.length, "models...");
keys.forEach(id => MODELS[id].save())
}, 60 * 5 * 1000);