Files
X25020501_card_book/config.py
2025-02-22 11:35:19 +08:00

131 lines
3.2 KiB
Python

# -*- coding = utf-8 -*-
# @Time : 2025/2/12 下午4:55
# @File : config.py
# @Software : PyCharm
# @Author : xingc
# @Desc :
import logging
from pathlib import Path
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
# 基础设置
NAME: str = 'card book'
PREFIX: str = NAME.lower().replace(' ', '_')
ENVIRONMENT: str = 'dev'
BASE_DIR: Path = Path(__file__).resolve().parent
DEBUG: bool = True
# 应用配置
ALLOWED_ORIGINS: str = 'http://127.0.0.1:3000,http://localhost:3000'
# 日志配置
LOG_LEVEL: int = logging.DEBUG
# 数据库配置
DB_USER: str
DB_PASSWORD: str
DB_HOST: str
DB_PORT: int = 3306
DB_NAME: str
DB_SCHEMA: str = "mysql+aiomysql"
DB_POOL_SIZE: int = 30
# 指定单个数据库url
DB_URL: str | None = None
# redis配置
REDIS_PASSWORD: str
REDIS_HOST: str
REDIS_PORT: int = 6379
REDIS_DB: int = 0
REDIS_CACHE_EXPIRATION_SECONDS: int = 60 * 5 * 1
# 身份认证
ACCESS_SECRET_KEY: str = 'a2f718e8313bf8efcb45ef7364d040c45800f34d10f65776eef0f6584677ecd0'
ALGORITHM: str = 'HS256'
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 7
# 文件上传配置
UPLOADED_FILES_DIR: Path = BASE_DIR / 'uploads'
UPLOADED_ALLOWED_MIME_TYPES: set = {
"image/jpeg",
"image/png"
}
UPLOADED_MAX_FILE_SIZE: int = 1024 * 1024 * 10 # 10MB
ASSETS_BASE_URL: str = 'http://127.0.0.1:8000'
# 代理
OVERSEAS_PROXY: str = 'http://127.0.0.1:7890'
# 微信
APPID: str = ''
APP_SECRET: str = ''
model_config = SettingsConfigDict(env_file=f'.env.{ENVIRONMENT}', env_file_encoding='utf-8')
class ProductionSettings(Settings):
# 基础设置
ENVIRONMENT: str = 'prod'
DEBUG: bool = False
# 身份认证
ACCESS_SECRET_KEY: str = '1f790219840ccd10b5d8b33d7f595b7fe0eed262403689f6fa1cab80149b00f2'
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 1 # 1days
class DevelopmentSettings(Settings):
# 日志配置
LOG_LEVEL: int = logging.INFO
# 数据库配置
DB_USER: str = 'card_book'
DB_PASSWORD: str = 'DFCPjk4rdkca6SPE'
DB_HOST: str = '192.168.3.4'
DB_NAME: str = 'card_book'
# redis配置
REDIS_PASSWORD: str = 'xingc'
REDIS_HOST: str = '192.168.3.4'
REDIS_PORT: int = 6379
REDIS_DB: int = 0
def get_settings():
import os
env = os.environ.get('ENVIRONMENT', 'dev')
if env == 'dev':
return DevelopmentSettings()
elif env == 'prod':
return ProductionSettings()
return Settings()
settings = get_settings()
LOGGING_CONFIG: dict = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"standard": {"format": "%(asctime)s [%(levelname)s] %(name)s: %(message)s"},
},
"handlers": {
"default": {
"level": settings.LOG_LEVEL,
"formatter": "standard",
"class": "logging.StreamHandler",
"stream": "ext://sys.stdout",
},
},
"loggers": {
"": {"handlers": ["default"], "level": settings.LOG_LEVEL, "propagate": False},
"uvicorn": {
"handlers": ["default"],
"level": logging.ERROR,
"propagate": False,
},
},
}