Admin Config
配置管理模块,提供统一的配置加载和管理功能。
功能特性
- ✅ TOML 配置文件支持
- ✅ 环境变量覆盖
- ✅ 类型安全的配置结构
- ✅ 默认值支持
- ✅ 多数据库实例配置
快速开始
添加依赖
[]
= { = "../admin-config" }
使用配置
use AppConfig;
配置方式
支持三种配置方式,优先级从高到低:
- 环境变量(最高优先级)- 支持两种格式:
- 简写格式:
PORT,REDIS_IP,MONGODB_USER等 - 层级格式:
SERVER__PORT,REDIS__HOST,DATABASE__MONGODB__USERNAME等
- 简写格式:
- config.toml 配置文件
- 默认值(最低优先级)
配置文件路径可通过 CONFIG_PATH 环境变量指定,默认为 config.toml。
配置文件示例
服务器配置
[]
= "actix-admin-server"
= "0.1.0"
= 3400 # 环境变量: PORT 或 SERVER__PORT
= "0.0.0.0"
= "info" # 环境变量: RUST_LOG 或 SERVER__LOG_LEVEL
数据库配置(支持多实例)
# MongoDB - 支持配置多个实例
[[]]
= "localhost" # 环境变量: MONGODB_IP
= 27017 # 环境变量: MONGODB_PORT
= "admin_db"
= "admin" # 环境变量: MONGODB_USER
= "password" # 环境变量: MONGODB_PASSWORD
= 10
= 10
# MySQL - 支持配置多个实例
[[]]
= "localhost"
= 3306
= "app_db"
= "root"
= "password"
= 10
= 10
# PostgreSQL
[[]]
= "localhost"
= 5432
= "app_db"
= "postgres"
= "password"
= 10
= 10
# SQLite
[[]]
= "./data.db"
= 10
# Qdrant (向量数据库)
[[]]
= "localhost"
= 6333
= false
= "" # 可选
# SurrealDB
[[]]
= "localhost"
= 8000
= "app"
= "main"
= "root"
= "root"
= false
Redis 配置
[]
= "localhost" # 环境变量: REDIS_IP 或 REDIS__HOST
= 6379 # 环境变量: REDIS_PORT 或 REDIS__PORT
= "" # 可选
= "" # 环境变量: REDIS_PASSWORD 或 REDIS__PASSWORD
= 0
= 20
= 5
认证配置
[]
= "your-secret-key" # 环境变量: TOKEN_SECRET
= 24
= 7
LLM 配置(支持多个)
[[]]
= "openai"
= "sk-xxx"
= "gpt-4"
= 2048
= 0.7
[[]]
= "anthropic"
= "sk-ant-xxx"
= "claude-3-sonnet"
= 4096
= 0.7
其他配置
[]
= "smtp.gmail.com"
= 587
= ""
= ""
= "系统通知"
= ""
= true
[]
= "tencent" # tencent/aliyun
= ""
= ""
= "您的应用"
= "123456"
[]
= 6
= 300 # 秒
= 60 # 秒
[]
= ""
= ""
= ""
= "ap-guangzhou"
= "" # 可选
[]
= ""
= ""
= true
= "*"
= false
[]
= ""
= 86400 # 秒
= true
= false
= "/"
[]
= "image/jpeg,image/png,image/gif,image/webp,application/pdf"
= 10 # MB
= "./uploads"
= "./temp"
[]
= true
= 60
= 10
[]
= false
= false
= false
= true
环境变量示例
创建 .env 文件:
# 服务器
PORT=8080
RUST_LOG=debug
# 数据库
MONGODB_IP=127.0.0.1
MONGODB_PORT=27017
MONGODB_USER=admin
MONGODB_PASSWORD=secret
# Redis
REDIS_IP=127.0.0.1
REDIS_PORT=6379
REDIS_PASSWORD=secret
# 认证
TOKEN_SECRET=your-secret-key-here
# LLM
LLM_PROVIDER=openai
LLM_API_KEY=sk-xxx
LLM_MODEL=gpt-4
辅助方法
// 获取数据库连接字符串
let mongo_url = config.database.mongodb.connection_string;
let mysql_url = config.database.mysql.connection_string;
let redis_url = config.redis.connection_string;
// Token 过期时间(秒)
let token_ttl = config.auth.token_expiry_seconds;
let refresh_ttl = config.auth.refresh_token_expiry_seconds;
// 上传文件大小限制(字节)
let max_size = config.upload.max_file_size_bytes;
// 允许的文件类型列表
let allowed_types = config.upload.allowed_types_list;
生产环境建议
- 使用环境变量存储敏感信息(密码、密钥)
- 定期更新密钥和密码
- 启用 HTTPS 和 Secure Cookie
- 设置适当的 CORS 策略
- 启用限流保护
许可
MIT