add cloudflare workers support (#326)

This commit is contained in:
Aman Varshney
2025-06-16 22:55:26 +05:30
committed by GitHub
parent 5fc1ba164e
commit b34e94a09e
34 changed files with 556 additions and 538 deletions

View File

@@ -14,21 +14,22 @@ export const auth = betterAuth({
{{#if (eq database "mongodb")}}provider: "mongodb"{{/if}}
}),
trustedOrigins: [
process.env.CORS_ORIGIN || "",{{#if (or (includes frontend "native-nativewind") (includes frontend "native-unistyles"))}}
"my-better-t-app://",{{/if}}
process.env.CORS_ORIGIN || "",
{{#if (or (includes frontend "native-nativewind") (includes frontend "native-unistyles"))}}
"my-better-t-app://",
{{/if}}
],
emailAndPassword: {
enabled: true,
}
{{#if (or (includes frontend "native-nativewind") (includes frontend "native-unistyles"))}}
,
plugins: [expo()]
, plugins: [expo()]
{{/if}}
});
{{/if}}
{{#if (eq orm "drizzle")}}
{{#if (or (eq runtime "bun") (eq runtime "node"))}}
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
{{#if (or (includes frontend "native-nativewind") (includes frontend "native-unistyles"))}}
@@ -45,20 +46,52 @@ export const auth = betterAuth({
schema: schema,
}),
trustedOrigins: [
process.env.CORS_ORIGIN || "",{{#if (or (includes frontend "native-nativewind") (includes frontend "native-unistyles"))}}
"my-better-t-app://",{{/if}}
process.env.CORS_ORIGIN || "",
{{#if (or (includes frontend "native-nativewind") (includes frontend "native-unistyles"))}}
"my-better-t-app://",
{{/if}}
],
emailAndPassword: {
enabled: true,
}
},
secret: process.env.BETTER_AUTH_SECRET,
baseURL: process.env.BETTER_AUTH_URL,
{{#if (or (includes frontend "native-nativewind") (includes frontend "native-unistyles"))}}
,
plugins: [expo()]
plugins: [expo()],
{{/if}}
});
{{/if}}
{{#if (eq runtime "workers")}}
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
{{#if (or (includes frontend "native-nativewind") (includes frontend "native-unistyles"))}}
import { expo } from "@better-auth/expo";
{{/if}}
import { db } from "@/db";
import * as schema from "../db/schema/auth";
import { env } from "cloudflare:workers";
export const auth = betterAuth({
database: drizzleAdapter(db, {
{{#if (eq database "postgres")}}provider: "pg",{{/if}}
{{#if (eq database "sqlite")}}provider: "sqlite",{{/if}}
{{#if (eq database "mysql")}}provider: "mysql",{{/if}}
schema: schema,
}),
trustedOrigins: [env.CORS_ORIGIN],
emailAndPassword: {
enabled: true,
},
secret: env.BETTER_AUTH_SECRET,
baseURL: env.BETTER_AUTH_URL,
{{#if (or (includes frontend "native-nativewind") (includes frontend "native-unistyles"))}}
plugins: [expo()],
{{/if}}
});
{{/if}}
{{/if}}
{{#if (eq orm "mongoose")}}
import { betterAuth } from "better-auth";
import { mongodbAdapter } from "better-auth/adapters/mongodb";
@@ -70,16 +103,16 @@ import { client } from "../db";
export const auth = betterAuth({
database: mongodbAdapter(client),
trustedOrigins: [
process.env.CORS_ORIGIN || "",{{#if (or (includes frontend "native-nativewind") (includes frontend "native-unistyles"))}}
"my-better-t-app://",{{/if}}
process.env.CORS_ORIGIN || "",
{{#if (or (includes frontend "native-nativewind") (includes frontend "native-unistyles"))}}
"my-better-t-app://",
{{/if}}
],
emailAndPassword: {
enabled: true,
}
{{#if (or (includes frontend "native-nativewind") (includes frontend "native-unistyles"))}}
,
plugins: [expo()]
, plugins: [expo()]
{{/if}}
});
{{/if}}
@@ -93,16 +126,16 @@ import { expo } from "@better-auth/expo";
export const auth = betterAuth({
database: "", // Invalid configuration
trustedOrigins: [
process.env.CORS_ORIGIN || "",{{#if (or (includes frontend "native-nativewind") (includes frontend "native-unistyles"))}}
"my-better-t-app://",{{/if}}
process.env.CORS_ORIGIN || "",
{{#if (or (includes frontend "native-nativewind") (includes frontend "native-unistyles"))}}
"my-better-t-app://",
{{/if}}
],
emailAndPassword: {
enabled: true,
}
{{#if (or (includes frontend "native-nativewind") (includes frontend "native-unistyles"))}}
,
plugins: [expo()]
, plugins: [expo()]
{{/if}}
});
{{/if}}

View File

@@ -1,4 +1,9 @@
{{#if (or (eq runtime "bun") (eq runtime "node"))}}
import "dotenv/config";
{{/if}}
{{#if (eq runtime "workers")}}
import { env } from "cloudflare:workers";
{{/if}}
{{#if (eq api "orpc")}}
import { RPCHandler } from "@orpc/server/fetch";
import { createContext } from "./lib/context";
@@ -15,26 +20,33 @@ import { auth } from "./lib/auth";
import { Hono } from "hono";
import { cors } from "hono/cors";
import { logger } from "hono/logger";
{{#if (includes examples "ai")}}
{{#if (and (includes examples "ai") (or (eq runtime "bun") (eq runtime "node")))}}
import { streamText } from "ai";
import { google } from "@ai-sdk/google";
import { stream } from "hono/streaming";
{{/if}}
{{#if (and (includes examples "ai") (eq runtime "workers"))}}
import { streamText } from "ai";
import { stream } from "hono/streaming";
import { createGoogleGenerativeAI } from "@ai-sdk/google";
{{/if}}
const app = new Hono();
app.use(logger());
app.use(
"/*",
cors({
origin: process.env.CORS_ORIGIN || "",
allowMethods: ["GET", "POST", "OPTIONS"],
{{#if auth}}
allowHeaders: ["Content-Type", "Authorization"],
credentials: true,
{{/if}}
})
);
app.use("/*", cors({
{{#if (or (eq runtime "bun") (eq runtime "node"))}}
origin: process.env.CORS_ORIGIN || "",
{{/if}}
{{#if (eq runtime "workers")}}
origin: env.CORS_ORIGIN || "",
{{/if}}
allowMethods: ["GET", "POST", "OPTIONS"],
{{#if auth}}
allowHeaders: ["Content-Type", "Authorization"],
credentials: true,
{{/if}}
}));
{{#if auth}}
app.on(["POST", "GET"], "/api/auth/**", (c) => auth.handler(c.req.raw));
@@ -48,6 +60,7 @@ app.use("/rpc/*", async (c, next) => {
prefix: "/rpc",
context: context,
});
if (matched) {
return c.newResponse(response.body, response);
}
@@ -64,11 +77,10 @@ app.use("/trpc/*", trpcServer({
}));
{{/if}}
{{#if (includes examples "ai")}}
{{#if (and (includes examples "ai") (or (eq runtime "bun") (eq runtime "node")))}}
app.post("/ai", async (c) => {
const body = await c.req.json();
const messages = body.messages || [];
const result = streamText({
model: google("gemini-1.5-flash"),
messages,
@@ -76,7 +88,24 @@ app.post("/ai", async (c) => {
c.header("X-Vercel-AI-Data-Stream", "v1");
c.header("Content-Type", "text/plain; charset=utf-8");
return stream(c, (stream) => stream.pipe(result.toDataStream()));
});
{{/if}}
{{#if (and (includes examples "ai") (eq runtime "workers"))}}
app.post("/ai", async (c) => {
const body = await c.req.json();
const messages = body.messages || [];
const google = createGoogleGenerativeAI({
apiKey: env.GOOGLE_GENERATIVE_AI_API_KEY,
});
const result = streamText({
model: google("gemini-1.5-flash"),
messages,
});
c.header("X-Vercel-AI-Data-Stream", "v1");
c.header("Content-Type", "text/plain; charset=utf-8");
return stream(c, (stream) => stream.pipe(result.toDataStream()));
});
{{/if}}
@@ -95,5 +124,10 @@ serve({
console.log(`Server is running on http://localhost:${info.port}`);
});
{{else}}
{{#if (eq runtime "bun")}}
export default app;
{{/if}}
{{#if (eq runtime "workers")}}
export default app;
{{/if}}
{{/if}}

View File

@@ -1,35 +1,39 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "bundler",
"verbatimModuleSyntax": true,
"strict": true,
"skipLibCheck": true,
"baseUrl": "./",
"paths": {
"@/*": ["./src/*"]
{{#if (eq orm 'prisma')}},
"prisma": ["node_modules/prisma"]
{{/if}}
},
"outDir": "./dist",
"types": [
{{#if (eq runtime 'node')}}
"node"
{{else if (eq runtime 'bun')}}
"bun"
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "bundler",
"verbatimModuleSyntax": true,
"strict": true,
"skipLibCheck": true,
"baseUrl": "./",
"paths": {
"@/*": ["./src/*"]
{{#if (eq orm "prisma")}},
"prisma": ["node_modules/prisma"]
{{/if}}
},
"outDir": "./dist",
"types": [
{{#if (eq runtime "node")}}
"node"
{{else if (eq runtime "bun")}}
"bun"
{{else if (eq runtime "workers")}}
"./worker-configuration",
"node"
{{else}}
"node", "bun"
"node",
"bun"
{{/if}}
],
{{#unless (or (eq backend "convex") (eq backend "none"))}}
"composite": true,
{{/unless}}
"jsx": "react-jsx"{{#if (eq backend 'hono')}},
"jsxImportSource": "hono/jsx"{{/if}}
},
"tsc-alias": {
"resolveFullPaths": true
}
{{#unless (or (eq backend "convex") (eq backend "none"))}}
"composite": true,
{{/unless}}
"jsx": "react-jsx"{{#if (eq backend "hono")}},
"jsxImportSource": "hono/jsx"{{/if}}
},
"tsc-alias": {
"resolveFullPaths": true
}
}

View File

@@ -1,3 +0,0 @@
import { drizzle } from "drizzle-orm/mysql2";
export const db = drizzle({ connection: { uri: process.env.DATABASE_URL } });

View File

@@ -0,0 +1,20 @@
{{#if (or (eq runtime "bun") (eq runtime "node"))}}
import { drizzle } from "drizzle-orm/mysql2";
export const db = drizzle({
connection: {
uri: process.env.DATABASE_URL,
},
});
{{/if}}
{{#if (eq runtime "workers")}}
import { drizzle } from "drizzle-orm/mysql2";
import { env } from "cloudflare:workers";
export const db = drizzle({
connection: {
uri: env.DATABASE_URL,
},
});
{{/if}}

View File

@@ -1,3 +0,0 @@
import { drizzle } from "drizzle-orm/node-postgres";
export const db = drizzle(process.env.DATABASE_URL || "");

View File

@@ -0,0 +1,12 @@
{{#if (or (eq runtime "bun") (eq runtime "node"))}}
import { drizzle } from "drizzle-orm/node-postgres";
export const db = drizzle(process.env.DATABASE_URL || "");
{{/if}}
{{#if (eq runtime "workers")}}
import { drizzle } from "drizzle-orm/node-postgres";
import { env } from "cloudflare:workers";
export const db = drizzle(env.DATABASE_URL || "");
{{/if}}

View File

@@ -6,6 +6,8 @@ export default defineConfig({
dialect: "turso",
dbCredentials: {
url: process.env.DATABASE_URL || "",
{{#if (eq dbSetup "turso")}}
authToken: process.env.DATABASE_AUTH_TOKEN,
{{/if}}
},
});

View File

@@ -1,9 +0,0 @@
import { drizzle } from "drizzle-orm/libsql";
import { createClient } from "@libsql/client";
const client = createClient({
url: process.env.DATABASE_URL || "",
authToken: process.env.DATABASE_AUTH_TOKEN ,
});
export const db = drizzle({ client });

View File

@@ -0,0 +1,28 @@
{{#if (or (eq runtime "bun") (eq runtime "node"))}}
import { drizzle } from "drizzle-orm/libsql";
import { createClient } from "@libsql/client";
const client = createClient({
url: process.env.DATABASE_URL || "",
{{#if (eq dbSetup "turso")}}
authToken: process.env.DATABASE_AUTH_TOKEN,
{{/if}}
});
export const db = drizzle({ client });
{{/if}}
{{#if (eq runtime "workers")}}
import { drizzle } from "drizzle-orm/libsql";
import { env } from "cloudflare:workers";
import { createClient } from "@libsql/client";
const client = createClient({
url: env.DATABASE_URL || "",
{{#if (eq dbSetup "turso")}}
authToken: env.DATABASE_AUTH_TOKEN,
{{/if}}
});
export const db = drizzle({ client });
{{/if}}

View File

@@ -1,4 +1,8 @@
{{#if (eq dbSetup "prisma-postgres")}}
// import "dotenv/config"; uncomment this to load .env
{{else}}
import "dotenv/config";
{{/if}}
import path from "node:path";
import type { PrismaConfig } from "prisma";

View File

@@ -0,0 +1,18 @@
name = "{{projectName}}-server"
main = "src/index.ts"
compatibility_date = "2025-06-15"
compatibility_flags = ["nodejs_compat"]
[vars]
NODE_ENV = "production"
# Non-sensitive environment variables (visible in dashboard)
# CORS_ORIGIN = "https://your-frontend-domain.com"
# BETTER_AUTH_URL = "https://your-worker-domain.workers.dev"
# ⚠️ SENSITIVE DATA: Use `wrangler secret put` instead of adding here
# Don't put these in [vars] - they'll be visible in the dashboard!
# - DATABASE_URL
# - DATABASE_AUTH_TOKEN
# - GOOGLE_GENERATIVE_AI_API_KEY
# - BETTER_AUTH_SECRET