deliveryman-api/app/main.py

77 lines
2.9 KiB
Python

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.api.endpoints import wechat,user, address, community, station, order, coupon, community_building, upload, merchant, merchant_product, merchant_order, point, config, merchant_category, log
from app.models.database import Base, engine
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from app.core.response import error_response
from fastapi import HTTPException
from app.middleware.request_logger import RequestLoggerMiddleware
# 创建数据库表
Base.metadata.create_all(bind=engine)
app = FastAPI(
title="闪兔到家",
description="API 文档",
version="1.0.0"
)
# 配置 CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# 添加请求日志中间件
app.add_middleware(RequestLoggerMiddleware)
# 添加用户路由
app.include_router(wechat.router,prefix="/wechat",tags=["微信接口"])
app.include_router(user.router, prefix="/api/user", tags=["用户"])
app.include_router(address.router, prefix="/api/address", tags=["配送地址"])
app.include_router(community.router, prefix="/api/community", tags=["社区"])
app.include_router(community_building.router, prefix="/api/community/building", tags=["社区楼栋"])
app.include_router(station.router, prefix="/api/station", tags=["驿站"])
app.include_router(order.router, prefix="/api/order", tags=["订单"])
app.include_router(coupon.router, prefix="/api/coupon", tags=["跑腿券"])
app.include_router(upload.router, prefix="/api/upload", tags=["文件上传"])
app.include_router(merchant.router, prefix="/api/merchant", tags=["商家"])
app.include_router(merchant_category.router, prefix="/api/merchant-categories", tags=["商家分类"])
app.include_router(merchant_product.router, prefix="/api/merchant/product", tags=["商家产品"])
app.include_router(merchant_order.router, prefix="/api/merchant/order", tags=["商家订单"])
app.include_router(point.router, prefix="/api/point", tags=["用户积分"])
app.include_router(config.router, prefix="/api/config", tags=["系统配置"])
app.include_router(log.router, prefix="/api/logs", tags=["系统日志"])
@app.get("/")
async def root():
return {"message": "欢迎使用 FastAPI!"}
@app.get("/health")
async def health_check():
return {"status": "healthy"}
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
return JSONResponse(
status_code=400,
content=error_response(
code=400,
message=str(exc)
)
)
@app.exception_handler(HTTPException)
async def http_exception_handler(request, exc):
return JSONResponse(
status_code=exc.status_code,
content=error_response(
code=exc.status_code,
message=exc.detail
)
)