2023-06-21 11:55:27 +00:00
|
|
|
import type { Request, Response } from "express";
|
|
|
|
import { LemmyHttp } from "lemmy-js-client";
|
2023-06-22 02:45:21 +00:00
|
|
|
import { getHttpBaseExternal, getHttpBaseInternal } from "../../shared/env";
|
2023-06-21 11:55:27 +00:00
|
|
|
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(
|
|
|
|
new LemmyHttp(getHttpBaseInternal(), { fetchFunction: fetch, headers })
|
|
|
|
);
|
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);
|
|
|
|
};
|