|
|
|
@ -1,5 +1,3 @@
|
|
|
|
|
import cache from "memory-cache";
|
|
|
|
|
|
|
|
|
|
import { httpProxy } from "utils/proxy/http";
|
|
|
|
|
import { formatApiCall } from "utils/proxy/api-helpers";
|
|
|
|
|
import getServiceWidget from "utils/config/service-helpers";
|
|
|
|
@ -7,7 +5,6 @@ import createLogger from "utils/logger";
|
|
|
|
|
import widgets from "widgets/widgets";
|
|
|
|
|
|
|
|
|
|
const proxyName = "watchtowerProxyHandler";
|
|
|
|
|
const headerCacheKey = `${proxyName}__headers`;
|
|
|
|
|
const logger = createLogger(proxyName);
|
|
|
|
|
|
|
|
|
|
export default async function watchtowerProxyHandler(req, res) {
|
|
|
|
@ -25,34 +22,27 @@ export default async function watchtowerProxyHandler(req, res) {
|
|
|
|
|
return res.status(400).json({ error: "Invalid proxy service type" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let headers = cache.get(headerCacheKey);
|
|
|
|
|
if (!headers) {
|
|
|
|
|
headers = {
|
|
|
|
|
"Authorization": `Bearer ${widget.key}`,
|
|
|
|
|
}
|
|
|
|
|
cache.put(headerCacheKey, headers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
|
|
|
|
|
|
|
|
|
|
const method = "GET"
|
|
|
|
|
const [status, contentType, data] = await httpProxy(url, {
|
|
|
|
|
method,
|
|
|
|
|
headers,
|
|
|
|
|
method: "GET",
|
|
|
|
|
headers: {
|
|
|
|
|
"Authorization": `Bearer ${widget.key}`,
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (status !== 200 || !data) {
|
|
|
|
|
logger.error("Error getting data from WatchTower: %d. Data: %s", status, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const cleanData = data.toString().split("\n").filter(s => s.startsWith("watchtower"))
|
|
|
|
|
const jsonRes={}
|
|
|
|
|
const jsonRes = {}
|
|
|
|
|
|
|
|
|
|
cleanData.map(e => e.split(" ")).forEach(strArray => {
|
|
|
|
|
const [key, value] = strArray
|
|
|
|
|
jsonRes[key] = value
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (status !== 200) {
|
|
|
|
|
logger.error("Error getting data from WatchTower: %d. Data: %s", status, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (contentType) res.setHeader("Content-Type", contentType);
|
|
|
|
|
return res.status(status).send(jsonRes);
|
|
|
|
|
}
|
|
|
|
|