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",