mirror of
https://github.com/DI0IK/homepage-plus.git
synced 2025-07-12 16:08:48 +00:00
Merge branch 'main' into kubernetes
This commit is contained in:
commit
a1f2003a77
108 changed files with 619 additions and 168 deletions
|
@ -4,7 +4,7 @@ import path from "path";
|
|||
|
||||
import yaml from "js-yaml";
|
||||
|
||||
import checkAndCopyConfig from "utils/config/config";
|
||||
import checkAndCopyConfig, { getSettings } from "utils/config/config";
|
||||
import {
|
||||
servicesFromConfig,
|
||||
servicesFromDocker,
|
||||
|
@ -52,6 +52,7 @@ export async function servicesResponse() {
|
|||
let discoveredDockerServices;
|
||||
let discoveredKubernetesServices;
|
||||
let configuredServices;
|
||||
let initialSettings;
|
||||
|
||||
try {
|
||||
discoveredDockerServices = cleanServiceGroups(await servicesFromDocker());
|
||||
|
@ -77,6 +78,14 @@ export async function servicesResponse() {
|
|||
configuredServices = [];
|
||||
}
|
||||
|
||||
try {
|
||||
initialSettings = await getSettings();
|
||||
} catch (e) {
|
||||
console.error("Failed to load settings.yaml, please check for errors");
|
||||
if (e) console.error(e);
|
||||
initialSettings = {};
|
||||
}
|
||||
|
||||
const mergedGroupsNames = [
|
||||
...new Set([
|
||||
discoveredDockerServices.map((group) => group.name),
|
||||
|
@ -86,6 +95,7 @@ export async function servicesResponse() {
|
|||
];
|
||||
|
||||
const mergedGroups = [];
|
||||
const definedLayouts = initialSettings.layout ? Object.keys(initialSettings.layout) : null;
|
||||
|
||||
mergedGroupsNames.forEach((groupName) => {
|
||||
const discoveredDockerGroup = discoveredDockerServices.find((group) => group.name === groupName) || { services: [] };
|
||||
|
@ -101,7 +111,13 @@ export async function servicesResponse() {
|
|||
].filter((service) => service),
|
||||
};
|
||||
|
||||
mergedGroups.push(mergedGroup);
|
||||
if (definedLayouts) {
|
||||
const layoutIndex = definedLayouts.findIndex(layout => layout === mergedGroup.name);
|
||||
if (layoutIndex > -1) mergedGroups.splice(layoutIndex, 0, mergedGroup);
|
||||
else mergedGroups.push(mergedGroup);
|
||||
} else {
|
||||
mergedGroups.push(mergedGroup);
|
||||
}
|
||||
});
|
||||
|
||||
return mergedGroups;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import getServiceWidget from "utils/config/service-helpers";
|
||||
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||
import validateWidgetData from "utils/proxy/validate-widget-data";
|
||||
import { httpProxy } from "utils/proxy/http";
|
||||
import createLogger from "utils/logger";
|
||||
import widgets from "widgets/widgets";
|
||||
|
@ -51,7 +52,11 @@ export default async function credentialedProxyHandler(req, res) {
|
|||
}
|
||||
|
||||
if (status >= 400) {
|
||||
logger.debug("HTTP Error %d calling %s//%s%s...", status, url.protocol, url.hostname, url.pathname);
|
||||
logger.error("HTTP Error %d calling %s", status, url.toString());
|
||||
}
|
||||
|
||||
if (!validateWidgetData(widget, endpoint, data)) {
|
||||
return res.status(500).json({error: {message: "Invalid data", url, data}});
|
||||
}
|
||||
|
||||
if (contentType) res.setHeader("Content-Type", contentType);
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import getServiceWidget from "utils/config/service-helpers";
|
||||
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||
import validateWidgetData from "utils/proxy/validate-widget-data";
|
||||
import { httpProxy } from "utils/proxy/http";
|
||||
import createLogger from "utils/logger";
|
||||
import widgets from "widgets/widgets";
|
||||
|
@ -32,6 +33,11 @@ export default async function genericProxyHandler(req, res, map) {
|
|||
});
|
||||
|
||||
let resultData = data;
|
||||
|
||||
if (!validateWidgetData(widget, endpoint, resultData)) {
|
||||
return res.status(status).json({error: {message: "Invalid data", url, data: resultData}});
|
||||
}
|
||||
|
||||
if (status === 200 && map) {
|
||||
resultData = map(data);
|
||||
}
|
||||
|
@ -44,6 +50,7 @@ export default async function genericProxyHandler(req, res, map) {
|
|||
|
||||
if (status >= 400) {
|
||||
logger.debug("HTTP Error %d calling %s//%s%s...", status, url.protocol, url.hostname, url.pathname);
|
||||
return res.status(status).json({error: {message: "HTTP Error", url, data}});
|
||||
}
|
||||
|
||||
return res.status(status).send(resultData);
|
||||
|
|
|
@ -98,6 +98,6 @@ export async function httpProxy(url, params = {}) {
|
|||
catch (err) {
|
||||
logger.error("Error calling %s//%s%s...", url.protocol, url.hostname, url.pathname);
|
||||
logger.error(err);
|
||||
return [500, "application/json", { error: "Unexpected error" }, null];
|
||||
return [500, "application/json", { error: {message: err?.message ?? "Unknown error", url, rawError: err} }, null];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,5 +3,11 @@ import useSWR from "swr";
|
|||
import { formatProxyUrl } from "./api-helpers";
|
||||
|
||||
export default function useWidgetAPI(widget, ...options) {
|
||||
return useSWR(formatProxyUrl(widget, ...options));
|
||||
const config = {};
|
||||
if (options?.refreshInterval) {
|
||||
config.refreshInterval = options.refreshInterval;
|
||||
}
|
||||
const { data, error } = useSWR(formatProxyUrl(widget, ...options), config);
|
||||
// make the data error the top-level error
|
||||
return { data, error: data?.error ?? error }
|
||||
}
|
||||
|
|
22
src/utils/proxy/validate-widget-data.js
Normal file
22
src/utils/proxy/validate-widget-data.js
Normal file
|
@ -0,0 +1,22 @@
|
|||
import widgets from "widgets/widgets";
|
||||
|
||||
export default function validateWidgetData(widget, endpoint, data) {
|
||||
let valid = true;
|
||||
let dataParsed;
|
||||
try {
|
||||
dataParsed = JSON.parse(data);
|
||||
} catch (e) {
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (dataParsed && Object.entries(dataParsed).length) {
|
||||
const validate = widgets[widget.type]?.mappings?.[endpoint]?.validate;
|
||||
validate?.forEach(key => {
|
||||
if (dataParsed[key] === undefined) {
|
||||
valid = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue