-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
57 lines (49 loc) · 1.47 KB
/
server.js
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
var appender = require('append-only');
var peeps = appender();
var argv = require('optimist').argv;
peeps.on('item', function (peep) {
var s = peep.__id.split(':');
var who = s[0], timestamp = s[1];
var title = peep.name + '{' + who + '}';
title += Array(Math.max(4, 50 - title.length)).join(' ');
title += timestamp;
console.log('# ' + title);
console.log(' ' + slashify(peep.message));
});
var http = require('http');
var ecstatic = require('ecstatic')(__dirname + '/static');
var server = http.createServer(function (req, res) {
if (req.url === '/replicate') {
res.setHeader('content-type', 'application/json-lines');
req.pipe(peeps.createStream()).pipe(res);
}
else if (req.method === 'POST') {
var name = req.url.replace(/^\//, '');
var data = '';
req.on('data', function (buf) { data += buf });
req.on('end', function () {
peeps.push({
name : name,
message : data
});
res.end();
});
}
else res.end('...\n');
});
server.listen(argv._[0]);
function slashify (s) {
return s.replace(/[\r\n\t\f]/g, function (x) {
return {
'\r' : '\\r',
'\n' : '\\n',
'\t' : '\\t',
'\f' : '\\f'
}[x];
});
}
var request = require('request');
argv._.slice(1).forEach(function (u) {
var r = request.post(u);
r.pipe(peeps.createStream()).pipe(r);
});