-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.js
71 lines (62 loc) · 1.61 KB
/
handlers.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var handlers = module.exports = {};
var qs = require('querystring');
var fs = require('fs');
var https = require('https');
handlers.home = function(req, res) {
var index = fs.readFileSync(__dirname + '/views/index.html');
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.end(index);
};
handlers.file = function(req, res) {
var file = fs.readFileSync(__dirname + '/public' + req.url);
var ext = (req.url).split('.')[1];
res.writeHead(200, {
'content-type': 'text/' + ext
});
res.end(file);
};
handlers.raiseissue = function(req, res) {
console.log(req, req.url);
parseBody(req, function(body) {
body = qs.parse(body);
console.log(body);
postToGithub(body);
res.end('done');
});
};
function parseBody(object, callback) {
var body = "";
object.on('data', function(data) {
body += data;
});
object.on('end', function() {
callback(body);
});
object.on('error', function(e) {
console.log("Got error: " + e.message);
});
}
function postToGithub(body) {
var options = {
host: 'api.github.com',
path: '/repos/mantagen/postgithubissue/issues',
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0',
'Authorization': 'Basic ' + new Buffer(body.username + ':' + body.password).toString('base64')
},
method: 'POST'
};
body = {
title: body.title,
body: body.body
};
var reqToGithub = https.request(options, function(res) {
parseBody(res, function(body) {
console.log(body);
});
});
reqToGithub.write(JSON.stringify(body));
reqToGithub.end();
}