mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
add convex
This commit is contained in:
72
apps/cli/templates/backend/server/elysia/src/index.ts.hbs
Normal file
72
apps/cli/templates/backend/server/elysia/src/index.ts.hbs
Normal file
@@ -0,0 +1,72 @@
|
||||
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 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 auth}}
|
||||
allowedHeaders: ["Content-Type", "Authorization"],
|
||||
credentials: true,
|
||||
{{/if}}
|
||||
}),
|
||||
)
|
||||
{{#if 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");
|
||||
});
|
||||
Reference in New Issue
Block a user