main.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { NestFactory } from '@nestjs/core';
  2. import { AppModule } from './app.module';
  3. import { ValidationPipe } from '@nestjs/common';
  4. import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
  5. async function bootstrap() {
  6. const app = await NestFactory.create(AppModule);
  7. app.useGlobalPipes(new ValidationPipe());
  8. app.enableCors({
  9. origin: true, // Allow all origins
  10. methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
  11. credentials: true,
  12. });
  13. app.setGlobalPrefix('api'); // Set a global API prefix
  14. // Swagger / OpenAPI documentation
  15. const config = new DocumentBuilder()
  16. .setTitle('AuraK API')
  17. .setDescription(`
  18. ## AuraK 知识库平台 API 文档
  19. ### 认证方式
  20. 本系统支持两种认证方式:
  21. #### 1. API Key (推荐用于外部系统)
  22. | Header | 说明 | 必需 |
  23. |--------|------|------|
  24. | \`x-api-key\` | API Key值 (格式: kb_xxx) | ✅ 必需 |
  25. | \`x-tenant-id\` | 租户ID (用户有多租户时必填) | 可选 |
  26. 或使用 Authorization 头:
  27. \`Authorization: Bearer kb_xxx\`
  28. #### 2. JWT Token (用于前端应用)
  29. \`Authorization: Bearer jwt_token\`
  30. ### API分类
  31. - **开放API (V1)**: \`/api/v1/*\` - 支持API Key认证,用于外部系统集成
  32. - **内部API**: \`/api/*\` - 支持JWT认证,用于前端应用
  33. ### 主要功能模块
  34. - 知识库管理 (Knowledge Base)
  35. - RAG聊天 (Chat with RAG)
  36. - 模型配置 (Model Configuration)
  37. - 用户管理 (User Management)
  38. - 租户管理 (Tenant Management)
  39. - 知识分组 (Knowledge Groups)
  40. `)
  41. .setVersion('2.0')
  42. .addApiKey({ type: 'apiKey', name: 'x-api-key', in: 'header' }, 'x-api-key')
  43. .addBearerAuth(
  44. {
  45. type: 'http',
  46. scheme: 'bearer',
  47. bearerFormat: 'JWT',
  48. name: 'JWT',
  49. description: '输入 JWT token',
  50. in: 'header',
  51. },
  52. 'JWT',
  53. )
  54. .addTag('API v1', '开放API接口 - 使用API Key认证')
  55. .addTag('Auth', '认证接口')
  56. .addTag('Users', '用户管理')
  57. .addTag('Chat', '聊天接口')
  58. .addTag('Knowledge Base', '知识库管理')
  59. .addTag('Knowledge Groups', '知识分组')
  60. .addTag('Models', '模型配置')
  61. .addTag('Tenants', '租户管理')
  62. .build();
  63. const document = SwaggerModule.createDocument(app, config);
  64. SwaggerModule.setup('api/docs', app, document);
  65. await app.listen(process.env.PORT ?? 3001);
  66. // Ensure "Default" tenant exists
  67. const { TenantService } = await import('./tenant/tenant.service');
  68. const tenantService = app.get(TenantService);
  69. await tenantService.ensureDefaultTenant();
  70. }
  71. bootstrap();