summaryrefslogtreecommitdiffstats
path: root/lib/main.js
blob: 461f47b5d19e8528ab10536f922b1383ffd8007f (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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
var http = require('http'),
fs = require('fs'),
path = require('path'),
bee = require('beeline'),
qs = require('querystring'),
hl = require(__dirname + '/highlight'),
tmpl = require(__dirname + '/templates'),
pasteInit = require(__dirname + '/paste');

var config = {
    port: 8080,
    database: __dirname + '/../db/pastes.kch'
};

var paste;

function _200(res, contentType, data) {
    res.writeHead(200, {'Content-Type': contentType});
    res.write(data);
    res.end('\n');
}

function _302(res, url) {
    res.writeHead(302, {'Location': url});
    res.end();
}

function _404(res) {
    res.writeHead(404, {'Content-Type': 'text/plain'});
    res.end('404: Not found.\n');
}

function _500(res, err) {
    res.writeHead(404, {'Content-Type': 'text/plain'});
    res.write('500: Internal Server Error');

    if (err) {
        res.write(':\n');
        res.write(JSON.stringify(err));
    }

    res.end('\n');
}

function parsePost(req, callback) {
    var content = '';

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

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

function getContentType(ext) {
    contentType = {
        '.css': 'text/css',
        '.js': 'text/javascript',
        '.png': 'image/png'
    };

    if (contentType[ext]) {
        return contentType[ext];
    }
    else {
        return 'unknown';
    }
}

function checkPath(file, base) {
    var normFile = path.normalize(file);
    var normBase = path.normalize(base);

    if (normFile.indexOf(normBase) === 0) {
        return true;
    }
    else {
        return false;
    }
}

function serveStaticFiles(res, filepath) {
    var base = path.join(__dirname, '..', 'static');
    var file = path.join(base, filepath);

    if (checkPath(file, base)) {
        fs.lstat(file, function(err, stat) {
            if (stat.isFile()) {
                var ext = path.extname(file);
                var contentType = getContentType(ext);

                fs.readFile(file, function (err, data) {
                    if (err) {
                        _500(res, err);
                    }
                    else {
                        _200(res, contentType, data);
                    }
                });
            }
            else {
                _404(res);
            }
        });
    }
    else {
        _404(res);
    }
}

var router = bee.route({
    "/": function(req, res) {
        tmpl.renderHtml('submit-form.tmpl', {}, res);
    },

    "r`^/plain/([^/]+)$`": function(req, res, id) {
        paste.get(id[0], function(data) {
            if (data) {
                _200(res, 'text/plain', data.content);
            }
            else {
                _404(res);
            }
        });
    },

    "r`^/get/([^/]+)$`": function(req, res, id) {
        paste.get(id[0], function(data) {
            if (data) {
                data.styles = [{name: '/static/highlight/github.css'}];
                data.content = hl.highlight(data.content, data.language);

                tmpl.renderHtml('paste.tmpl', data, res);
            }
            else {
                _404(res);
            }
        });
    },

    "r`^/static/(.+)$`": function(req, res, filepath) {
        serveStaticFiles(res, filepath[0]);
    },

    "/add": {
        POST: function(req, res) {
            parsePost(req, function(post) {
                paste.add(post, function(err, id) {
                    if (err) {
                        _500(res, err);
                    }
                    else {
                        console.log('new paste: %s', id);
                        _302(res, '/get/' + id);
                    }
                });
            });
        },

        GET: function(req, res) {
            _404(res);
        }
    },

    "`404`": function(req, res) {
        _404(res);
    },

    "`503`": function(req, res, err) {
        _500(res, err);
    }
});

var server = function(port) {
    port = port || config.port;

    pasteInit.init(config, function(func) {
        paste = func;

        http.createServer(router).listen(port);
        console.log('Listening on port %d...', port);
    });
};

if (typeof module == "object" && typeof require == "function") {
    exports.server = server;
    exports.config = config;
}
if (module === require.main) {
    server();
}