58 lines
1.4 KiB
Python
58 lines
1.4 KiB
Python
from pydantic_settings import BaseSettings
|
|
from typing import Optional
|
|
|
|
|
|
class PostgreSQLSettings(BaseSettings):
|
|
"""PostgreSQL数据库配置"""
|
|
host: str = "localhost"
|
|
port: int = 5432
|
|
user: str = "postgres"
|
|
password: str = "postgres"
|
|
database: str = "book_system"
|
|
min_size: int = 5
|
|
max_size: int = 20
|
|
|
|
@property
|
|
def url(self) -> str:
|
|
"""生成数据库连接URL"""
|
|
return f"postgresql+asyncpg://{self.user}:{self.password}@{self.host}:{self.port}/{self.database}"
|
|
|
|
class Config:
|
|
env_prefix = "POSTGRES_"
|
|
|
|
|
|
class RedisSettings(BaseSettings):
|
|
"""Redis配置"""
|
|
host: str = "localhost"
|
|
port: int = 6379
|
|
password: Optional[str] = None
|
|
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 = "your-secret-key"
|
|
algorithm: str = "HS256"
|
|
access_token_expire_minutes: int = 30
|
|
|
|
class Config:
|
|
env_prefix = "APP_"
|
|
|
|
|
|
# 创建设置实例
|
|
postgres_settings = PostgreSQLSettings()
|
|
redis_settings = RedisSettings()
|
|
app_settings = AppSettings() |