This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
96 lines (77 loc) · 2.64 KB
/
app.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
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
const fs = require('fs');
const http_server = require('http');
const http_proxy = require('http-proxy');
const path = require('path');
const request = require('request-promise-native');
const RSS = require('rss');
const server = http_server.createServer(http);
const proxy = http_proxy.createServer({ xfwd: true });
async function rss() {
const apikey = process.env.APIKEY;
const outages = await request('https://api2.panopta.com/v2/outage', {
headers: { authorization: `ApiKey ${apikey}` },
json: true,
});
const feed = new RSS({
title: 'Tugboat Status - Incident History',
feed_url: 'https://www.tugboatqa-status.com/rss',
site_url: 'https://www.tugboatqa.com',
image_url: 'https://tugboatqa.github.io/assets.tugboat.qa/images/logo/logo_horizontal_light.svg',
docs: 'https://docs.tugboatqa.com',
copyright: 'Tugboat',
language: 'en',
pubDate: new Date(),
});
outages.outage_list.forEach((outage) => {
if (!outage.exclude_from_availability) {
feed.item({
title: `Incident at ${new Date(outage.start_time)}`,
description: outage.description,
url: 'https://www.tugboatqa-status.com',
guid: outage.hash,
date: new Date(outage.start_time),
});
}
});
return feed.xml({ indent: true });
}
async function svg(req, res) {
const file = path.join(__dirname, req.url);
const stream = fs.createReadStream(file);
stream.on('open', () => {
res.setHeader('Content-Type', 'image/svg+xml');
stream.pipe(res);
});
stream.on('error', () => {
res.setHeader('Content-Type', 'text/plain');
res.statusCode = 404;
res.end('Not Found');
});
}
async function http(req, res) {
if (req.url === '/_status') {
return res.end('Status OK');
}
if (req.url === '/rss') {
const body = await rss();
res.setHeader('Cache-Control', 'max-age=0, private, must-revalidate');
res.setHeader('Content-Type', 'application/rss+xml');
return res.end(body);
}
if (req.url.match(/^\/svg\/.*/)) {
return svg(req, res);
}
const target = 'https://status.panopta.com';
if (req.url === '/') {
return proxy.web(req, res, { target: `${target}/tugboat`, changeOrigin: true, ignorePath: true });
}
return proxy.web(req, res, { target, changeOrigin: true });
}
const port = process.env.PORT || 3000;
server.listen(port, (err) => {
if (err) {
console.error(err);
} else {
console.log(`Service listening on port ${port}`);
}
});