Skip to content

configs 模块设计说明 / Design Documentation for configs Module

简介 / Introduction

configs 模块是 TF Robot Server 项目的核心部分,负责处理所有配置相关的任务。该模块通过灵活的设计,提供了一种有效的方式来加载和管理应用配置。

The configs module is a core component of the TF Robot Server project, responsible for handling all configuration-related tasks. This module, through its flexible design, offers an efficient way to load and manage application configurations.

第三方库 / Third-Party Libraries

此模块主要依赖以下第三方库:

  • Confz: 用于管理和加载配置文件。
  • FastAPI: 用于与 FastAPI 框架集成。
  • Pydantic: 用于定义配置模型和数据验证。
  • Redis: 用于 Redis 配置管理。

This module primarily relies on the following third-party libraries:

  • Confz: For managing and loading configuration files.
  • FastAPI: For integration with the FastAPI framework.
  • Pydantic: For defining configuration models and data validation.
  • Redis: For Redis configuration management.

设计模式 / Design Pattern

configs 模块使用了基于类的设计模式,每个配置文件对应一个类。这些类继承自 BaseConfig 类,并使用了 Confz 库来加载配置。这种设计模式提高了代码的可维护性和可扩展性。

The configs module utilizes a class-based design pattern, with each configuration file corresponding to a class. These classes inherit from the BaseConfig class and use the Confz library for loading configurations. This design pattern enhances the maintainability and extensibility of the code.

与 FastAPI 的集成 / Integration with FastAPI

configs 模块与 FastAPI 的集成体现在几个方面:

  • CORS 配置: 利用 FastAPI 的中间件机制,实现了跨域资源共享配置。
  • Redis 配置: 提供了 Redis 实例的初始化和拆解功能,与 FastAPI 生命周期钩子相集成。
  • 追踪配置: 配置了追踪日志,以便与 FastAPI 应用的请求响应周期同步。

The integration of the configs module with FastAPI is evident in several aspects:

  • CORS Configuration: Implements Cross-Origin Resource Sharing configuration using FastAPI's middleware mechanism.
  • Redis Configuration: Provides initialization and teardown of Redis instances, integrated with FastAPI's lifecycle hooks.
  • Tracing Configuration: Configures tracing logs to synchronize with the FastAPI application's request-response cycle.

配置文件 / Configuration Files

configs 模块默认识别的飘转文件在tfrobotserver/resources目录下。配置文件的命名规则如下:

tfrobotserver-{dev/test/prod}.{cfg_type}

其中cfg_type取决于环境变量:TFS_CONFIG_FORMAT。如果未设置此环境变量,默认toml格式。

The configuration files recognized by the configs module are located in the tfrobotserver/resources directory. The naming convention for configuration files is as follows:

tfrobotserver-{dev/test/prod}.{cfg_type}

Where cfg_type depends on the environment variable: TFS_CONFIG_FORMAT. If this environment variable is not set, the default format is toml.

环境变量 / Environment Variables

configs 模块会读取全部环境变量,并且与当前配置项进行匹配。环境变量的要求如下:

  1. TFS_开始,后面跟着配置项的大写形式
  2. 对于嵌套的配置项,使用.分隔符