2023-06-22 13:02:48 +00:00
|
|
|
import { getHttpBaseExternal, getHttpBaseInternal } from "@utils/env";
|
2023-06-22 11:42:57 +00:00
|
|
|
import fetch from "cross-fetch";
|
2023-06-21 11:55:27 +00:00
|
|
|
import type { Request, Response } from "express";
|
|
|
|
import { LemmyHttp } from "lemmy-js-client";
|
|
|
|
import { wrapClient } from "../../shared/services/HttpService";
|
|
|
|
import generateManifestJson from "../utils/generate-manifest-json";
|
|
|
|
import { setForwardedHeaders } from "../utils/set-forwarded-headers";
|
|
|
|
|
|
|
|
let manifest: Awaited<ReturnType<typeof generateManifestJson>> | undefined =
|
|
|
|
undefined;
|
|
|
|
|
|
|
|
export default async (req: Request, res: Response) => {
|
2023-06-22 02:45:21 +00:00
|
|
|
if (!manifest || manifest.start_url !== getHttpBaseExternal()) {
|
2023-06-21 11:55:27 +00:00
|
|
|
const headers = setForwardedHeaders(req.headers);
|
2023-06-21 19:08:21 +00:00
|
|
|
const client = wrapClient(
|
2023-07-28 20:15:42 +00:00
|
|
|
new LemmyHttp(getHttpBaseInternal(), { fetchFunction: fetch, headers }),
|
2023-06-21 19:08:21 +00:00
|
|
|
);
|
2023-06-21 11:55:27 +00:00
|
|
|
const site = await client.getSite({});
|
|
|
|
|
|
|
|
if (site.state === "success") {
|
|
|
|
manifest = await generateManifestJson(site.data);
|
|
|
|
} else {
|
|
|
|
res.sendStatus(500);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res.setHeader("content-type", "application/manifest+json");
|
|
|
|
|
|
|
|
res.send(manifest);
|
|
|
|
};
|