import express from "express";
import cookieParser from "cookie-parser";
import * as trpcExpress from "@trpc/server/adapters/express";
import { createServer as createViteServer } from "vite";
import { appRouter } from "../routers.js";
import { createContext } from "./context.js";
import { registrarRotasDeUpload } from "./uploads.js";
import { env } from "./env.js";

async function main() {
  const app = express();
  app.use(cookieParser());
  app.use(express.json());

  app.use("/api/trpc", trpcExpress.createExpressMiddleware({ router: appRouter, createContext }));
  registrarRotasDeUpload(app);

  const vite = await createViteServer({ server: { middlewareMode: true }, appType: "spa" });
  app.use(vite.middlewares);

  app.listen(env.porta, () => {
    console.log(`Biblioteca OnFide (dev) rodando na porta ${env.porta}`);
  });
}

main();
