mirror of
https://github.com/harry0703/MoneyPrinterTurbo.git
synced 2026-11-14 18:22:59 +01:00
Unify task lifecycle handling across the API, CLI, and WebUI. Persist stage and error details, recover interrupted tasks, and keep social publishing independent from the video generation pipeline. Expand API, state, task, and CLI coverage, and add Linux and Windows CI checks with branch coverage enforcement.
91 lines
2.6 KiB
Python
91 lines
2.6 KiB
Python
"""Application implementation - ASGI."""
|
|
|
|
import os
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.exceptions import RequestValidationError
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import JSONResponse
|
|
from fastapi.staticfiles import StaticFiles
|
|
from loguru import logger
|
|
|
|
from app.config import config
|
|
from app.models.exception import HttpException
|
|
from app.router import root_api_router
|
|
from app.utils import utils
|
|
|
|
|
|
@asynccontextmanager
|
|
async def application_lifespan(_: FastAPI):
|
|
"""集中处理 API 进程启动恢复和关闭日志。"""
|
|
logger.info("startup event")
|
|
|
|
# 跨平台发布由当前进程线程池执行,不会在服务重启后恢复。启动时把 Redis
|
|
# 中确认已失去执行进程的活动状态收敛为失败,避免任务永久无法删除。
|
|
from app.services import task as task_service
|
|
|
|
task_service.recover_interrupted_cross_posts()
|
|
try:
|
|
yield
|
|
finally:
|
|
logger.info("shutdown event")
|
|
|
|
|
|
def exception_handler(request: Request, e: HttpException):
|
|
return JSONResponse(
|
|
status_code=e.status_code,
|
|
content=utils.get_response(e.status_code, e.data, e.message),
|
|
)
|
|
|
|
|
|
def validation_exception_handler(request: Request, e: RequestValidationError):
|
|
return JSONResponse(
|
|
status_code=400,
|
|
content=utils.get_response(
|
|
status=400, data=e.errors(), message="field required"
|
|
),
|
|
)
|
|
|
|
|
|
def get_application() -> FastAPI:
|
|
"""Initialize FastAPI application.
|
|
|
|
Returns:
|
|
FastAPI: Application object instance.
|
|
|
|
"""
|
|
instance = FastAPI(
|
|
title=config.project_name,
|
|
description=config.project_description,
|
|
version=config.project_version,
|
|
debug=False,
|
|
lifespan=application_lifespan,
|
|
)
|
|
instance.include_router(root_api_router)
|
|
instance.add_exception_handler(HttpException, exception_handler)
|
|
instance.add_exception_handler(RequestValidationError, validation_exception_handler)
|
|
return instance
|
|
|
|
|
|
app = get_application()
|
|
|
|
# Configures the CORS middleware for the FastAPI app
|
|
cors_allowed_origins_str = os.getenv("CORS_ALLOWED_ORIGINS", "")
|
|
origins = cors_allowed_origins_str.split(",") if cors_allowed_origins_str else ["*"]
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=origins,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
task_dir = utils.task_dir()
|
|
app.mount(
|
|
"/tasks", StaticFiles(directory=task_dir, html=True, follow_symlink=True), name=""
|
|
)
|
|
|
|
public_dir = utils.public_dir()
|
|
app.mount("/", StaticFiles(directory=public_dir, html=True), name="")
|