summaryrefslogtreecommitdiffstats
path: root/lib/main.js
blob: 6e2f36ab89dbabaa31d0419dd633fa257292a07f (plain) (blame)
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
var http = require('http'),
router = require('choreographer').router(),
qs = require('querystring'),
uuid = require(__dirname + '/uuid'),
hl = require(__dirname + '/highlight'),
kyoto = require('kyoto');

var db = new kyoto.open(__dirname + '/../db/pastes.kch', 'a+', kyotoOpen);

function generateId(callback) {
    var id = uuid.generate(14);
    db.get(id, function(err, value) {
        if (value) {
            generateId();
        }
        else {
            callback(id);
        }
    });
}

function getPaste(plain, req, res, paste) {
    db.get(paste, function(err, value) {
        if (err) {
            res.writeHead(404, {'Content-Type': 'text/plain'});
            res.end('404: ' + req.url + ' not found:\n' + err + '\n');
        }
        else {
            var data = JSON.parse(value);
            if (plain) {
                res.writeHead(200, {'Content-Type': 'text/plain'});
                res.write(data.content);
            }
            else {
                res.writeHead(200, {'Content-Type': 'text/html'});
                res.write(hl.highlight(data.content, data.language));
            }
            res.end('\n');
        }
    });
}

router
    .get('/plain/*', function(req, res, paste) {
        getPaste(true, req, res, paste);
    })
    .get('/get/*', function(req, res, paste) {
        getPaste(false, req, res, paste);
    })
    .get('/', function(req, res) {
    })
    .post('/add', function(req, res) {
        var content = '';

        req.on('data', function(chunk) {
            content += chunk;
        });

        req.on('end', function() {
            var post = qs.parse(content);

            generateId(function(id) {
                var data = {
                    content: post.content,
                    language: post.language,
                    time: Date()
                };
                db.set(id, JSON.stringify(data), function(err) {
                    if (err) {
                        res.writeHead(500, {'Content-Type': 'text/plain'});
                        res.write(err);
                        res.end('\n');
                    }
                    else {
                        console.log('new paste: %s', id);
                        res.writeHead(200, {'Content-Type': 'text/plain'});
                        res.write(id);
                        res.end('\n');
                    }
                });
            });
        });
    })
    .notFound(function(req, res) {
        res.writeHead(404, {'Content-Type': 'text/plain'});
        res.end('404: ' + req.url + ' not found.\n');
    });


function kyotoOpen(err) {
    if (err) throw err;

    http.createServer(router).listen(8080);
    console.log('Listening on port 8080...');
}

process.on('uncaughtException', function(exeption) {
    process.exit(1);
});

process.on('exit', function() {
    db.close(function(err) { console.log(err); });
});