import { readFile } from "node:fs/promises"; import { resolve } from "node:path"; import { serve } from "@hono/node-server"; import { serveStatic } from "@hono/node-server/serve-static"; import { Hono } from "hono"; import { getServerEnvironment } from "./env"; import { handleAgents24 } from "./handler"; try { process.loadEnvFile(".env.local"); } catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; } if (process.argv.includes("--production")) process.env.NODE_ENV = "production"; const env = getServerEnvironment(); const production = process.env.NODE_ENV === "production"; const app = new Hono(); app.use("*", async (context, next) => { await next(); context.header("Referrer-Policy", "no-referrer"); context.header("Content-Security-Policy", "frame-ancestors 'none'"); context.header("X-Content-Type-Options", "nosniff"); context.header("X-Frame-Options", "DENY"); }); app.all("/api/agents24", (context) => handleAgents24(context.req.raw)); app.all("/api/agents24/*", (context) => handleAgents24(context.req.raw)); if (production) { app.use("*", serveStatic({ root: "./dist" })); app.get("*", async (context) => context.html(await readFile(resolve("dist/index.html"), "utf8"))); } serve({ fetch: app.fetch, hostname: production ? "0.0.0.0" : "127.0.0.1", port: env.PORT }, (info) => { process.stdout.write(`Agents24 app listening on http://127.0.0.1:${info.port}\n`); });