manifold/og-image/api/index.ts
Austin Chen 4b2412c49c
Set up a custom OpenGraph image generator for social media previews (#20)
* Copy in og-image code

* Add "yarn start" command in lieu of vercel

* Don't require that images be sourced from vercel

* Load in Major Mono and Readex fonts

* Fix vercel config (?)

* Replace default image with Manifold's

* Add some brief instructions on getting started

* In the UI, use the default image

* Fix typescript errors

* More typescript fixing
2022-01-07 12:07:38 -08:00

37 lines
1.1 KiB
TypeScript

import { IncomingMessage, ServerResponse } from "http";
import { parseRequest } from "./_lib/parser";
import { getScreenshot } from "./_lib/chromium";
import { getHtml } from "./_lib/template";
const isDev = !process.env.AWS_REGION;
const isHtmlDebug = process.env.OG_HTML_DEBUG === "1";
export default async function handler(
req: IncomingMessage,
res: ServerResponse
) {
try {
const parsedReq = parseRequest(req);
const html = getHtml(parsedReq);
if (isHtmlDebug) {
res.setHeader("Content-Type", "text/html");
res.end(html);
return;
}
const { fileType } = parsedReq;
const file = await getScreenshot(html, fileType, isDev);
res.statusCode = 200;
res.setHeader("Content-Type", `image/${fileType}`);
res.setHeader(
"Cache-Control",
`public, immutable, no-transform, s-maxage=31536000, max-age=31536000`
);
res.end(file);
} catch (e) {
res.statusCode = 500;
res.setHeader("Content-Type", "text/html");
res.end("<h1>Internal Error</h1><p>Sorry, there was a problem</p>");
console.error(e);
}
}