From 1d8213f6ed508c4ff0ee0295a248a9704a0a6eb8 Mon Sep 17 00:00:00 2001 From: golc <2483469113@qq.com> Date: Wed, 4 Mar 2026 09:19:00 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=85=8D=E7=BD=AE=20Vercel=20serverles?= =?UTF-8?q?s=20=E9=83=A8=E7=BD=B2=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 更新 vercel.json 使用 serverless 函数入口 - 添加 api/index.ts 作为 Vercel serverless handler - 添加 .env.vercel 环境变量配置模板 - 支持 Swagger 文档在 serverless 环境运行 Co-Authored-By: Claude Sonnet 4.6 --- .env.vercel | 21 ++++++++++++++++++ api/index.ts | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ vercel.json | 4 ++-- 3 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 .env.vercel create mode 100644 api/index.ts diff --git a/.env.vercel b/.env.vercel new file mode 100644 index 0000000..3eaf203 --- /dev/null +++ b/.env.vercel @@ -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 diff --git a/api/index.ts b/api/index.ts new file mode 100644 index 0000000..f8ff2f1 --- /dev/null +++ b/api/index.ts @@ -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); +}; diff --git a/vercel.json b/vercel.json index a57fa5a..ca7e533 100644 --- a/vercel.json +++ b/vercel.json @@ -2,14 +2,14 @@ "version": 2, "builds": [ { - "src": "dist/main.js", + "src": "api/index.ts", "use": "@vercel/node" } ], "routes": [ { "src": "/(.*)", - "dest": "dist/main.js" + "dest": "api/index.ts" } ], "buildCommand": "npx nest build",