feat: 配置 Vercel serverless 部署支持

- 更新 vercel.json 使用 serverless 函数入口
- 添加 api/index.ts 作为 Vercel serverless handler
- 添加 .env.vercel 环境变量配置模板
- 支持 Swagger 文档在 serverless 环境运行

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
golc 2026-03-04 09:19:00 +00:00
parent c505421544
commit 1d8213f6ed
3 changed files with 86 additions and 2 deletions

21
.env.vercel Normal file
View File

@ -0,0 +1,21 @@
# 数据库配置(需要在 Vercel Dashboard 中配置)
SERVER_HOST=your-mysql-host
SERVER_USER=your-mysql-user
PASSWORD=your-mysql-password
DATABASE=auth_db
# JWT 配置
JWT_SECRET=your-jwt-secret-key
JWT_EXPIRES_IN=7d
# OAuth 配置
GITHUB_CLIENT_ID=your-github-client-id
GITHUB_CLIENT_SECRET=your-github-client-secret
GITHUB_CALLBACK_URL=https://tradingadvbackend.vercel.app/auth/github/callback
LARK_APP_ID=your-lark-app-id
LARK_APP_SECRET=your-lark-app-secret
LARK_CALLBACK_URL=https://tradingadvbackend.vercel.app/auth/lark/callback
# 前端回调地址
FRONTEND_URL=https://tda.lcdd.net

63
api/index.ts Normal file
View File

@ -0,0 +1,63 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from '../src/app.module';
import { HttpExceptionFilter } from '../src/common/filters/http-exception.filter';
import { TransformInterceptor } from '../src/common/interceptors/transform.interceptor';
import { ValidationPipe } from '@nestjs/common';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import * as bodyParser from 'body-parser';
let cachedApp;
async function bootstrap() {
if (!cachedApp) {
const app = await NestFactory.create(AppModule);
app.enableCors({
origin: true,
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
allowedHeaders: [
'Content-Type',
'Authorization',
'Accept',
'Origin',
'Referer',
'User-Agent',
],
});
app.useGlobalFilters(new HttpExceptionFilter());
app.useGlobalInterceptors(new TransformInterceptor());
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
transform: true,
}),
);
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
// Swagger 配置
const config = new DocumentBuilder()
.setTitle('我的应用 API')
.setDescription('这是我的应用程序的 API 文档')
.setVersion('1.0')
.addBearerAuth()
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);
await app.init();
cachedApp = app;
}
return cachedApp;
}
export default async (req, res) => {
const app = await bootstrap();
const server = app.getHttpAdapter().getInstance();
return server(req, res);
};

View File

@ -2,14 +2,14 @@
"version": 2, "version": 2,
"builds": [ "builds": [
{ {
"src": "dist/main.js", "src": "api/index.ts",
"use": "@vercel/node" "use": "@vercel/node"
} }
], ],
"routes": [ "routes": [
{ {
"src": "/(.*)", "src": "/(.*)",
"dest": "dist/main.js" "dest": "api/index.ts"
} }
], ],
"buildCommand": "npx nest build", "buildCommand": "npx nest build",