create-dp-koa 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -0
- package/index.mjs +97 -0
- package/package.json +33 -0
- package/template/.env.development +9 -0
- package/template/.env.production +12 -0
- package/template/.github/workflows/ci-cd.yml +182 -0
- package/template/.trae/documents/controller_development_plan.md +386 -0
- package/template/.trae/skills/00-backend-core.skill.md +50 -0
- package/template/.trae/skills/01-backend-skill-router.skill.md +55 -0
- package/template/.trae/skills/10-backend-api.skill.md +54 -0
- package/template/.trae/skills/11-backend-controller-recipes.skill.md +107 -0
- package/template/.trae/skills/20-backend-repository.skill.md +25 -0
- package/template/.trae/skills/21-backend-service.skill.md +135 -0
- package/template/.trae/skills/25-backend-comments-and-doc.skill.md +97 -0
- package/template/.trae/skills/30-backend-validation.skill.md +320 -0
- package/template/.trae/skills/40-backend-error-logging.skill.md +21 -0
- package/template/.trae/skills/50-backend-bootstrap-lifecycle.skill.md +90 -0
- package/template/.trae/skills/60-backend-router-registration.skill.md +71 -0
- package/template/.trae/skills/70-backend-middleware.skill.md +98 -0
- package/template/.trae/skills/80-backend-utils-and-libs.skill.md +90 -0
- package/template/.trae/skills/85-backend-plugins.rule.md +64 -0
- package/template/.trae/skills/90-backend-testing.skill.md +29 -0
- package/template/.trae/skills/README.md +49 -0
- package/template/.vscode/launch.json +38 -0
- package/template/.vscode/settings.json +1 -0
- package/template/Dockerfile +36 -0
- package/template/README.md +229 -0
- package/template/docker-compose.yml +135 -0
- package/template/docs/API_DOCUMENTATION.md +837 -0
- package/template/docs/ARCHITECTURE_REFACTOR.md +109 -0
- package/template/docs/CACHE_MIGRATION_GUIDE.md +142 -0
- package/template/docs/DEPLOYMENT_GUIDE.md +1062 -0
- package/template/docs/DEVELOPMENT_GUIDE.md +1097 -0
- package/template/docs/DOCUMENTATION_CLEANUP_REPORT.md +166 -0
- package/template/docs/DOCUMENTATION_COMPLETION_REPORT.md +223 -0
- package/template/docs/DOCUMENTATION_INDEX.md +294 -0
- package/template/docs/DOCUMENTATION_STRUCTURE.md +221 -0
- package/template/docs/ENTERPRISE_ANNOTATION_SYSTEM_GUIDE.md +2069 -0
- package/template/docs/ENTERPRISE_DATABASE_ARCHITECTURE.md +318 -0
- package/template/docs/ENTERPRISE_DEPLOYMENT_GUIDE.md +547 -0
- package/template/docs/ENTERPRISE_ERROR_HANDLING_GUIDE.md +357 -0
- package/template/docs/ENTERPRISE_LOGGING_SYSTEM_GUIDE.md +494 -0
- package/template/docs/ENVIRONMENT_CONFIG_EXAMPLE.md +69 -0
- package/template/docs/FINAL_IMPLEMENTATION_SUMMARY.md +206 -0
- package/template/docs/HEALTH_CHECK_ROUTE_FIX.md +134 -0
- package/template/docs/IMPLEMENTATION_CHECKLIST.md +204 -0
- package/template/docs/INSTALLATION_GUIDE.md +611 -0
- package/template/docs/INTERCEPTOR_TESTING_REPORT.md +226 -0
- package/template/docs/INTERCEPTOR_TESTING_SCRIPTS.md +143 -0
- package/template/docs/LOGGING_OPTIMIZATION_GUIDE.md +126 -0
- package/template/docs/MEMORY_DATABASE_GUIDE.md +212 -0
- package/template/docs/NEW_ROUTER_INTEGRATION_GUIDE.md +345 -0
- package/template/docs/NEW_ROUTER_INTEGRATION_SUMMARY.md +259 -0
- package/template/docs/NEW_ROUTER_USAGE_GUIDE.md +364 -0
- package/template/docs/QUICK_START.md +268 -0
- package/template/docs/ROUTE_SLASH_COMPATIBILITY_FIX.md +191 -0
- package/template/docs/SERVICE_INTERCEPTOR_GUIDE.md +243 -0
- package/template/docs/SERVICE_LAYER_INDEX.md +205 -0
- package/template/docs/SERVICE_PATTERN_GUIDE.md +270 -0
- package/template/docs/SERVICE_RETURN_VALUE_SPECIFICATION.md +466 -0
- package/template/docs/SWAGGER_DEBUG_MODE_GUIDE.md +80 -0
- package/template/docs/SWAGGER_INTEGRATION_GUIDE.md +416 -0
- package/template/docs/TRANSACTION_MANAGER_USAGE.md +360 -0
- package/template/docs/TROUBLESHOOTING.md +869 -0
- package/template/env.production.example +62 -0
- package/template/jest.config.js +34 -0
- package/template/package-lock.json +13240 -0
- package/template/package.json +119 -0
- package/template/patches/typeorm+0.3.25.patch +22 -0
- package/template/scripts/sync-template.mjs +84 -0
- package/template/scripts/test-annotation-system.sh +48 -0
- package/template/scripts/test-core-functionality.sh +28 -0
- package/template/src/annotations/decorators/ConfigManagement.ts +9 -0
- package/template/src/annotations/decorators/DistributedTracing.ts +9 -0
- package/template/src/annotations/decorators/EnterprisePerformance.ts +9 -0
- package/template/src/annotations/decorators/PerformanceMonitor.ts +32 -0
- package/template/src/annotations/decorators/SecurityAudit.ts +9 -0
- package/template/src/annotations/index.ts +50 -0
- package/template/src/annotations/processors/ConfigManagementProcessor.ts +369 -0
- package/template/src/annotations/processors/DistributedTracingProcessor.ts +288 -0
- package/template/src/annotations/processors/EnterprisePerformanceProcessor.ts +189 -0
- package/template/src/annotations/processors/PerformanceMonitorProcessor.ts +101 -0
- package/template/src/annotations/processors/SecurityAuditProcessor.ts +345 -0
- package/template/src/annotations/processors/SwaggerProcessor.ts +612 -0
- package/template/src/annotations/processors/index.ts +10 -0
- package/template/src/app.ts +123 -0
- package/template/src/controllers/base.controller.ts +41 -0
- package/template/src/controllers/cacheManagement.controller.ts +131 -0
- package/template/src/controllers/captcha.controller.ts +57 -0
- package/template/src/controllers/demo/AnnotationDemoController.ts +118 -0
- package/template/src/controllers/example/EnterpriseExampleController.ts +297 -0
- package/template/src/controllers/example/ExampleController.ts +110 -0
- package/template/src/controllers/example/NewAnnotationExampleController.ts +159 -0
- package/template/src/controllers/example/SwaggerExampleController.ts +205 -0
- package/template/src/controllers/example/TransactionExample.controller.ts +336 -0
- package/template/src/controllers/health.controller.ts +235 -0
- package/template/src/controllers/home/register.controller.ts +58 -0
- package/template/src/controllers/home/ytGoods.controller.ts +92 -0
- package/template/src/controllers/home/ytShop.controller.ts +135 -0
- package/template/src/controllers/home/ytUser.controller.ts +89 -0
- package/template/src/controllers/logManagement.controller.ts +396 -0
- package/template/src/controllers/public/emailSend.controller.ts +65 -0
- package/template/src/controllers/public/ytUserAuth.controller.ts +174 -0
- package/template/src/controllers/testData.controller.ts +253 -0
- package/template/src/dto/controller/example/NewAnnotationExampleController.dto.ts +73 -0
- package/template/src/dto/controller/home/emailSend.controller.dto.ts +40 -0
- package/template/src/dto/controller/home/register.controller.dto.ts +45 -0
- package/template/src/dto/controller/home/ytGoods.controller.dto.ts +55 -0
- package/template/src/dto/controller/home/ytShop.controller.dto.ts +69 -0
- package/template/src/dto/controller/home/ytUser.controller.dto.ts +44 -0
- package/template/src/dto/controller/public/ytUserAuth.controller.dto.ts +63 -0
- package/template/src/dto/goods.dto.ts +212 -0
- package/template/src/dto/service/ytService.dto.ts +13 -0
- package/template/src/dto/user.dto.ts +177 -0
- package/template/src/entity/base.entity.ts +13 -0
- package/template/src/entity/columnTypes.ts +13 -0
- package/template/src/entity/goodsImagesUnlockKey.entity.ts +33 -0
- package/template/src/entity/goodsUnlocker.entity.ts +34 -0
- package/template/src/entity/index.ts +15 -0
- package/template/src/entity/shop.entity.ts +52 -0
- package/template/src/entity/shopUser.entity.ts +41 -0
- package/template/src/entity/ytGoods.entity.ts +94 -0
- package/template/src/entity/ytUser.entity.ts +96 -0
- package/template/src/examples/InterceptorExampleRunner.ts +284 -0
- package/template/src/examples/ServiceInterceptorExample.ts +214 -0
- package/template/src/examples/SwaggerProcessorExample.ts +169 -0
- package/template/src/examples/TransactionManagerDemo.ts +377 -0
- package/template/src/examples/cacheExamples.ts +155 -0
- package/template/src/framework/decorator/controller.ts +311 -0
- package/template/src/framework/decorator/processor/AnnotationDecorators.ts +100 -0
- package/template/src/framework/decorator/processor/AnnotationProcessor.ts +156 -0
- package/template/src/framework/decorator/processor/AnnotationProcessorConfig.ts +45 -0
- package/template/src/framework/decorator/processor/AnnotationRegistry.ts +117 -0
- package/template/src/framework/decorator/processor/AnnotationSystemInitializer.ts +95 -0
- package/template/src/framework/decorator/processor/ProcessorManager.ts +76 -0
- package/template/src/framework/decorator/processor/processors/CustomProcessors.ts +126 -0
- package/template/src/framework/decorator/processor/processors/DefaultProcessors.ts +207 -0
- package/template/src/framework/decorator/refactored/DecoratorFactory.ts +99 -0
- package/template/src/framework/decorator/refactored/DecoratorMetadataManager.ts +125 -0
- package/template/src/framework/decorator/refactored/DecoratorValidator.ts +128 -0
- package/template/src/framework/decorator/refactored/TypeSafeDecorators.ts +139 -0
- package/template/src/framework/decorator/refactored/index.ts +98 -0
- package/template/src/framework/decorator/swagger.ts +150 -0
- package/template/src/framework/interceptors/AdvancedServiceCallInterceptor.ts +375 -0
- package/template/src/framework/interceptors/ServiceCallInterceptor.ts +348 -0
- package/template/src/framework/interceptors/index.ts +19 -0
- package/template/src/framework/plugins/registry.ts +63 -0
- package/template/src/framework/plugins/types.ts +15 -0
- package/template/src/framework/types/ServiceResult.ts +151 -0
- package/template/src/framework/types/index.ts +16 -0
- package/template/src/framework/utils/CacheManager.ts +430 -0
- package/template/src/framework/utils/CacheService.ts +248 -0
- package/template/src/framework/utils/DtoValidator.ts +164 -0
- package/template/src/framework/utils/MigrationHelper.ts +179 -0
- package/template/src/framework/utils/MigrationManager.ts +256 -0
- package/template/src/framework/utils/NewRouter.ts +207 -0
- package/template/src/framework/utils/TransactionManager.ts +172 -0
- package/template/src/framework/utils/bootstrap.ts +445 -0
- package/template/src/framework/utils/cache.ts +269 -0
- package/template/src/framework/utils/databaseConfig.ts +148 -0
- package/template/src/framework/utils/db.ts +39 -0
- package/template/src/framework/utils/dbMonitor.ts +106 -0
- package/template/src/framework/utils/dynamicSwagger.ts +410 -0
- package/template/src/framework/utils/function.ts +61 -0
- package/template/src/framework/utils/gracefulShutdown.ts +131 -0
- package/template/src/framework/utils/logger.ts +388 -0
- package/template/src/framework/utils/metrics.ts +182 -0
- package/template/src/framework/utils/router.ts +417 -0
- package/template/src/framework/utils/swagger.ts +184 -0
- package/template/src/framework/utils/testDb.ts +19 -0
- package/template/src/framework/utils/token.ts +23 -0
- package/template/src/framework/utils/transform.ts +17 -0
- package/template/src/libs/aokEmailSender.ts +42 -0
- package/template/src/libs/captcha.ts +37 -0
- package/template/src/libs/cos.ts +45 -0
- package/template/src/libs/mCache.ts +7 -0
- package/template/src/libs/serviceValidate.ts +3 -0
- package/template/src/libs/tecentSms.ts +51 -0
- package/template/src/middlewares/a.middleware.ts +6 -0
- package/template/src/middlewares/error.middleware.ts +14 -0
- package/template/src/middlewares/logging.middleware.ts +187 -0
- package/template/src/middlewares/static.middleware.ts +79 -0
- package/template/src/middlewares/swagger.middleware.ts +70 -0
- package/template/src/middlewares/token.middleware.ts +32 -0
- package/template/src/migrations/1700000000000-InitialDatabaseStructure.ts +172 -0
- package/template/src/migrations/index.ts +6 -0
- package/template/src/plugins/weboffice/core/context.ts +47 -0
- package/template/src/plugins/weboffice/core/errors.ts +51 -0
- package/template/src/plugins/weboffice/core/types.ts +63 -0
- package/template/src/plugins/weboffice/core/utils.ts +7 -0
- package/template/src/plugins/weboffice/entities/index.ts +3 -0
- package/template/src/plugins/weboffice/entities/webofficeFile.entity.ts +28 -0
- package/template/src/plugins/weboffice/entities/webofficeFileVersion.entity.ts +29 -0
- package/template/src/plugins/weboffice/http/routes.ts +179 -0
- package/template/src/plugins/weboffice/index.ts +23 -0
- package/template/src/plugins/weboffice/services/webofficeCallback.service.ts +274 -0
- package/template/src/repository/UserRepository.ts +122 -0
- package/template/src/repository/base/BaseRepository.ts +124 -0
- package/template/src/repository/interfaces/IBaseRepository.ts +67 -0
- package/template/src/routers/index.ts +49 -0
- package/template/src/service/base.service.ts +116 -0
- package/template/src/service/paramValidateTest.service.ts +139 -0
- package/template/src/service/ytGoods.service.ts +42 -0
- package/template/src/service/ytShop.service.ts +90 -0
- package/template/src/service/ytUser.service.ts +451 -0
- package/template/src/test/swaggerParameterTest.ts +90 -0
- package/template/src/utils/testDataInitializer.ts +296 -0
- package/template/static/output.json +15203 -0
- package/template/test/controllers/controllers.test.ts +173 -0
- package/template/test/controllers/example/ExampleController.test.ts +222 -0
- package/template/test/controllers/example/NewAnnotationExampleController.test.ts +200 -0
- package/template/test/framework/TransactionManagerDemo.test.ts +363 -0
- package/template/test/framework/annotation/AnnotationDecorators.test.ts +222 -0
- package/template/test/framework/annotation/AnnotationExecutor.test.ts +246 -0
- package/template/test/framework/annotation/AnnotationProcessor.test.ts +179 -0
- package/template/test/framework/annotation/CustomProcessors.test.ts +313 -0
- package/template/test/framework/annotation/DefaultProcessors.test.ts +371 -0
- package/template/test/framework/annotation/NewRouter.test.ts +272 -0
- package/template/test/framework/annotation/ProcessorManager.test.ts +248 -0
- package/template/test/framework/annotation/setup.ts +26 -0
- package/template/test/framework/cache.test.ts +101 -0
- package/template/test/framework/databaseConfig.test.ts +142 -0
- package/template/test/integration/integration.test.ts +153 -0
- package/template/test/plugins/weboffice/http.routes.int.test.ts +61 -0
- package/template/test/service/business.test.ts +87 -0
- package/template/test/service/paramValidateTest.service.test.ts +184 -0
- package/template/test/service/ytUser.service.test.ts +566 -0
- package/template/test/setup.ts +20 -0
- package/template/test/setupAfterEnv.ts +14 -0
- package/template/test/utils/testHelpers.ts +220 -0
- package/template/test_output.txt +0 -0
- package/template/tsconfig.build.json +17 -0
- package/template/tsconfig.json +31 -0
- package/template/webpack.config.js +71 -0
- package/template/yarn.lock +7354 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 企业级注解系统示例控制器
|
|
3
|
+
* 展示所有企业级注解的使用方法
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Get, Post, Body, Query, State } from '@src/framework/decorator/controller';
|
|
7
|
+
import { Logging, Permission, RateLimit } from '@src/framework/decorator/processor/AnnotationDecorators';
|
|
8
|
+
import {
|
|
9
|
+
EnterprisePerformance,
|
|
10
|
+
SecurityAudit,
|
|
11
|
+
DistributedTracing,
|
|
12
|
+
ConfigManagement
|
|
13
|
+
} from '@src/annotations';
|
|
14
|
+
import { BaseController } from '@src/controllers/base.controller';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 企业级示例控制器
|
|
18
|
+
* 展示企业级注解系统的完整功能
|
|
19
|
+
*/
|
|
20
|
+
export class EnterpriseExampleController extends BaseController {
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 示例1: 基础企业级注解组合
|
|
24
|
+
* - @EnterprisePerformance: 企业级性能监控
|
|
25
|
+
* - @SecurityAudit: 安全审计
|
|
26
|
+
* - @DistributedTracing: 分布式追踪
|
|
27
|
+
* - @ConfigManagement: 配置管理
|
|
28
|
+
*/
|
|
29
|
+
@Get('/enterprise/basic')
|
|
30
|
+
@EnterprisePerformance({
|
|
31
|
+
maxExecutionTime: 1000,
|
|
32
|
+
maxMemoryUsage: 50,
|
|
33
|
+
enableMetrics: true,
|
|
34
|
+
enableAlerts: true,
|
|
35
|
+
alertThresholds: {
|
|
36
|
+
executionTime: 500,
|
|
37
|
+
memoryUsage: 30
|
|
38
|
+
}
|
|
39
|
+
})
|
|
40
|
+
@SecurityAudit({
|
|
41
|
+
enableAudit: true,
|
|
42
|
+
logLevel: 'info',
|
|
43
|
+
includeRequestData: true,
|
|
44
|
+
complianceMode: 'GDPR',
|
|
45
|
+
sensitiveFields: ['email', 'phone']
|
|
46
|
+
})
|
|
47
|
+
@DistributedTracing({
|
|
48
|
+
enableTracing: true,
|
|
49
|
+
samplingRate: 1.0,
|
|
50
|
+
customTags: {
|
|
51
|
+
'service.version': '1.0.0',
|
|
52
|
+
'deployment.environment': 'production'
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
@ConfigManagement({
|
|
56
|
+
configKey: 'api.timeout',
|
|
57
|
+
defaultValue: 30000,
|
|
58
|
+
required: true,
|
|
59
|
+
validationRules: [
|
|
60
|
+
{ type: 'number', min: 1000, max: 300000 }
|
|
61
|
+
]
|
|
62
|
+
})
|
|
63
|
+
@Logging()
|
|
64
|
+
async getBasicEnterpriseData(@Query() query: any, ctx?: any) {
|
|
65
|
+
// 模拟一些业务逻辑
|
|
66
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
code: 0,
|
|
70
|
+
data: {
|
|
71
|
+
message: '企业级基础数据',
|
|
72
|
+
query,
|
|
73
|
+
timestamp: new Date().toISOString(),
|
|
74
|
+
config: ctx?.state.config
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 示例2: 高安全级别操作
|
|
81
|
+
* - 严格的权限控制
|
|
82
|
+
* - 详细的安全审计
|
|
83
|
+
* - 性能监控和告警
|
|
84
|
+
*/
|
|
85
|
+
@Post('/enterprise/secure-operation')
|
|
86
|
+
@EnterprisePerformance({
|
|
87
|
+
maxExecutionTime: 2000,
|
|
88
|
+
enableAlerts: true,
|
|
89
|
+
alertThresholds: {
|
|
90
|
+
executionTime: 1000,
|
|
91
|
+
memoryUsage: 100
|
|
92
|
+
}
|
|
93
|
+
})
|
|
94
|
+
@SecurityAudit({
|
|
95
|
+
enableAudit: true,
|
|
96
|
+
logLevel: 'warn',
|
|
97
|
+
includeRequestData: true,
|
|
98
|
+
includeResponseData: true,
|
|
99
|
+
complianceMode: 'SOX',
|
|
100
|
+
sensitiveFields: ['amount', 'account', 'transaction']
|
|
101
|
+
})
|
|
102
|
+
@DistributedTracing({
|
|
103
|
+
enableTracing: true,
|
|
104
|
+
samplingRate: 1.0,
|
|
105
|
+
customTags: {
|
|
106
|
+
'operation.type': 'financial',
|
|
107
|
+
'security.level': 'high'
|
|
108
|
+
}
|
|
109
|
+
})
|
|
110
|
+
@Permission({ requiredPermission: 'financial:write' })
|
|
111
|
+
@RateLimit({ maxRequests: 5, windowMs: 60000 })
|
|
112
|
+
@Logging()
|
|
113
|
+
async performSecureOperation(@Body() data: any, @State() state: any) {
|
|
114
|
+
// 模拟高安全级别操作
|
|
115
|
+
const startTime = Date.now();
|
|
116
|
+
|
|
117
|
+
// 模拟复杂业务逻辑
|
|
118
|
+
let result = 0;
|
|
119
|
+
for (let i = 0; i < 1000000; i++) {
|
|
120
|
+
result += Math.sqrt(i);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const executionTime = Date.now() - startTime;
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
code: 0,
|
|
127
|
+
data: {
|
|
128
|
+
message: '安全操作完成',
|
|
129
|
+
result: result,
|
|
130
|
+
executionTime: `${executionTime}ms`,
|
|
131
|
+
user: state.user,
|
|
132
|
+
timestamp: new Date().toISOString()
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 示例3: 配置驱动的操作
|
|
139
|
+
* - 动态配置管理
|
|
140
|
+
* - 环境相关配置
|
|
141
|
+
* - 配置验证
|
|
142
|
+
*/
|
|
143
|
+
@Get('/enterprise/config-driven')
|
|
144
|
+
@ConfigManagement({
|
|
145
|
+
configKey: 'feature.flags',
|
|
146
|
+
defaultValue: { newFeature: false, betaFeature: false },
|
|
147
|
+
validationRules: [
|
|
148
|
+
{ type: 'object' },
|
|
149
|
+
{
|
|
150
|
+
type: 'object',
|
|
151
|
+
custom: (value) => typeof value.newFeature === 'boolean'
|
|
152
|
+
}
|
|
153
|
+
]
|
|
154
|
+
})
|
|
155
|
+
@ConfigManagement({
|
|
156
|
+
configKey: 'api.rate.limit',
|
|
157
|
+
defaultValue: 100,
|
|
158
|
+
validationRules: [
|
|
159
|
+
{ type: 'number', min: 1, max: 10000 }
|
|
160
|
+
]
|
|
161
|
+
})
|
|
162
|
+
@EnterprisePerformance({
|
|
163
|
+
enableMetrics: true
|
|
164
|
+
})
|
|
165
|
+
@SecurityAudit({
|
|
166
|
+
enableAudit: true,
|
|
167
|
+
logLevel: 'info'
|
|
168
|
+
})
|
|
169
|
+
@Logging()
|
|
170
|
+
async getConfigDrivenData(@Query() query: any, ctx?: any) {
|
|
171
|
+
const config = ctx?.state.config || {};
|
|
172
|
+
|
|
173
|
+
return {
|
|
174
|
+
code: 0,
|
|
175
|
+
data: {
|
|
176
|
+
message: '配置驱动的数据',
|
|
177
|
+
query,
|
|
178
|
+
featureFlags: config['feature.flags'],
|
|
179
|
+
rateLimit: config['api.rate.limit'],
|
|
180
|
+
timestamp: new Date().toISOString()
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* 示例4: 分布式追踪演示
|
|
187
|
+
* - 跨服务调用追踪
|
|
188
|
+
* - 自定义标签和日志
|
|
189
|
+
* - 性能分析
|
|
190
|
+
*/
|
|
191
|
+
@Get('/enterprise/distributed-demo')
|
|
192
|
+
@DistributedTracing({
|
|
193
|
+
enableTracing: true,
|
|
194
|
+
samplingRate: 1.0,
|
|
195
|
+
customTags: {
|
|
196
|
+
'service.name': 'enterprise-example',
|
|
197
|
+
'operation.category': 'demo',
|
|
198
|
+
'business.unit': 'engineering'
|
|
199
|
+
}
|
|
200
|
+
})
|
|
201
|
+
@EnterprisePerformance({
|
|
202
|
+
enableMetrics: true,
|
|
203
|
+
enableAlerts: true
|
|
204
|
+
})
|
|
205
|
+
@SecurityAudit({
|
|
206
|
+
enableAudit: true,
|
|
207
|
+
logLevel: 'info'
|
|
208
|
+
})
|
|
209
|
+
@Logging()
|
|
210
|
+
async getDistributedDemoData(@Query() query: any, ctx?: any) {
|
|
211
|
+
// 模拟跨服务调用
|
|
212
|
+
const serviceCalls = [
|
|
213
|
+
this.callUserService(query.userId),
|
|
214
|
+
this.callProductService(query.productId),
|
|
215
|
+
this.callOrderService(query.orderId)
|
|
216
|
+
];
|
|
217
|
+
|
|
218
|
+
const results = await Promise.all(serviceCalls);
|
|
219
|
+
|
|
220
|
+
return {
|
|
221
|
+
code: 0,
|
|
222
|
+
data: {
|
|
223
|
+
message: '分布式追踪演示',
|
|
224
|
+
query,
|
|
225
|
+
serviceResults: results,
|
|
226
|
+
traceId: ctx?.state.traceContext?.traceId,
|
|
227
|
+
timestamp: new Date().toISOString()
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* 示例5: 错误处理和监控
|
|
234
|
+
* - 错误监控
|
|
235
|
+
* - 性能告警
|
|
236
|
+
* - 安全事件记录
|
|
237
|
+
*/
|
|
238
|
+
@Post('/enterprise/error-demo')
|
|
239
|
+
@EnterprisePerformance({
|
|
240
|
+
enableAlerts: true,
|
|
241
|
+
alertThresholds: {
|
|
242
|
+
executionTime: 100
|
|
243
|
+
}
|
|
244
|
+
})
|
|
245
|
+
@SecurityAudit({
|
|
246
|
+
enableAudit: true,
|
|
247
|
+
logLevel: 'error'
|
|
248
|
+
})
|
|
249
|
+
@DistributedTracing({
|
|
250
|
+
enableTracing: true,
|
|
251
|
+
customTags: {
|
|
252
|
+
'operation.type': 'error-demo'
|
|
253
|
+
}
|
|
254
|
+
})
|
|
255
|
+
@Logging()
|
|
256
|
+
async demonstrateErrorHandling(@Body() data: any) {
|
|
257
|
+
try {
|
|
258
|
+
// 模拟可能出错的操作
|
|
259
|
+
if (data.shouldError) {
|
|
260
|
+
throw new Error('模拟的业务错误');
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// 模拟慢操作
|
|
264
|
+
if (data.slowOperation) {
|
|
265
|
+
await new Promise(resolve => setTimeout(resolve, 200));
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return {
|
|
269
|
+
code: 0,
|
|
270
|
+
data: {
|
|
271
|
+
message: '操作成功',
|
|
272
|
+
data,
|
|
273
|
+
timestamp: new Date().toISOString()
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
} catch (error) {
|
|
278
|
+
// 错误会被注解系统自动记录和追踪
|
|
279
|
+
throw error;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
private async callUserService(userId: string): Promise<any> {
|
|
284
|
+
await new Promise(resolve => setTimeout(resolve, 50));
|
|
285
|
+
return { service: 'user', userId, data: 'user data' };
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
private async callProductService(productId: string): Promise<any> {
|
|
289
|
+
await new Promise(resolve => setTimeout(resolve, 30));
|
|
290
|
+
return { service: 'product', productId, data: 'product data' };
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
private async callOrderService(orderId: string): Promise<any> {
|
|
294
|
+
await new Promise(resolve => setTimeout(resolve, 40));
|
|
295
|
+
return { service: 'order', orderId, data: 'order data' };
|
|
296
|
+
}
|
|
297
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { Get, Query, State } from '@src/framework/decorator/controller';
|
|
2
|
+
import { Logging, Permission, RateLimit } from '@src/framework/decorator/processor/AnnotationDecorators';
|
|
3
|
+
import { PerformanceMonitor } from '@src/annotations/decorators/PerformanceMonitor';
|
|
4
|
+
import { BaseController } from '@src/controllers/base.controller';
|
|
5
|
+
import { Api, ApiTags, ApiResponse, ApiOperation } from '@src/framework/decorator/swagger';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 示例控制器 - 展示新的注解系统用法
|
|
9
|
+
*/
|
|
10
|
+
@ApiTags('示例', 'API文档示例')
|
|
11
|
+
export class ExampleController extends BaseController {
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 示例1: 使用新的注解系统
|
|
15
|
+
* - @Logging: 记录请求日志
|
|
16
|
+
* - @Permission: 检查用户权限
|
|
17
|
+
* - @RateLimit: 限制请求频率
|
|
18
|
+
*/
|
|
19
|
+
@Get('/example/secure')
|
|
20
|
+
@Logging()
|
|
21
|
+
@Permission({ requiredPermission: 'read:data' })
|
|
22
|
+
@RateLimit({ maxRequests: 10, windowMs: 60000 })
|
|
23
|
+
@Api({
|
|
24
|
+
tags: ['示例', '安全'],
|
|
25
|
+
summary: '获取受保护的数据',
|
|
26
|
+
description: '该接口需要权限验证和速率限制,展示如何保护敏感数据',
|
|
27
|
+
})
|
|
28
|
+
@ApiResponse({
|
|
29
|
+
status: 200,
|
|
30
|
+
description: '成功返回受保护的数据',
|
|
31
|
+
})
|
|
32
|
+
@ApiResponse({
|
|
33
|
+
status: 401,
|
|
34
|
+
description: '未授权访问',
|
|
35
|
+
})
|
|
36
|
+
@ApiResponse({
|
|
37
|
+
status: 403,
|
|
38
|
+
description: '权限不足',
|
|
39
|
+
})
|
|
40
|
+
async getSecureData(@Query() query: any, @State() state: any) {
|
|
41
|
+
return {
|
|
42
|
+
code: 0,
|
|
43
|
+
data: {
|
|
44
|
+
message: '这是受保护的数据',
|
|
45
|
+
user: state.user,
|
|
46
|
+
query: query
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 示例2: 混合使用新旧注解系统
|
|
53
|
+
* - @PerformanceMonitor: 监控方法执行时间和内存使用
|
|
54
|
+
* - @Logging: 记录请求日志
|
|
55
|
+
*/
|
|
56
|
+
@Get('/example/mixed')
|
|
57
|
+
@PerformanceMonitor({ maxExecutionTime: 100, maxMemoryUsage: 2 }) // 100ms, 2MB
|
|
58
|
+
@Logging()
|
|
59
|
+
@Api({
|
|
60
|
+
tags: ['示例', '性能监控'],
|
|
61
|
+
summary: '混合注解示例',
|
|
62
|
+
description: '展示如何使用性能监控装饰器来监控方法执行时间和内存使用',
|
|
63
|
+
})
|
|
64
|
+
@ApiResponse({
|
|
65
|
+
status: 200,
|
|
66
|
+
description: '成功返回计算结果',
|
|
67
|
+
})
|
|
68
|
+
async getMixedData(@Query() query: any) {
|
|
69
|
+
// 模拟一些计算操作来展示性能监控
|
|
70
|
+
let sum = 0;
|
|
71
|
+
for (let i = 0; i < 1000000; i++) {
|
|
72
|
+
sum += Math.sqrt(i);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
code: 0,
|
|
77
|
+
data: {
|
|
78
|
+
message: '混合注解示例 - 包含性能监控',
|
|
79
|
+
query: query,
|
|
80
|
+
calculationResult: sum,
|
|
81
|
+
performance: 'monitored'
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 示例3: 只使用新注解系统
|
|
88
|
+
*/
|
|
89
|
+
@Get('/example/new-only')
|
|
90
|
+
@Logging()
|
|
91
|
+
@RateLimit({ maxRequests: 5, windowMs: 30000 })
|
|
92
|
+
@Api({
|
|
93
|
+
tags: ['示例', '基础'],
|
|
94
|
+
summary: '新注解系统示例',
|
|
95
|
+
description: '展示仅使用新的注解系统实现功能和文档',
|
|
96
|
+
})
|
|
97
|
+
@ApiResponse({
|
|
98
|
+
status: 200,
|
|
99
|
+
description: '成功返回',
|
|
100
|
+
})
|
|
101
|
+
async getNewOnlyData() {
|
|
102
|
+
return {
|
|
103
|
+
code: 0,
|
|
104
|
+
data: {
|
|
105
|
+
message: '仅使用新注解系统'
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { Get, Post, Query, Body, State, ResponseCode, ResponseHeader } from '@src/framework/decorator/controller';
|
|
2
|
+
import { BaseController, ControllerResponse } from '@src/controllers/base.controller';
|
|
3
|
+
import { Inject } from 'dp-ioc2';
|
|
4
|
+
import { YtUserService } from '@src/service/ytUser.service';
|
|
5
|
+
import { Logging, Permission, RateLimit } from '@src/framework/decorator/processor/AnnotationDecorators';
|
|
6
|
+
import { ExampleBodyDto, ExampleQueryDto } from '@src/dto/controller/example/NewAnnotationExampleController.dto';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 新注解系统使用示例控制器
|
|
10
|
+
* 展示如何在实际项目中使用新的注解处理器系统
|
|
11
|
+
*/
|
|
12
|
+
export class NewAnnotationExampleController extends BaseController {
|
|
13
|
+
@Inject(YtUserService)
|
|
14
|
+
private ytUserService: YtUserService;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 示例1:基础的新注解使用
|
|
18
|
+
* 使用 Logging 注解记录请求日志
|
|
19
|
+
*/
|
|
20
|
+
@Get('/basic-example')
|
|
21
|
+
@Logging()
|
|
22
|
+
@ResponseCode(200)
|
|
23
|
+
async basicExample(@Query() query: ExampleQueryDto): Promise<ControllerResponse<string>> {
|
|
24
|
+
console.log('执行基础示例方法');
|
|
25
|
+
return this.success('基础示例执行成功');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 示例2:权限控制
|
|
30
|
+
* 使用 Permission 注解进行权限检查
|
|
31
|
+
*/
|
|
32
|
+
@Get('/protected-data')
|
|
33
|
+
@Logging()
|
|
34
|
+
@Permission({ requiredPermission: 'read:sensitive-data' })
|
|
35
|
+
@ResponseCode(200)
|
|
36
|
+
async getProtectedData(@State('user') user: any): Promise<ControllerResponse<any>> {
|
|
37
|
+
console.log('获取受保护的数据');
|
|
38
|
+
return this.success({
|
|
39
|
+
message: '这是受保护的数据',
|
|
40
|
+
user: user,
|
|
41
|
+
timestamp: new Date().toISOString()
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 示例3:请求限流
|
|
47
|
+
* 使用 RateLimit 注解限制请求频率
|
|
48
|
+
*/
|
|
49
|
+
@Post('/rate-limited-endpoint')
|
|
50
|
+
@Logging()
|
|
51
|
+
@RateLimit({ maxRequests: 5, windowMs: 60000 }) // 每分钟最多5次请求
|
|
52
|
+
@ResponseCode(201)
|
|
53
|
+
async rateLimitedEndpoint(@Body() body: ExampleBodyDto): Promise<ControllerResponse<any>> {
|
|
54
|
+
console.log('处理限流端点请求');
|
|
55
|
+
return this.success({
|
|
56
|
+
message: '请求处理成功',
|
|
57
|
+
data: body,
|
|
58
|
+
timestamp: new Date().toISOString()
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 示例4:复合注解使用
|
|
64
|
+
* 同时使用多个新注解
|
|
65
|
+
*/
|
|
66
|
+
@Get('/complex-example')
|
|
67
|
+
@Logging()
|
|
68
|
+
@Permission({ requiredPermission: 'read:complex-data' })
|
|
69
|
+
@RateLimit({ maxRequests: 10, windowMs: 300000 }) // 5分钟内最多10次请求
|
|
70
|
+
@ResponseCode(200)
|
|
71
|
+
@ResponseHeader('X-Custom-Header', 'ComplexExample')
|
|
72
|
+
async complexExample(
|
|
73
|
+
@Query() query: ExampleQueryDto,
|
|
74
|
+
@State('user') user: any
|
|
75
|
+
): Promise<ControllerResponse<any>> {
|
|
76
|
+
console.log('执行复杂示例方法');
|
|
77
|
+
|
|
78
|
+
// 模拟业务逻辑
|
|
79
|
+
const usersResult = await this.ytUserService.getUsers({ page: 1, pageSize: 10 });
|
|
80
|
+
|
|
81
|
+
return this.success({
|
|
82
|
+
message: '复杂示例执行成功',
|
|
83
|
+
query: query,
|
|
84
|
+
user: user,
|
|
85
|
+
usersCount: usersResult.data ? usersResult.data[1] : 0,
|
|
86
|
+
timestamp: new Date().toISOString()
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* 示例5:新旧注解混合使用
|
|
92
|
+
* 展示新旧注解系统的兼容性
|
|
93
|
+
*/
|
|
94
|
+
@Post('/mixed-annotations')
|
|
95
|
+
@Logging() // 新注解
|
|
96
|
+
@ResponseCode(201) // 旧注解
|
|
97
|
+
@ResponseHeader('X-Mixed-Example', 'true') // 旧注解
|
|
98
|
+
async mixedAnnotations(@Body() body: ExampleBodyDto): Promise<ControllerResponse<any>> {
|
|
99
|
+
console.log('执行混合注解示例');
|
|
100
|
+
return this.success({
|
|
101
|
+
message: '混合注解示例执行成功',
|
|
102
|
+
data: body,
|
|
103
|
+
note: '这个端点同时使用了新旧注解系统'
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 示例6:错误处理
|
|
109
|
+
* 展示注解处理器如何处理错误情况
|
|
110
|
+
*/
|
|
111
|
+
@Get('/error-example')
|
|
112
|
+
@Logging()
|
|
113
|
+
@Permission({ requiredPermission: 'admin:access' })
|
|
114
|
+
async errorExample(): Promise<ControllerResponse<any>> {
|
|
115
|
+
console.log('执行错误示例');
|
|
116
|
+
|
|
117
|
+
// 模拟权限不足的情况
|
|
118
|
+
throw new Error('模拟权限不足错误');
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 示例7:异步处理
|
|
123
|
+
* 展示注解处理器如何处理异步方法
|
|
124
|
+
*/
|
|
125
|
+
@Get('/async-example')
|
|
126
|
+
@Logging()
|
|
127
|
+
@RateLimit({ maxRequests: 3, windowMs: 60000 })
|
|
128
|
+
async asyncExample(): Promise<ControllerResponse<any>> {
|
|
129
|
+
console.log('开始异步处理');
|
|
130
|
+
|
|
131
|
+
// 模拟异步操作
|
|
132
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
133
|
+
|
|
134
|
+
console.log('异步处理完成');
|
|
135
|
+
return this.success({
|
|
136
|
+
message: '异步处理完成',
|
|
137
|
+
timestamp: new Date().toISOString()
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 示例8:数据验证
|
|
143
|
+
* 展示参数验证和响应验证
|
|
144
|
+
*/
|
|
145
|
+
@Post('/validation-example')
|
|
146
|
+
@Logging()
|
|
147
|
+
async validationExample(@Body() body: ExampleBodyDto): Promise<ControllerResponse<any>> {
|
|
148
|
+
console.log('执行验证示例');
|
|
149
|
+
|
|
150
|
+
// 这里会进行参数验证(通过 @Body 注解)
|
|
151
|
+
// 响应也会进行验证(通过 ControllerResponse 类型)
|
|
152
|
+
|
|
153
|
+
return this.success({
|
|
154
|
+
message: '验证通过',
|
|
155
|
+
receivedData: body,
|
|
156
|
+
validationTimestamp: new Date().toISOString()
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|