- 添加用户登录、登出及令牌管理功能 - 实现基于JWT和Redis的认证系统 - 完善用户和角色管理API - 添加密码加密与验证功能 - 配置数据库连接和Redis客户端 - 实现中间件进行权限验证 - 初始化管理员和测试用户数据 - 更新模型和接口文档 - 添加测试API和配置文件
60 lines
1.6 KiB
Python
60 lines
1.6 KiB
Python
from pydantic_settings import BaseSettings
|
|
from typing import Optional
|
|
from urllib.parse import quote_plus
|
|
|
|
|
|
class PostgreSQLSettings(BaseSettings):
|
|
"""PostgreSQL数据库配置"""
|
|
host: str = "127.0.0.1"
|
|
port: int = 5432
|
|
user: str = "admin"
|
|
password: str = "PostgresAdmin@123"
|
|
database: str = "booksystem"
|
|
min_size: int = 5
|
|
max_size: int = 20
|
|
|
|
@property
|
|
def url(self) -> str:
|
|
"""生成数据库连接URL"""
|
|
encoded_password = quote_plus(self.password)
|
|
return f"postgresql+asyncpg://{self.user}:{encoded_password}@{self.host}:{self.port}/{self.database}"
|
|
|
|
class Config:
|
|
env_prefix = "POSTGRES_"
|
|
|
|
|
|
class RedisSettings(BaseSettings):
|
|
"""Redis配置"""
|
|
host: str = "127.0.0.1"
|
|
port: int = 6379
|
|
password: Optional[str] = "RedisAdmin@123"
|
|
db: int = 0
|
|
|
|
@property
|
|
def url(self) -> str:
|
|
"""生成Redis连接URL"""
|
|
if self.password:
|
|
return f"redis://:{self.password}@{self.host}:{self.port}/{self.db}"
|
|
return f"redis://{self.host}:{self.port}/{self.db}"
|
|
|
|
class Config:
|
|
env_prefix = "REDIS_"
|
|
|
|
|
|
class AppSettings(BaseSettings):
|
|
"""应用配置"""
|
|
name: str = "Book System API"
|
|
version: str = "1.0.0"
|
|
debug: bool = False
|
|
secret_key: str = "BookSystemAPIQRAdmin123"
|
|
algorithm: str = "HS256"
|
|
access_token_expire_minutes: int = 30
|
|
|
|
class Config:
|
|
env_prefix = "APP_"
|
|
|
|
|
|
# 创建设置实例
|
|
postgres_settings = PostgreSQLSettings()
|
|
redis_settings = RedisSettings()
|
|
app_settings = AppSettings() |