| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- import { NestFactory } from '@nestjs/core';
- import { AppModule } from './app.module';
- import { ValidationPipe } from '@nestjs/common';
- import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
- async function bootstrap() {
- const app = await NestFactory.create(AppModule);
- app.useGlobalPipes(new ValidationPipe());
- app.enableCors({
- origin: true, // Allow all origins
- methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
- credentials: true,
- });
- app.setGlobalPrefix('api'); // Set a global API prefix
- // Swagger / OpenAPI documentation
- const config = new DocumentBuilder()
- .setTitle('AuraK API')
- .setDescription(`
- ## AuraK 知识库平台 API 文档
- ### 认证方式
- 本系统支持两种认证方式:
- #### 1. API Key (推荐用于外部系统)
- | Header | 说明 | 必需 |
- |--------|------|------|
- | \`x-api-key\` | API Key值 (格式: kb_xxx) | ✅ 必需 |
- | \`x-tenant-id\` | 租户ID (用户有多租户时必填) | 可选 |
- 或使用 Authorization 头:
- \`Authorization: Bearer kb_xxx\`
- #### 2. JWT Token (用于前端应用)
- \`Authorization: Bearer jwt_token\`
- ### API分类
- - **开放API (V1)**: \`/api/v1/*\` - 支持API Key认证,用于外部系统集成
- - **内部API**: \`/api/*\` - 支持JWT认证,用于前端应用
- ### 主要功能模块
- - 知识库管理 (Knowledge Base)
- - RAG聊天 (Chat with RAG)
- - 模型配置 (Model Configuration)
- - 用户管理 (User Management)
- - 租户管理 (Tenant Management)
- - 知识分组 (Knowledge Groups)
- `)
- .setVersion('2.0')
- .addApiKey({ type: 'apiKey', name: 'x-api-key', in: 'header' }, 'x-api-key')
- .addBearerAuth(
- {
- type: 'http',
- scheme: 'bearer',
- bearerFormat: 'JWT',
- name: 'JWT',
- description: '输入 JWT token',
- in: 'header',
- },
- 'JWT',
- )
- .addTag('API v1', '开放API接口 - 使用API Key认证')
- .addTag('Auth', '认证接口')
- .addTag('Users', '用户管理')
- .addTag('Chat', '聊天接口')
- .addTag('Knowledge Base', '知识库管理')
- .addTag('Knowledge Groups', '知识分组')
- .addTag('Models', '模型配置')
- .addTag('Tenants', '租户管理')
- .build();
- const document = SwaggerModule.createDocument(app, config);
- SwaggerModule.setup('api/docs', app, document);
- await app.listen(process.env.PORT ?? 3001);
- // Ensure "Default" tenant exists
- const { TenantService } = await import('./tenant/tenant.service');
- const tenantService = app.get(TenantService);
- await tenantService.ensureDefaultTenant();
- }
- bootstrap();
|