remove unnecessary watchtower header cache, code style

This commit is contained in:
Michael Shamoon 2022-10-28 00:58:59 -07:00
parent 6fb9ce1b53
commit 5f71486b74
2 changed files with 10 additions and 20 deletions

View File

@ -1,5 +1,3 @@
import cache from "memory-cache";
import { httpProxy } from "utils/proxy/http"; import { httpProxy } from "utils/proxy/http";
import { formatApiCall } from "utils/proxy/api-helpers"; import { formatApiCall } from "utils/proxy/api-helpers";
import getServiceWidget from "utils/config/service-helpers"; import getServiceWidget from "utils/config/service-helpers";
@ -7,7 +5,6 @@ import createLogger from "utils/logger";
import widgets from "widgets/widgets"; import widgets from "widgets/widgets";
const proxyName = "watchtowerProxyHandler"; const proxyName = "watchtowerProxyHandler";
const headerCacheKey = `${proxyName}__headers`;
const logger = createLogger(proxyName); const logger = createLogger(proxyName);
export default async function watchtowerProxyHandler(req, res) { 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" }); 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 url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
const method = "GET"
const [status, contentType, data] = await httpProxy(url, { const [status, contentType, data] = await httpProxy(url, {
method, method: "GET",
headers, 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 cleanData = data.toString().split("\n").filter(s => s.startsWith("watchtower"))
const jsonRes={} const jsonRes = {}
cleanData.map(e => e.split(" ")).forEach(strArray => { cleanData.map(e => e.split(" ")).forEach(strArray => {
const [key, value] = strArray const [key, value] = strArray
jsonRes[key] = value 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); if (contentType) res.setHeader("Content-Type", contentType);
return res.status(status).send(jsonRes); return res.status(status).send(jsonRes);
} }