重构后端服务架构并优化前端错误处理
This commit is contained in:
45
fastapi-backend/app/main.py
Normal file
45
fastapi-backend/app/main.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from fastapi import FastAPI, Request, Depends
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from app.api import auth, users, orders, payments
|
||||
from app.core.config import settings
|
||||
from app.db.database import engine, Base
|
||||
from app.middleware.error_handler import error_handler
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||
|
||||
# Create database tables
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
app = FastAPI(
|
||||
title="活牛采购智能数字化系统API",
|
||||
description="活牛采购智能数字化系统后端API接口",
|
||||
version="1.0.0",
|
||||
)
|
||||
|
||||
# Add CORS middleware
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"], # In production, specify exact origins
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
# Add exception handlers
|
||||
app.add_exception_handler(StarletteHTTPException, error_handler)
|
||||
app.add_exception_handler(RequestValidationError, error_handler)
|
||||
app.add_exception_handler(Exception, error_handler)
|
||||
|
||||
# Include routers
|
||||
app.include_router(auth.router, prefix="/api/auth", tags=["auth"])
|
||||
app.include_router(users.router, prefix="/api/users", tags=["users"])
|
||||
app.include_router(orders.router, prefix="/api/orders", tags=["orders"])
|
||||
app.include_router(payments.router, prefix="/api/payments", tags=["payments"])
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
return {"message": "活牛采购智能数字化系统后端服务", "version": "1.0.0"}
|
||||
|
||||
@app.get("/health")
|
||||
async def health_check():
|
||||
return {"status": "healthy"}
|
||||
Reference in New Issue
Block a user