feat(base): 优化环境变量配置

This commit is contained in:
xingc
2025-02-22 11:35:19 +08:00
parent e35e97402f
commit 02ddc81c51
6 changed files with 60 additions and 18 deletions

View File

@@ -16,9 +16,12 @@ REDIS_HOST = 192.168.3.4
REDIS_PORT = 6379 REDIS_PORT = 6379
REDIS_DB = 0 REDIS_DB = 0
# 文件上传配置
ASSETS_BASE_URL = http://127.0.0.1:8001
# 代理 # 代理
OVERSEAS_PROXY = 'http://127.0.0.1:7890' OVERSEAS_PROXY = http://127.0.0.1:7890
# 微信 # 微信
APPID = APPID = wx3d30771f3987cc92
APP_SECRET = APP_SECRET = 8e57b1b5344d1dbd058cd45f3b2c4d3c

View File

@@ -5,7 +5,7 @@
# @Author : xingc # @Author : xingc
# @Desc : # @Desc :
import logging import logging
import os from pathlib import Path
from pydantic_settings import BaseSettings, SettingsConfigDict from pydantic_settings import BaseSettings, SettingsConfigDict
@@ -15,7 +15,7 @@ class Settings(BaseSettings):
NAME: str = 'card book' NAME: str = 'card book'
PREFIX: str = NAME.lower().replace(' ', '_') PREFIX: str = NAME.lower().replace(' ', '_')
ENVIRONMENT: str = 'dev' ENVIRONMENT: str = 'dev'
BASE_DIR: str = os.path.dirname(os.path.abspath(__file__)) BASE_DIR: Path = Path(__file__).resolve().parent
DEBUG: bool = True DEBUG: bool = True
# 应用配置 # 应用配置
@@ -40,30 +40,30 @@ class Settings(BaseSettings):
REDIS_HOST: str REDIS_HOST: str
REDIS_PORT: int = 6379 REDIS_PORT: int = 6379
REDIS_DB: int = 0 REDIS_DB: int = 0
REDIS_CACHE_EXPIRATION_SECONDS: int = 60 * 30 * 1 REDIS_CACHE_EXPIRATION_SECONDS: int = 60 * 5 * 1
# 身份认证 # 身份认证
ACCESS_SECRET_KEY: str = 'a2f718e8313bf8efcb45ef7364d040c45800f34d10f65776eef0f6584677ecd0' ACCESS_SECRET_KEY: str = 'a2f718e8313bf8efcb45ef7364d040c45800f34d10f65776eef0f6584677ecd0'
REFRESH_SECRET_KEY: str = 'bb0cbf245de98c09c42c928326f6e07a44d91bc5a81787b37b4eb33428915fa7'
ALGORITHM: str = 'HS256' ALGORITHM: str = 'HS256'
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 7 ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 7
# 文件上传配置 # 文件上传配置
UPLOADED_FILES_DIR: str = os.path.join(BASE_DIR, 'uploads') UPLOADED_FILES_DIR: Path = BASE_DIR / 'uploads'
UPLOADED_ALLOWED_MIME_TYPES: set = { UPLOADED_ALLOWED_MIME_TYPES: set = {
"image/jpeg", "image/jpeg",
"image/png" "image/png"
} }
UPLOADED_MAX_FILE_SIZE: int = 1024 * 1024 * 10 # 10MB 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' OVERSEAS_PROXY: str = 'http://127.0.0.1:7890'
# 微信 # 微信
APPID: str = '<KEY>' APPID: str = ''
APP_SECRET: str = '<KEY>' APP_SECRET: str = ''
model_config = SettingsConfigDict(env_file=F'.env.{ENVIRONMENT.lower()}', env_file_encoding='utf-8') model_config = SettingsConfigDict(env_file=f'.env.{ENVIRONMENT}', env_file_encoding='utf-8')
class ProductionSettings(Settings): class ProductionSettings(Settings):
@@ -73,7 +73,6 @@ class ProductionSettings(Settings):
# 身份认证 # 身份认证
ACCESS_SECRET_KEY: str = '1f790219840ccd10b5d8b33d7f595b7fe0eed262403689f6fa1cab80149b00f2' ACCESS_SECRET_KEY: str = '1f790219840ccd10b5d8b33d7f595b7fe0eed262403689f6fa1cab80149b00f2'
REFRESH_SECRET_KEY: str = 'fb57c85dc4e0256024ae809bc3200adc7b7de222b348da6df35d58e27338e3b8'
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 1 # 1days ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 1 # 1days
@@ -95,6 +94,7 @@ class DevelopmentSettings(Settings):
def get_settings(): def get_settings():
import os
env = os.environ.get('ENVIRONMENT', 'dev') env = os.environ.get('ENVIRONMENT', 'dev')
if env == 'dev': if env == 'dev':
return DevelopmentSettings() return DevelopmentSettings()

View File

@@ -35,6 +35,10 @@ def create_app() -> FastAPI:
for router in routers: for router in routers:
app.include_router(router) app.include_router(router)
if settings.ENVIRONMENT != 'prod':
from fastapi.staticfiles import StaticFiles
app.mount("/uploads", StaticFiles(directory="uploads"), name="uploads")
@app.on_event('startup') @app.on_event('startup')
async def startup_event(): async def startup_event():
logger.info(f'Application <<{settings.NAME}>> is started') logger.info(f'Application <<{settings.NAME}>> is started')

View File

@@ -5,15 +5,25 @@
# @Author : xingc # @Author : xingc
# @Desc : # @Desc :
class BaseException(Exception): class CardBookException(Exception):
pass pass
class SearchCardNotFound(Exception): class ScraperError(CardBookException):
""""采集错误"""
pass
class SearchCardNotFound(ScraperError):
"""搜索卡片不存在""" """搜索卡片不存在"""
pass pass
class SearchCardRetry(Exception): class SearchCardRetry(ScraperError):
"""搜索重试""" """搜索重试"""
pass pass
class WechatApiError(CardBookException):
"""微信开放服务调用api错误"""
pass

View File

@@ -20,9 +20,9 @@ class BaseResponse(BaseModel, Generic[T]):
class Paginated(BaseModel, Generic[T]): class Paginated(BaseModel, Generic[T]):
list: Optional[T] = None list: Optional[T] = None
total: int total: int = 0
page: int page: int = 1
page_size: int page_size: int = 15
class SupportPlatformType(enum.Enum): class SupportPlatformType(enum.Enum):

View File

@@ -6,8 +6,11 @@
# @Desc : # @Desc :
import asyncio import asyncio
import functools import functools
from datetime import datetime
from typing import Callable from typing import Callable
from config import settings
def async_retry(max_retries: int = 3, exceptions: tuple = (Exception,)): def async_retry(max_retries: int = 3, exceptions: tuple = (Exception,)):
""" """
@@ -32,3 +35,25 @@ def async_retry(max_retries: int = 3, exceptions: tuple = (Exception,)):
return wrapper return wrapper
return decorator return decorator
def generate_keyname(name: str, use_today: bool = False) -> str:
"""生成统一键名前缀"""
if name.startswith(settings.PREFIX) is False:
names = [settings.PREFIX, name]
if use_today:
today = datetime.now().strftime('%Y%m%d')
names.insert(1, today)
return ':'.join(names)
else:
return name
def check_picture_for_empty(picture: dict, or_: bool = False) -> bool:
"""检查图片是否为空"""
results = [
picture.get('front_picture') is None,
picture.get('reverse_picture') is None
]
return any(results) if or_ else all(results)