-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
115 lines (105 loc) · 2.87 KB
/
Copy pathapp.js
File metadata and controls
115 lines (105 loc) · 2.87 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
var https = require("https"),
querystring = require('querystring'),
http = require('http'),
fs = require('fs'),
url = require('url'),
host = process.env.JIRAHOST || "atlassian.net",
port = process.env.JIRAPORT || 443,
appPort = process.env.PORT || 8164;
function loadJira(usr, pwd, jql, done){
var query = {jql: jql};
var jqlString = JSON.stringify(query);
var path = "/rest/api/latest/search?" + querystring.stringify(query);
var req = https.request({
hostname: host,
port: port,
path: path,
method: "GET",
auth: usr+":"+pwd,
headers: {
"Content-Type": "application/json",
"Content-Length": jqlString.length
}
}, function(result){
result.setEncoding('utf-8');
var responseString = '';
result.on('data', function(data) {
responseString += data;
});
result.on('end', function() {
done(responseString);
});
});
req.write(jqlString);
req.end();
}
function fileHandler(contentType, res){
return function (err, html) {
if (err) {
throw err;
}
res.writeHead(200, {'Content-Type': contentType});
res.end(html);
}
}
function handlePost(req, callback){
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
var POST = querystring.parse(body);
callback(POST);
});
return;
}
function isHttp(req){
return req.headers['x-forwarded-proto'] === 'https' || req.headers['x-arr-ssl'];
}
http.createServer(function (req, res) {
if (!isHttp(req) && process.env.NODE_ENV === 'production') {
console.log('Request for login page made over HTTP, redirecting to HTTPS');
res.writeHead(302, {
'Location': 'https://' + req.headers.host + req.url
});
res.end();
return;
}
if(req.url === "/"){
req.url = "/index.html";
}
var root = "./app";
var parsedUrl = url.parse(req.url);
var htmlExpr = /.+\.html/g;
var lessExpr = /.+\.less/g;
var jsExpr = /.+\.js/g;
var pngExpr = /.+\.png/g;
if(htmlExpr.test(parsedUrl.path)){
fs.readFile(root + parsedUrl.path, fileHandler('text/html', res));
return;
}
if(lessExpr.test(parsedUrl.path)){
fs.readFile(root + parsedUrl.path, fileHandler('text/less', res));
return;
}
if(jsExpr.test(parsedUrl.path)){
fs.readFile(root + parsedUrl.path, fileHandler('text/javascript', res));
return;
}
if(pngExpr.test(parsedUrl.path)){
fs.readFile(root + parsedUrl.path, fileHandler('image/png', res));
return;
}
if(parsedUrl.pathname === "/search"){
handlePost(req, function(data){
loadJira(data.usr, data.pwd, data.jql, function(jiraJson){
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(jiraJson);
});
});
return;
}
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('404');
}).listen(appPort);
console.log('Server running on port ' + appPort);