aury-boot 0.0.2__py3-none-any.whl → 0.0.4__py3-none-any.whl

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.
Files changed (138) hide show
  1. aury/boot/__init__.py +66 -0
  2. aury/boot/_version.py +2 -2
  3. aury/boot/application/__init__.py +120 -0
  4. aury/boot/application/app/__init__.py +39 -0
  5. aury/boot/application/app/base.py +511 -0
  6. aury/boot/application/app/components.py +434 -0
  7. aury/boot/application/app/middlewares.py +101 -0
  8. aury/boot/application/config/__init__.py +44 -0
  9. aury/boot/application/config/settings.py +663 -0
  10. aury/boot/application/constants/__init__.py +19 -0
  11. aury/boot/application/constants/components.py +50 -0
  12. aury/boot/application/constants/scheduler.py +28 -0
  13. aury/boot/application/constants/service.py +29 -0
  14. aury/boot/application/errors/__init__.py +55 -0
  15. aury/boot/application/errors/chain.py +80 -0
  16. aury/boot/application/errors/codes.py +67 -0
  17. aury/boot/application/errors/exceptions.py +238 -0
  18. aury/boot/application/errors/handlers.py +320 -0
  19. aury/boot/application/errors/response.py +120 -0
  20. aury/boot/application/interfaces/__init__.py +76 -0
  21. aury/boot/application/interfaces/egress.py +224 -0
  22. aury/boot/application/interfaces/ingress.py +98 -0
  23. aury/boot/application/middleware/__init__.py +22 -0
  24. aury/boot/application/middleware/logging.py +451 -0
  25. aury/boot/application/migrations/__init__.py +13 -0
  26. aury/boot/application/migrations/manager.py +685 -0
  27. aury/boot/application/migrations/setup.py +237 -0
  28. aury/boot/application/rpc/__init__.py +63 -0
  29. aury/boot/application/rpc/base.py +108 -0
  30. aury/boot/application/rpc/client.py +294 -0
  31. aury/boot/application/rpc/discovery.py +218 -0
  32. aury/boot/application/scheduler/__init__.py +13 -0
  33. aury/boot/application/scheduler/runner.py +123 -0
  34. aury/boot/application/server/__init__.py +296 -0
  35. aury/boot/commands/__init__.py +30 -0
  36. aury/boot/commands/add.py +76 -0
  37. aury/boot/commands/app.py +105 -0
  38. aury/boot/commands/config.py +177 -0
  39. aury/boot/commands/docker.py +367 -0
  40. aury/boot/commands/docs.py +284 -0
  41. aury/boot/commands/generate.py +1277 -0
  42. aury/boot/commands/init.py +892 -0
  43. aury/boot/commands/migrate/__init__.py +37 -0
  44. aury/boot/commands/migrate/app.py +54 -0
  45. aury/boot/commands/migrate/commands.py +303 -0
  46. aury/boot/commands/scheduler.py +124 -0
  47. aury/boot/commands/server/__init__.py +21 -0
  48. aury/boot/commands/server/app.py +541 -0
  49. aury/boot/commands/templates/generate/api.py.tpl +105 -0
  50. aury/boot/commands/templates/generate/model.py.tpl +17 -0
  51. aury/boot/commands/templates/generate/repository.py.tpl +19 -0
  52. aury/boot/commands/templates/generate/schema.py.tpl +29 -0
  53. aury/boot/commands/templates/generate/service.py.tpl +48 -0
  54. aury/boot/commands/templates/project/CLI.md.tpl +92 -0
  55. aury/boot/commands/templates/project/DEVELOPMENT.md.tpl +1397 -0
  56. aury/boot/commands/templates/project/README.md.tpl +111 -0
  57. aury/boot/commands/templates/project/admin_console_init.py.tpl +50 -0
  58. aury/boot/commands/templates/project/config.py.tpl +30 -0
  59. aury/boot/commands/templates/project/conftest.py.tpl +26 -0
  60. aury/boot/commands/templates/project/env.example.tpl +213 -0
  61. aury/boot/commands/templates/project/gitignore.tpl +128 -0
  62. aury/boot/commands/templates/project/main.py.tpl +41 -0
  63. aury/boot/commands/templates/project/modules/api.py.tpl +19 -0
  64. aury/boot/commands/templates/project/modules/exceptions.py.tpl +84 -0
  65. aury/boot/commands/templates/project/modules/schedules.py.tpl +18 -0
  66. aury/boot/commands/templates/project/modules/tasks.py.tpl +20 -0
  67. aury/boot/commands/worker.py +143 -0
  68. aury/boot/common/__init__.py +35 -0
  69. aury/boot/common/exceptions/__init__.py +114 -0
  70. aury/boot/common/i18n/__init__.py +16 -0
  71. aury/boot/common/i18n/translator.py +272 -0
  72. aury/boot/common/logging/__init__.py +716 -0
  73. aury/boot/contrib/__init__.py +10 -0
  74. aury/boot/contrib/admin_console/__init__.py +18 -0
  75. aury/boot/contrib/admin_console/auth.py +137 -0
  76. aury/boot/contrib/admin_console/discovery.py +69 -0
  77. aury/boot/contrib/admin_console/install.py +172 -0
  78. aury/boot/contrib/admin_console/utils.py +44 -0
  79. aury/boot/domain/__init__.py +79 -0
  80. aury/boot/domain/exceptions/__init__.py +132 -0
  81. aury/boot/domain/models/__init__.py +51 -0
  82. aury/boot/domain/models/base.py +69 -0
  83. aury/boot/domain/models/mixins.py +135 -0
  84. aury/boot/domain/models/models.py +96 -0
  85. aury/boot/domain/pagination/__init__.py +279 -0
  86. aury/boot/domain/repository/__init__.py +23 -0
  87. aury/boot/domain/repository/impl.py +423 -0
  88. aury/boot/domain/repository/interceptors.py +47 -0
  89. aury/boot/domain/repository/interface.py +106 -0
  90. aury/boot/domain/repository/query_builder.py +348 -0
  91. aury/boot/domain/service/__init__.py +11 -0
  92. aury/boot/domain/service/base.py +73 -0
  93. aury/boot/domain/transaction/__init__.py +404 -0
  94. aury/boot/infrastructure/__init__.py +104 -0
  95. aury/boot/infrastructure/cache/__init__.py +31 -0
  96. aury/boot/infrastructure/cache/backends.py +348 -0
  97. aury/boot/infrastructure/cache/base.py +68 -0
  98. aury/boot/infrastructure/cache/exceptions.py +37 -0
  99. aury/boot/infrastructure/cache/factory.py +94 -0
  100. aury/boot/infrastructure/cache/manager.py +274 -0
  101. aury/boot/infrastructure/database/__init__.py +39 -0
  102. aury/boot/infrastructure/database/config.py +71 -0
  103. aury/boot/infrastructure/database/exceptions.py +44 -0
  104. aury/boot/infrastructure/database/manager.py +317 -0
  105. aury/boot/infrastructure/database/query_tools/__init__.py +164 -0
  106. aury/boot/infrastructure/database/strategies/__init__.py +198 -0
  107. aury/boot/infrastructure/di/__init__.py +15 -0
  108. aury/boot/infrastructure/di/container.py +393 -0
  109. aury/boot/infrastructure/events/__init__.py +33 -0
  110. aury/boot/infrastructure/events/bus.py +362 -0
  111. aury/boot/infrastructure/events/config.py +52 -0
  112. aury/boot/infrastructure/events/consumer.py +134 -0
  113. aury/boot/infrastructure/events/middleware.py +51 -0
  114. aury/boot/infrastructure/events/models.py +63 -0
  115. aury/boot/infrastructure/monitoring/__init__.py +529 -0
  116. aury/boot/infrastructure/scheduler/__init__.py +19 -0
  117. aury/boot/infrastructure/scheduler/exceptions.py +37 -0
  118. aury/boot/infrastructure/scheduler/manager.py +478 -0
  119. aury/boot/infrastructure/storage/__init__.py +38 -0
  120. aury/boot/infrastructure/storage/base.py +164 -0
  121. aury/boot/infrastructure/storage/exceptions.py +37 -0
  122. aury/boot/infrastructure/storage/factory.py +88 -0
  123. aury/boot/infrastructure/tasks/__init__.py +24 -0
  124. aury/boot/infrastructure/tasks/config.py +45 -0
  125. aury/boot/infrastructure/tasks/constants.py +37 -0
  126. aury/boot/infrastructure/tasks/exceptions.py +37 -0
  127. aury/boot/infrastructure/tasks/manager.py +490 -0
  128. aury/boot/testing/__init__.py +24 -0
  129. aury/boot/testing/base.py +122 -0
  130. aury/boot/testing/client.py +163 -0
  131. aury/boot/testing/factory.py +154 -0
  132. aury/boot/toolkit/__init__.py +21 -0
  133. aury/boot/toolkit/http/__init__.py +367 -0
  134. {aury_boot-0.0.2.dist-info → aury_boot-0.0.4.dist-info}/METADATA +3 -2
  135. aury_boot-0.0.4.dist-info/RECORD +137 -0
  136. aury_boot-0.0.2.dist-info/RECORD +0 -5
  137. {aury_boot-0.0.2.dist-info → aury_boot-0.0.4.dist-info}/WHEEL +0 -0
  138. {aury_boot-0.0.2.dist-info → aury_boot-0.0.4.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,663 @@
1
+ """共享配置基类。
2
+
3
+ 提供所有应用共享的基础配置结构。
4
+ 使用 pydantic-settings 进行分层分级配置管理。
5
+
6
+ 注意:Application 层的配置是独立的,不依赖 Infrastructure 层。
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ from pathlib import Path
12
+ from typing import Literal
13
+
14
+ from dotenv import load_dotenv
15
+ from pydantic import Field
16
+ from pydantic_settings import BaseSettings, SettingsConfigDict
17
+
18
+
19
+ def _load_env_file(env_file: str | Path) -> bool:
20
+ """加载 .env 文件到环境变量。"""
21
+ return load_dotenv(env_file, override=True)
22
+
23
+
24
+ class DatabaseSettings(BaseSettings):
25
+ """数据库配置。
26
+
27
+ 环境变量前缀: DATABASE_
28
+ 示例: DATABASE_URL, DATABASE_ECHO, DATABASE_POOL_SIZE
29
+ """
30
+
31
+ url: str = Field(
32
+ default="sqlite+aiosqlite:///./app.db",
33
+ description="数据库连接字符串"
34
+ )
35
+ echo: bool = Field(
36
+ default=False,
37
+ description="是否输出 SQL 语句"
38
+ )
39
+ pool_size: int = Field(
40
+ default=5,
41
+ description="数据库连接池大小"
42
+ )
43
+ max_overflow: int = Field(
44
+ default=10,
45
+ description="连接池最大溢出连接数"
46
+ )
47
+ pool_recycle: int = Field(
48
+ default=3600,
49
+ description="连接回收时间(秒)"
50
+ )
51
+ pool_timeout: int = Field(
52
+ default=30,
53
+ description="获取连接超时时间(秒)"
54
+ )
55
+ pool_pre_ping: bool = Field(
56
+ default=True,
57
+ description="是否在获取连接前进行 PING"
58
+ )
59
+
60
+ model_config = SettingsConfigDict(
61
+ env_prefix="DATABASE_",
62
+ case_sensitive=False,
63
+ )
64
+
65
+
66
+ class CacheSettings(BaseSettings):
67
+ """缓存配置。
68
+
69
+ 环境变量前缀: CACHE_
70
+ 示例: CACHE_TYPE, CACHE_URL, CACHE_MAX_SIZE
71
+
72
+ 支持的缓存类型:
73
+ - memory: 内存缓存(默认,无需 URL)
74
+ - redis: Redis 缓存(需要设置 CACHE_URL)
75
+ - memcached: Memcached 缓存(需要设置 CACHE_URL)
76
+ """
77
+
78
+ cache_type: str = Field(
79
+ default="memory",
80
+ description="缓存类型 (memory/redis/memcached)"
81
+ )
82
+ url: str | None = Field(
83
+ default=None,
84
+ description="缓存服务 URL(如 redis://localhost:6379)"
85
+ )
86
+ max_size: int = Field(
87
+ default=1000,
88
+ description="内存缓存最大大小"
89
+ )
90
+
91
+ model_config = SettingsConfigDict(
92
+ env_prefix="CACHE_",
93
+ case_sensitive=False,
94
+ )
95
+
96
+
97
+ class StorageSettings(BaseSettings):
98
+ """对象存储组件接入配置(Application 层)。
99
+
100
+ 说明:
101
+ - Application 层负责从 env/.env 读取配置(快速接入组件)
102
+ - Infrastructure 层的 storage 仅接受 Pydantic(BaseModel) 的配置对象,不读取 env
103
+
104
+ 环境变量前缀:STORAGE_
105
+ """
106
+
107
+ enabled: bool = Field(default=True, description="是否启用存储组件")
108
+
109
+ # 后端类型
110
+ type: Literal["local", "s3", "oss", "cos"] = Field(default="local", description="存储类型")
111
+
112
+ # S3/兼容协议通用
113
+ access_key_id: str | None = Field(default=None, description="访问密钥ID")
114
+ access_key_secret: str | None = Field(default=None, description="访问密钥")
115
+ session_token: str | None = Field(default=None, description="会话令牌(STS临时凭证)")
116
+ endpoint: str | None = Field(default=None, description="端点URL(MinIO/私有云等)")
117
+ region: str | None = Field(default=None, description="区域")
118
+ bucket_name: str | None = Field(default=None, description="默认桶名")
119
+ addressing_style: Literal["virtual", "path"] = Field(default="virtual", description="S3寻址风格")
120
+
121
+ # S3 AssumeRole(服务端使用;由外部决定何时刷新)
122
+ role_arn: str | None = Field(default=None, description="STS AssumeRole 的角色ARN")
123
+ role_session_name: str = Field(default="aury-storage", description="STS会话名")
124
+ external_id: str | None = Field(default=None, description="STS ExternalId")
125
+ sts_endpoint: str | None = Field(default=None, description="STS端点(可选)")
126
+ sts_region: str | None = Field(default=None, description="STS区域(可选)")
127
+ sts_duration_seconds: int = Field(default=3600, description="AssumeRole DurationSeconds")
128
+
129
+ # local
130
+ base_path: str = Field(default="./storage", description="本地存储基础目录")
131
+
132
+ model_config = SettingsConfigDict(
133
+ env_prefix="STORAGE_",
134
+ case_sensitive=False,
135
+ extra="ignore",
136
+ )
137
+
138
+
139
+ class ServerSettings(BaseSettings):
140
+ """服务器配置。
141
+
142
+ 环境变量前缀: SERVER_
143
+ 示例: SERVER_HOST, SERVER_PORT, SERVER_RELOAD
144
+ """
145
+
146
+ host: str = Field(
147
+ default="127.0.0.1",
148
+ description="服务器监听地址"
149
+ )
150
+ port: int = Field(
151
+ default=8000,
152
+ description="服务器监听端口"
153
+ )
154
+ reload: bool = Field(
155
+ default=True,
156
+ description="是否启用热重载"
157
+ )
158
+ workers: int = Field(
159
+ default=1,
160
+ description="工作进程数"
161
+ )
162
+
163
+ model_config = SettingsConfigDict(
164
+ env_prefix="SERVER_",
165
+ case_sensitive=False,
166
+ )
167
+
168
+
169
+ class CORSSettings(BaseSettings):
170
+ """CORS配置。
171
+
172
+ 环境变量前缀: CORS_
173
+ 示例: CORS_ORIGINS, CORS_ALLOW_CREDENTIALS, CORS_ALLOW_METHODS
174
+ """
175
+
176
+ origins: list[str] = Field(
177
+ default=["*"],
178
+ description="允许的CORS源"
179
+ )
180
+ allow_credentials: bool = Field(
181
+ default=True,
182
+ description="是否允许CORS凭据"
183
+ )
184
+ allow_methods: list[str] = Field(
185
+ default=["*"],
186
+ description="允许的CORS方法"
187
+ )
188
+ allow_headers: list[str] = Field(
189
+ default=["*"],
190
+ description="允许的CORS头"
191
+ )
192
+
193
+ model_config = SettingsConfigDict(
194
+ env_prefix="CORS_",
195
+ case_sensitive=False,
196
+ )
197
+
198
+
199
+ class LogSettings(BaseSettings):
200
+ """日志配置。
201
+
202
+ 环境变量前缀: LOG_
203
+ 示例: LOG_LEVEL, LOG_DIR, LOG_ROTATION_TIME, LOG_RETENTION_DAYS
204
+ """
205
+
206
+ level: str = Field(
207
+ default="INFO",
208
+ description="日志级别 (DEBUG/INFO/WARNING/ERROR/CRITICAL)"
209
+ )
210
+ dir: str | None = Field(
211
+ default=None,
212
+ description="日志文件目录(如果不设置则默认为 './log')"
213
+ )
214
+ rotation_time: str = Field(
215
+ default="00:00",
216
+ description="日志文件轮转时间 (HH:MM 格式,每天定时轮转)"
217
+ )
218
+ rotation_size: str = Field(
219
+ default="50 MB",
220
+ description="日志文件轮转大小阈值(超过此大小会产生 .1, .2 等后缀文件)"
221
+ )
222
+ retention_days: int = Field(
223
+ default=7,
224
+ description="日志文件保留天数"
225
+ )
226
+ enable_file_rotation: bool = Field(
227
+ default=True,
228
+ description="是否启用日志文件轮转"
229
+ )
230
+ enable_classify: bool = Field(
231
+ default=True,
232
+ description="是否按模块和级别分类日志文件"
233
+ )
234
+ enable_console: bool = Field(
235
+ default=True,
236
+ description="是否输出日志到控制台"
237
+ )
238
+ websocket_log_messages: bool = Field(
239
+ default=False,
240
+ description="是否记录 WebSocket 消息内容(注意性能和敏感数据)"
241
+ )
242
+
243
+ model_config = SettingsConfigDict(
244
+ env_prefix="LOG_",
245
+ case_sensitive=False,
246
+ )
247
+
248
+
249
+ class ServiceSettings(BaseSettings):
250
+ """服务配置。
251
+
252
+ 环境变量前缀: SERVICE_
253
+ 示例: SERVICE_NAME, SERVICE_TYPE
254
+
255
+ 服务类型说明:
256
+ - api: 运行 API 服务(SCHEDULER_ENABLED 决定是否同时运行调度器)
257
+ - worker: 运行任务队列 Worker(处理异步任务)
258
+
259
+ 独立调度器通过 `aury scheduler` 命令运行,不需要配置 SERVICE_TYPE。
260
+ """
261
+
262
+ name: str = Field(
263
+ default="app",
264
+ description="服务名称,用于日志目录区分"
265
+ )
266
+ service_type: str = Field(
267
+ default="api",
268
+ description="服务类型(api/worker)"
269
+ )
270
+
271
+ model_config = SettingsConfigDict(
272
+ env_prefix="SERVICE_",
273
+ case_sensitive=False,
274
+ )
275
+
276
+
277
+ class SchedulerSettings(BaseSettings):
278
+ """调度器配置。
279
+
280
+ 环境变量前缀: SCHEDULER_
281
+ 示例: SCHEDULER_ENABLED, SCHEDULER_SCHEDULE_MODULES
282
+
283
+ 仅在 SERVICE_TYPE=api 时有效:
284
+ - SCHEDULER_ENABLED=true: API 服务同时运行内嵌调度器(默认)
285
+ - SCHEDULER_ENABLED=false: 只运行 API,不启动调度器
286
+
287
+ 独立调度器通过 `aury scheduler` 命令运行,不需要此配置。
288
+ """
289
+
290
+ enabled: bool = Field(
291
+ default=True,
292
+ description="是否在 API 服务中启用内嵌调度器"
293
+ )
294
+ schedule_modules: list[str] = Field(
295
+ default_factory=list,
296
+ description="定时任务模块列表。为空时自动发现 schedules 模块"
297
+ )
298
+
299
+ model_config = SettingsConfigDict(
300
+ env_prefix="SCHEDULER_",
301
+ case_sensitive=False,
302
+ )
303
+
304
+
305
+ class TaskSettings(BaseSettings):
306
+ """任务队列配置。
307
+
308
+ 环境变量前缀: TASK_
309
+ 示例: TASK_BROKER_URL, TASK_MAX_RETRIES
310
+ """
311
+
312
+ broker_url: str | None = Field(
313
+ default=None,
314
+ description="任务队列代理 URL(如 Redis 或 RabbitMQ)"
315
+ )
316
+ max_retries: int = Field(
317
+ default=3,
318
+ description="最大重试次数"
319
+ )
320
+ timeout: int = Field(
321
+ default=3600,
322
+ description="任务超时时间(秒)"
323
+ )
324
+
325
+ model_config = SettingsConfigDict(
326
+ env_prefix="TASK_",
327
+ case_sensitive=False,
328
+ )
329
+
330
+
331
+ class EventSettings(BaseSettings):
332
+ """事件总线配置。
333
+
334
+ 环境变量前缀: EVENT_
335
+ 示例: EVENT_BROKER_URL, EVENT_EXCHANGE_NAME
336
+ """
337
+
338
+ broker_url: str | None = Field(
339
+ default=None,
340
+ description="事件总线代理 URL(如 RabbitMQ)"
341
+ )
342
+ exchange_name: str = Field(
343
+ default="aury.events",
344
+ description="事件交换机名称"
345
+ )
346
+
347
+ model_config = SettingsConfigDict(
348
+ env_prefix="EVENT_",
349
+ case_sensitive=False,
350
+ )
351
+
352
+
353
+ class MigrationSettings(BaseSettings):
354
+ """数据库迁移配置。
355
+
356
+ 环境变量前缀: MIGRATION_
357
+ 示例: MIGRATION_CONFIG_PATH, MIGRATION_SCRIPT_LOCATION, MIGRATION_MODEL_MODULES
358
+ """
359
+
360
+ config_path: str = Field(
361
+ default="alembic.ini",
362
+ description="Alembic 配置文件路径"
363
+ )
364
+ script_location: str = Field(
365
+ default="migrations",
366
+ description="Alembic 迁移脚本目录"
367
+ )
368
+ model_modules: list[str] = Field(
369
+ default_factory=lambda: [
370
+ "models",
371
+ "app.models",
372
+ "app.**.models",
373
+ ],
374
+ description="模型模块列表(用于自动检测变更)。支持通配符: * 和 **。"
375
+ )
376
+ auto_create: bool = Field(
377
+ default=True,
378
+ description="是否自动创建迁移配置和目录"
379
+ )
380
+
381
+ model_config = SettingsConfigDict(
382
+ env_prefix="MIGRATION_",
383
+ case_sensitive=False,
384
+ )
385
+
386
+
387
+ class RPCClientSettings(BaseSettings):
388
+ """RPC 客户端调用配置。
389
+
390
+ 用于配置客户端调用其他服务时的行为。
391
+
392
+ 环境变量前缀: RPC_CLIENT_
393
+ 示例: RPC_CLIENT_SERVICES, RPC_CLIENT_TIMEOUT, RPC_CLIENT_RETRY_TIMES, RPC_CLIENT_DNS_SCHEME
394
+ """
395
+
396
+ services: dict[str, str] = Field(
397
+ default_factory=dict,
398
+ description="服务地址映射 {service_name: url}(优先级最高,会覆盖 DNS 解析)"
399
+ )
400
+ default_timeout: int = Field(
401
+ default=30,
402
+ description="默认超时时间(秒)"
403
+ )
404
+ default_retry_times: int = Field(
405
+ default=3,
406
+ description="默认重试次数"
407
+ )
408
+ dns_scheme: str = Field(
409
+ default="http",
410
+ description="DNS 解析使用的协议(http 或 https)"
411
+ )
412
+ dns_port: int = Field(
413
+ default=80,
414
+ description="DNS 解析默认端口"
415
+ )
416
+ use_dns_fallback: bool = Field(
417
+ default=True,
418
+ description="是否在配置中找不到时使用 DNS 解析(K8s/Docker Compose 自动 DNS)"
419
+ )
420
+
421
+ model_config = SettingsConfigDict(
422
+ env_prefix="RPC_CLIENT_",
423
+ case_sensitive=False,
424
+ )
425
+
426
+
427
+ class RPCServiceSettings(BaseSettings):
428
+ """RPC 服务注册配置。
429
+
430
+ 用于配置当前服务注册到服务注册中心时的信息。
431
+
432
+ 环境变量前缀: RPC_SERVICE_
433
+ 示例: RPC_SERVICE_NAME, RPC_SERVICE_URL, RPC_SERVICE_HEALTH_CHECK_URL
434
+ """
435
+
436
+ name: str | None = Field(
437
+ default=None,
438
+ description="服务名称(用于注册)"
439
+ )
440
+ url: str | None = Field(
441
+ default=None,
442
+ description="服务地址(用于注册)"
443
+ )
444
+ health_check_url: str | None = Field(
445
+ default=None,
446
+ description="健康检查 URL(用于注册)"
447
+ )
448
+ auto_register: bool = Field(
449
+ default=False,
450
+ description="是否自动注册到服务注册中心"
451
+ )
452
+ registry_url: str | None = Field(
453
+ default=None,
454
+ description="服务注册中心地址(如果使用外部注册中心)"
455
+ )
456
+
457
+ model_config = SettingsConfigDict(
458
+ env_prefix="RPC_SERVICE_",
459
+ case_sensitive=False,
460
+ )
461
+
462
+
463
+ class HealthCheckSettings(BaseSettings):
464
+ """健康检查配置。
465
+
466
+ 用于配置 Aury 框架的默认健康检查端点。
467
+ 注意:此配置仅用于框架内置的健康检查端点,不影响服务自身的健康检查端点。
468
+
469
+ 环境变量前缀: HEALTH_CHECK_
470
+ 示例: HEALTH_CHECK_PATH, HEALTH_CHECK_ENABLED
471
+ """
472
+
473
+ path: str = Field(
474
+ default="/api/health",
475
+ description="健康检查端点路径(默认: /api/health)"
476
+ )
477
+ enabled: bool = Field(
478
+ default=True,
479
+ description="是否启用 Aury 默认健康检查端点"
480
+ )
481
+
482
+ model_config = SettingsConfigDict(
483
+ env_prefix="HEALTH_CHECK_",
484
+ case_sensitive=False,
485
+ )
486
+
487
+
488
+ class AdminAuthSettings(BaseSettings):
489
+ """管理后台认证配置。
490
+
491
+ 环境变量前缀: ADMIN_AUTH_
492
+ 示例: ADMIN_AUTH_MODE, ADMIN_AUTH_SECRET_KEY, ADMIN_AUTH_BASIC_USERNAME
493
+
494
+ 说明:
495
+ - 内置模式仅保证 basic / bearer 开箱即用
496
+ - jwt/custom 推荐由用户自定义 backend 实现(见 ADMIN_AUTH_BACKEND)
497
+ """
498
+
499
+ mode: Literal["none", "basic", "bearer", "jwt", "custom"] = Field(
500
+ default="basic",
501
+ description="认证模式 (none/basic/bearer/jwt/custom)",
502
+ )
503
+
504
+ # SQLAdmin AuthenticationBackend 需要 secret_key 用于 session 签名
505
+ secret_key: str | None = Field(
506
+ default=None,
507
+ description="Session 签名密钥(生产环境必须配置)",
508
+ )
509
+
510
+ # basic:用户名/密码(用于 SQLAdmin 登录页)
511
+ basic_username: str | None = Field(default=None, description="Basic 登录用户名")
512
+ basic_password: str | None = Field(default=None, description="Basic 登录密码")
513
+
514
+ # bearer:token 白名单(支持 Authorization: Bearer <token>,也支持登录页使用 token)
515
+ bearer_tokens: list[str] = Field(default_factory=list, description="Bearer token 白名单")
516
+
517
+ # custom/jwt:用户自定义认证后端(动态导入)
518
+ backend: str | None = Field(
519
+ default=None,
520
+ description='自定义认证后端导入路径,如 "yourpkg.admin_auth:backend"',
521
+ )
522
+
523
+ model_config = SettingsConfigDict(
524
+ env_prefix="ADMIN_AUTH_",
525
+ case_sensitive=False,
526
+ )
527
+
528
+
529
+ class AdminConsoleSettings(BaseSettings):
530
+ """SQLAdmin 管理后台配置。
531
+
532
+ 环境变量前缀: ADMIN_
533
+ 示例: ADMIN_ENABLED, ADMIN_PATH, ADMIN_DATABASE_URL
534
+ """
535
+
536
+ enabled: bool = Field(default=False, description="是否启用管理后台")
537
+
538
+ path: str = Field(
539
+ default="/api/admin-console",
540
+ description="管理后台路径(默认 /api/admin-console)",
541
+ )
542
+
543
+ # SQLAdmin 目前通常要求同步 SQLAlchemy Engine;此处允许单独指定同步数据库 URL
544
+ database_url: str | None = Field(
545
+ default=None,
546
+ description="管理后台专用数据库连接(同步 URL,可覆盖自动推导)",
547
+ )
548
+
549
+ # 显式指定项目侧 admin 模块路径(可选)
550
+ views_module: str | None = Field(
551
+ default=None,
552
+ description="项目侧 admin-console 模块(用于注册 views/auth),如 app.admin_console",
553
+ )
554
+
555
+ auth: AdminAuthSettings = Field(default_factory=AdminAuthSettings, description="管理后台认证配置")
556
+
557
+ model_config = SettingsConfigDict(
558
+ env_prefix="ADMIN_",
559
+ case_sensitive=False,
560
+ )
561
+
562
+
563
+ class BaseConfig(BaseSettings):
564
+ """基础配置类。
565
+
566
+ 所有应用配置的基类,提供通用配置项。
567
+ 初始化时自动从 .env 文件加载环境变量,然后由 pydantic-settings 读取环境变量。
568
+
569
+ 注意:Application 层配置完全独立,不依赖 Infrastructure 层。
570
+ """
571
+
572
+ def __init__(self, _env_file: str | Path = ".env", **kwargs) -> None:
573
+ """初始化配置。
574
+
575
+ Args:
576
+ _env_file: .env 文件路径,默认为当前目录下的 .env
577
+ **kwargs: 其他配置参数
578
+ """
579
+ # 在 pydantic-settings 初始化之前加载 .env 文件
580
+ _load_env_file(_env_file)
581
+ super().__init__(**kwargs)
582
+
583
+ # ========== 服务器与网络 ==========
584
+ # 服务器配置
585
+ server: ServerSettings = Field(default_factory=ServerSettings)
586
+
587
+ # CORS配置
588
+ cors: CORSSettings = Field(default_factory=CORSSettings)
589
+
590
+ # 日志配置
591
+ log: LogSettings = Field(default_factory=LogSettings)
592
+
593
+ # 健康检查配置
594
+ health_check: HealthCheckSettings = Field(default_factory=HealthCheckSettings)
595
+
596
+ # 管理后台配置(SQLAdmin)
597
+ admin: AdminConsoleSettings = Field(default_factory=AdminConsoleSettings)
598
+
599
+ # ========== 数据与缓存 ==========
600
+ # 数据库配置
601
+ database: DatabaseSettings = Field(default_factory=DatabaseSettings)
602
+
603
+ # 缓存配置
604
+ cache: CacheSettings = Field(default_factory=CacheSettings)
605
+
606
+ # 对象存储配置(接入用;storage SDK 本身不读取 env)
607
+ storage: StorageSettings = Field(default_factory=StorageSettings)
608
+
609
+ # 迁移配置
610
+ migration: MigrationSettings = Field(default_factory=MigrationSettings)
611
+
612
+ # ========== 服务编排 ==========
613
+ # 服务配置
614
+ service: ServiceSettings = Field(default_factory=ServiceSettings)
615
+
616
+ # 调度器配置
617
+ scheduler: SchedulerSettings = Field(default_factory=SchedulerSettings)
618
+
619
+ # ========== 异步与事件 ==========
620
+ # 任务队列配置
621
+ task: TaskSettings = Field(default_factory=TaskSettings)
622
+
623
+ # 事件总线配置
624
+ event: EventSettings = Field(default_factory=EventSettings)
625
+
626
+ # ========== 微服务通信 ==========
627
+ # RPC 客户端配置(调用其他服务)
628
+ rpc_client: RPCClientSettings = Field(default_factory=RPCClientSettings)
629
+
630
+ # RPC 服务配置(当前服务注册)
631
+ rpc_service: RPCServiceSettings = Field(default_factory=RPCServiceSettings)
632
+
633
+ model_config = SettingsConfigDict(
634
+ case_sensitive=False,
635
+ extra="ignore",
636
+ )
637
+
638
+ @property
639
+ def is_production(self) -> bool:
640
+ """是否为生产环境。"""
641
+ return self.log.level.upper() == "ERROR"
642
+
643
+
644
+ __all__ = [
645
+ "AdminAuthSettings",
646
+ "AdminConsoleSettings",
647
+ "BaseConfig",
648
+ "CORSSettings",
649
+ "CacheSettings",
650
+ "DatabaseSettings",
651
+ "EventSettings",
652
+ "HealthCheckSettings",
653
+ "LogSettings",
654
+ "MigrationSettings",
655
+ "RPCClientSettings",
656
+ "RPCServiceSettings",
657
+ "SchedulerSettings",
658
+ "ServerSettings",
659
+ "ServiceSettings",
660
+ "StorageSettings",
661
+ "TaskSettings",
662
+ ]
663
+
@@ -0,0 +1,19 @@
1
+ """应用层常量模块。
2
+
3
+ 提供应用层配置相关的常量定义。
4
+ """
5
+
6
+ from __future__ import annotations
7
+
8
+ from .components import ComponentName, MiddlewareName
9
+ from .scheduler import SchedulerMode
10
+ from .service import ServiceType
11
+
12
+ __all__ = [
13
+ "ComponentName",
14
+ "MiddlewareName",
15
+ "SchedulerMode",
16
+ "ServiceType",
17
+ ]
18
+
19
+