1
0
Fork 0
mirror of https://github.com/kou029w/quot.git synced 2025-01-19 00:18:09 +00:00
quot/app/controllers/routes/views.ts

34 lines
808 B
TypeScript
Raw Normal View History

import type { FastifyInstance } from "fastify";
import httpProxy from "@fastify/http-proxy";
import esbuild from "esbuild";
async function views(fastify: FastifyInstance) {
const viewsDir = fastify.config.viewsDir;
const entryPoint = `${viewsDir}/index.ts`;
const { host, port } = await esbuild.serve(
{
host: "127.0.0.1",
servedir: viewsDir,
},
{
bundle: true,
minify: true,
entryPoints: [entryPoint],
define: {
"import.meta.env.QUOT_API_ENDPOINT": JSON.stringify(
fastify.config.apiEndpoint
),
},
}
);
await fastify.register(httpProxy, {
upstream: `http://${host}:${port}`,
async preHandler(req) {
if (!/\.(?:html|css|js)$/.test(req.url)) req.raw.url = "/";
},
});
}
export default views;