mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
73 lines
1.9 KiB
Handlebars
73 lines
1.9 KiB
Handlebars
import "dotenv/config";
|
|
{{#if (eq runtime "node")}}
|
|
import { node } from "@elysiajs/node";
|
|
{{/if}}
|
|
import { Elysia } from "elysia";
|
|
import { cors } from "@elysiajs/cors";
|
|
{{#if (eq api "trpc")}}
|
|
import { createContext } from "./lib/context";
|
|
import { appRouter } from "./routers/index";
|
|
import { fetchRequestHandler } from "@trpc/server/adapters/fetch";
|
|
{{/if}}
|
|
{{#if (eq api "orpc")}}
|
|
import { RPCHandler } from "@orpc/server/fetch";
|
|
import { appRouter } from "./routers";
|
|
import { createContext } from "./lib/context";
|
|
{{/if}}
|
|
{{#if (eq auth "better-auth")}}
|
|
import { auth } from "./lib/auth";
|
|
{{/if}}
|
|
|
|
{{#if (eq api "orpc")}}
|
|
const handler = new RPCHandler(appRouter);
|
|
{{/if}}
|
|
|
|
{{#if (eq runtime "node")}}
|
|
const app = new Elysia({ adapter: node() })
|
|
{{else}}
|
|
const app = new Elysia()
|
|
{{/if}}
|
|
.use(
|
|
cors({
|
|
origin: process.env.CORS_ORIGIN || "",
|
|
methods: ["GET", "POST", "OPTIONS"],
|
|
{{#if (eq auth "better-auth")}}
|
|
allowedHeaders: ["Content-Type", "Authorization"],
|
|
credentials: true,
|
|
{{/if}}
|
|
}),
|
|
)
|
|
{{#if (eq auth "better-auth")}}
|
|
.all("/api/auth/*", async (context) => {
|
|
const { request } = context;
|
|
if (["POST", "GET"].includes(request.method)) {
|
|
return auth.handler(request);
|
|
}
|
|
context.error(405);
|
|
})
|
|
{{/if}}
|
|
{{#if (eq api "orpc")}}
|
|
.all('/rpc*', async (context) => {
|
|
const { response } = await handler.handle(context.request, {
|
|
prefix: '/rpc',
|
|
context: await createContext({ context })
|
|
})
|
|
return response ?? new Response('Not Found', { status: 404 })
|
|
})
|
|
{{/if}}
|
|
{{#if (eq api "trpc")}}
|
|
.all("/trpc/*", async (context) => {
|
|
const res = await fetchRequestHandler({
|
|
endpoint: "/trpc",
|
|
router: appRouter,
|
|
req: context.request,
|
|
createContext: () => createContext({ context }),
|
|
});
|
|
return res;
|
|
})
|
|
{{/if}}
|
|
.get("/", () => "OK")
|
|
.listen(3000, () => {
|
|
console.log("Server is running on http://localhost:3000");
|
|
});
|