Files
lezi-fun ec50efea89 feat: support serverless deployment (Vercel / Cloudflare Pages)
- Replace bcrypt (native) with bcryptjs (pure JS) — no native build on serverless
- Storage abstraction (lib/storage.js): local uploads/ for traditional deploy, S3-compatible (R2/MinIO/S3 via aws4fetch) for serverless
- DB pool auto-adapts to serverless (single connection, rotation, timeouts)
- next.config.js: standalone output only when DOCKER=1 (serverless skips it)
- Docs: serverless deployment section in README, S3 env vars in .env.example
2026-08-10 09:55:18 +08:00

26 lines
943 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// PostgreSQL 连接池单例 (Route Handlers 共享)
// - 传统部署:常规连接池
// - Serverless(Vercel 等):每个函数实例复用全局池,限制最大连接数
import { Pool } from 'pg';
const isServerless = !!process.env.VERCEL || !!process.env.AWS_LAMBDA_FUNCTION_NAME || !!process.env.CF_PAGES;
const pool = new Pool({
connectionString: process.env.DATABASE_URL || 'postgresql://forumlify:123456@localhost:5432/forumlify',
...(isServerless
? {
max: 1, // serverless 实例通常单并发
idleTimeoutMillis: 5000,
connectionTimeoutMillis: 10000,
maxUses: 7500, // 定期轮换连接,避免陈旧连接
}
: {}),
});
// 开发热重载时避免连接池泄漏
if (process.env.NODE_ENV !== 'production' && !globalThis.__forumlifyPool) {
globalThis.__forumlifyPool = pool;
}
export default isServerless ? pool : (globalThis.__forumlifyPool || pool);