This commit is contained in:
2025-03-18 21:52:17 +08:00
parent d766e2ae6a
commit 4447fbb70e
5 changed files with 53 additions and 39 deletions

View File

@@ -1,39 +1,4 @@
import { serve } from "bun";
import { createBunServeHandler } from "trpc-bun-adapter";
import { appRouter } from "./router.ts"
serve({
port: 3001,
// hostname: "fpga.swordlost.com",
routes: {
// Static routes
"/api/status": new Response("OK"),
// Dynamic routes
"/users/:id": req => {
return new Response(`Hello User ${req.params.id}!`);
},
// Per-HTTP method handlers
"/api/posts": {
GET: () => new Response("List posts"),
POST: async req => {
const body = await req.json();
if (body !== null) {
return Response.json({ created: true, ...body });
} else {
return Response.json({})
}
},
},
// Wildcard route for all routes that start with "/api/" and aren't otherwise matched
"/api/*": Response.json({ message: "Not found" }, { status: 404 }),
// Serve a file by buffering it in memory
// "/favicon.ico": new Response(await Bun.file("../public/favicon.icon").bytes(), {
// headers: {
// "Content-Type": "image/x-icon",
// },
// }),
}
});
Bun.serve(createBunServeHandler({ appRouter }))

7
server/router.ts Normal file
View File

@@ -0,0 +1,7 @@
import { publicRouter, publicProcedure } from "./trpc.ts"
export const appRouter = publicRouter({
ping: publicProcedure.query(() => "pong"),
});
export type AppRouter = typeof appRouter;

6
server/trpc.ts Normal file
View File

@@ -0,0 +1,6 @@
import { initTRPC } from '@trpc/server';
const t = initTRPC.create();
export const publicRouter = t.router;
export const publicProcedure = t.procedure;