mirror of
https://github.com/DI0IK/homepage-plus.git
synced 2025-07-17 18:29:48 +00:00
utils cleanup, initial static generation
This commit is contained in:
parent
ec8700f3e9
commit
e1a3a82f75
86 changed files with 279 additions and 261 deletions
|
@ -7,9 +7,9 @@ import "styles/theme.css";
|
|||
import "styles/manrope.css";
|
||||
import nextI18nextConfig from "../../next-i18next.config";
|
||||
|
||||
import { ColorProvider } from "utils/color-context";
|
||||
import { ThemeProvider } from "utils/theme-context";
|
||||
import { SettingsProvider } from "utils/settings-context";
|
||||
import { ColorProvider } from "utils/contexts/color";
|
||||
import { ThemeProvider } from "utils/contexts/theme";
|
||||
import { SettingsProvider } from "utils/contexts/settings";
|
||||
|
||||
function MyApp({ Component, pageProps }) {
|
||||
return (
|
||||
|
|
|
@ -1,25 +1,5 @@
|
|||
import { promises as fs } from "fs";
|
||||
import path from "path";
|
||||
|
||||
import yaml from "js-yaml";
|
||||
|
||||
import checkAndCopyConfig from "utils/config";
|
||||
import { bookmarksResponse } from "utils/config/api-response";
|
||||
|
||||
export default async function handler(req, res) {
|
||||
checkAndCopyConfig("bookmarks.yaml");
|
||||
|
||||
const bookmarksYaml = path.join(process.cwd(), "config", "bookmarks.yaml");
|
||||
const fileContents = await fs.readFile(bookmarksYaml, "utf8");
|
||||
const bookmarks = yaml.load(fileContents);
|
||||
|
||||
// map easy to write YAML objects into easy to consume JS arrays
|
||||
const bookmarksArray = bookmarks.map((group) => ({
|
||||
name: Object.keys(group)[0],
|
||||
bookmarks: group[Object.keys(group)[0]].map((entries) => ({
|
||||
name: Object.keys(entries)[0],
|
||||
...entries[Object.keys(entries)[0]][0],
|
||||
})),
|
||||
}));
|
||||
|
||||
res.send(bookmarksArray);
|
||||
res.send(await bookmarksResponse());
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ import https from "https";
|
|||
|
||||
import getRawBody from "raw-body";
|
||||
|
||||
import { httpRequest, httpsRequest } from "utils/http";
|
||||
import { httpRequest, httpsRequest } from "utils/proxy/http";
|
||||
|
||||
export const config = {
|
||||
api: {
|
||||
|
|
|
@ -1,43 +1,5 @@
|
|||
/* eslint-disable no-console */
|
||||
import { servicesFromConfig, servicesFromDocker, cleanServiceGroups } from "utils/service-helpers";
|
||||
import { servicesResponse } from "utils/config/api-response";
|
||||
|
||||
export default async function handler(req, res) {
|
||||
let discoveredServices;
|
||||
let configuredServices;
|
||||
|
||||
try {
|
||||
discoveredServices = cleanServiceGroups(await servicesFromDocker());
|
||||
} catch (e) {
|
||||
console.error("Failed to discover services, please check docker.yaml for errors or remove example entries.");
|
||||
console.error(e);
|
||||
discoveredServices = [];
|
||||
}
|
||||
|
||||
try {
|
||||
configuredServices = cleanServiceGroups(await servicesFromConfig());
|
||||
} catch (e) {
|
||||
console.error("Failed to load services.yaml, please check for errors");
|
||||
console.error(e);
|
||||
configuredServices = [];
|
||||
}
|
||||
|
||||
const mergedGroupsNames = [
|
||||
...new Set([discoveredServices.map((group) => group.name), configuredServices.map((group) => group.name)].flat()),
|
||||
];
|
||||
|
||||
const mergedGroups = [];
|
||||
|
||||
mergedGroupsNames.forEach((groupName) => {
|
||||
const discoveredGroup = discoveredServices.find((group) => group.name === groupName) || { services: [] };
|
||||
const configuredGroup = configuredServices.find((group) => group.name === groupName) || { services: [] };
|
||||
|
||||
const mergedGroup = {
|
||||
name: groupName,
|
||||
services: [...discoveredGroup.services, ...configuredGroup.services].filter((service) => service),
|
||||
};
|
||||
|
||||
mergedGroups.push(mergedGroup);
|
||||
});
|
||||
|
||||
res.send(mergedGroups);
|
||||
res.send(await servicesResponse());
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { formatApiCall } from "utils/api-helpers";
|
||||
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||
import createLogger from "utils/logger";
|
||||
import genericProxyHandler from "utils/proxies/generic";
|
||||
import genericProxyHandler from "utils/proxy/handlers/generic";
|
||||
import widgets from "widgets/widgets";
|
||||
|
||||
const logger = createLogger("servicesProxy");
|
||||
|
|
|
@ -1,22 +1,5 @@
|
|||
import { promises as fs } from "fs";
|
||||
import path from "path";
|
||||
|
||||
import yaml from "js-yaml";
|
||||
|
||||
import checkAndCopyConfig from "utils/config";
|
||||
import { widgetsResponse } from "utils/config/api-response";
|
||||
|
||||
export default async function handler(req, res) {
|
||||
checkAndCopyConfig("widgets.yaml");
|
||||
|
||||
const widgetsYaml = path.join(process.cwd(), "config", "widgets.yaml");
|
||||
const fileContents = await fs.readFile(widgetsYaml, "utf8");
|
||||
const widgets = yaml.load(fileContents);
|
||||
|
||||
// map easy to write YAML objects into easy to consume JS arrays
|
||||
const widgetsArray = widgets.map((group) => ({
|
||||
type: Object.keys(group)[0],
|
||||
options: { ...group[Object.keys(group)[0]] },
|
||||
}));
|
||||
|
||||
res.send(widgetsArray);
|
||||
res.send(await widgetsResponse());
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import cachedFetch from "utils/cached-fetch";
|
||||
import cachedFetch from "utils/proxy/cached-fetch";
|
||||
import { getSettings } from "utils/config";
|
||||
|
||||
export default async function handler(req, res) {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import cachedFetch from "utils/cached-fetch";
|
||||
import cachedFetch from "utils/proxy/cached-fetch";
|
||||
import { getSettings } from "utils/config";
|
||||
|
||||
export default async function handler(req, res) {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/* eslint-disable react/no-array-index-key */
|
||||
import useSWR from "swr";
|
||||
import useSWR, { SWRConfig } from "swr";
|
||||
import Head from "next/head";
|
||||
import dynamic from "next/dynamic";
|
||||
import { useTranslation } from "next-i18next";
|
||||
|
@ -13,9 +13,10 @@ import Widget from "components/widget";
|
|||
import Revalidate from "components/revalidate";
|
||||
import createLogger from "utils/logger";
|
||||
import { getSettings } from "utils/config";
|
||||
import { ColorContext } from "utils/color-context";
|
||||
import { ThemeContext } from "utils/theme-context";
|
||||
import { SettingsContext } from "utils/settings-context";
|
||||
import { ColorContext } from "utils/contexts/color";
|
||||
import { ThemeContext } from "utils/contexts/theme";
|
||||
import { SettingsContext } from "utils/contexts/settings";
|
||||
import { bookmarksResponse, servicesResponse, widgetsResponse } from "utils/config/api-response";
|
||||
|
||||
const ThemeToggle = dynamic(() => import("components/theme-toggle"), {
|
||||
ssr: false,
|
||||
|
@ -37,9 +38,18 @@ export async function getStaticProps() {
|
|||
logger = createLogger("index");
|
||||
const { providers, ...settings } = getSettings();
|
||||
|
||||
const services = await servicesResponse();
|
||||
const bookmarks = await bookmarksResponse();
|
||||
const widgets = await widgetsResponse();
|
||||
|
||||
return {
|
||||
props: {
|
||||
initialSettings: settings,
|
||||
fallback: {
|
||||
"/api/services": services,
|
||||
"/api/bookmarks": bookmarks,
|
||||
"/api/widgets": widgets,
|
||||
},
|
||||
...(await serverSideTranslations(settings.language ?? "en")),
|
||||
},
|
||||
};
|
||||
|
@ -56,7 +66,7 @@ export async function getStaticProps() {
|
|||
}
|
||||
}
|
||||
|
||||
export default function Index({ initialSettings }) {
|
||||
export default function Index({ initialSettings, fallback }) {
|
||||
const { data: errorsData } = useSWR("/api/validate");
|
||||
|
||||
if (errorsData && errorsData.length > 0) {
|
||||
|
@ -83,7 +93,11 @@ export default function Index({ initialSettings }) {
|
|||
);
|
||||
}
|
||||
|
||||
return <Home initialSettings={initialSettings} />;
|
||||
return (
|
||||
<SWRConfig value={{ fallback, fetcher: (resource, init) => fetch(resource, init).then((res) => res.json()) }}>
|
||||
<Home initialSettings={initialSettings} />
|
||||
</SWRConfig>
|
||||
);
|
||||
}
|
||||
|
||||
function Home({ initialSettings }) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue