391f3df1bb
把 main 的"美团 CPS 模块 + 后端分层重构(integrations/repositories)"合入领券分支。 解决 3 处冲突(均为两边各自新增, 保留双方): - app/main.py: 同时注册 coupon_router 与 meituan_router - app/core/config.py: 同时保留 PRICEBOT_* 与 MT_CPS_* 配置 - .env.example: 同上 coupon.py 本身无冲突(仅依赖 deps.CurrentUser 与 core.config, 重构未触及)。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
64 lines
1.7 KiB
Python
64 lines
1.7 KiB
Python
"""FastAPI 入口。
|
|
|
|
通过 `uvicorn app.main:app --reload` 启动。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from collections.abc import AsyncIterator
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from app.api.v1.auth import router as auth_router
|
|
from app.api.v1.coupon import router as coupon_router
|
|
from app.api.v1.meituan import router as meituan_router
|
|
from app.core.config import settings
|
|
from app.core.logging import setup_logging
|
|
|
|
setup_logging(debug=settings.APP_DEBUG)
|
|
logger = logging.getLogger("shagua.main")
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
|
|
# 提示而非强制建表:生产用 alembic upgrade head,本地 dev 也建议先跑一次 migration。
|
|
# 这里只 log,不自动 create_all,避免生产环境意外建出过时 schema。
|
|
logger.info(
|
|
"started env=%s debug=%s db=%s",
|
|
settings.APP_ENV,
|
|
settings.APP_DEBUG,
|
|
settings.DATABASE_URL.split("://", 1)[0],
|
|
)
|
|
yield
|
|
logger.info("shutting down")
|
|
|
|
|
|
app = FastAPI(
|
|
title=settings.APP_NAME,
|
|
version="0.1.0",
|
|
docs_url="/docs" if not settings.is_prod else None,
|
|
redoc_url="/redoc" if not settings.is_prod else None,
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
if settings.cors_origins_list:
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.cors_origins_list,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
|
|
@app.get("/health", tags=["meta"])
|
|
def health() -> dict[str, str]:
|
|
return {"status": "ok"}
|
|
|
|
|
|
app.include_router(auth_router)
|
|
app.include_router(coupon_router)
|
|
app.include_router(meituan_router)
|