// ------------------------------------------------------------ // Imports // ------------------------------------------------------------ import { Hono } from "hono"; import { connect } from "mongoose"; // Middlewares import { logger } from "hono/logger"; import { endpointEventGenerator } from "$lib/middlewares/endpointEventGenerator"; // ------------------------------------------------------------ // Database healthcheck and program initialization // ------------------------------------------------------------ // Check if MONGO_URI environment variable is set if (!process.env.MONGO_URI) throw new Error("Missing MONGO_URI environment variable"); // Check if USER_TOKEN environment variable is set if (!process.env.USER_TOKEN) throw new Error("Missing USER_TOKEN environment variable"); console.log("Checking database connection..."); // Check if database connection succeeds await connect(process.env.MONGO_URI) .then(() => { console.log("Database connection successful"); }) .catch((error) => { throw new Error(`Failed to connect to database: ${error.message}`); }); // Define new Hono instance and bind routes directory const app = new Hono() .use(logger()) .use(endpointEventGenerator) .route("/", (await import("./routes")).default); // ------------------------------------------------------------ // Exports // ------------------------------------------------------------ export default { fetch: app.fetch, port: process.env.PORT || 4300, };