Files
AiDA_Python/app/main.py
zcr 893f5e87b4
All checks were successful
git commit AiDA python develop 分支构建部署 / scheduled_deploy (push) Has been skipped
3D 打板部署
2026-04-28 17:17:29 +08:00

91 lines
2.2 KiB
Python

# 1. 这里的顺序至关重要!必须在最顶端
import sys
from contextlib import asynccontextmanager
# from app.core.nacos_config import load_nacos_config, register_server, deregister_server
try:
import asyncore
except ImportError:
import pyasyncore
sys.modules['asyncore'] = pyasyncore
import logging.config
import uvicorn
from fastapi import FastAPI
from fastapi import HTTPException
from fastapi.responses import JSONResponse
from app.api.api_route import router
from app.core.config import settings
# from app.core.record_api_count import count_api_calls
from app.schemas.response_template import ResponseModel
from logging_env import LOGGER_CONFIG_DICT
from dotenv import load_dotenv
from starlette.middleware.cors import CORSMiddleware
logging.config.dictConfig(LOGGER_CONFIG_DICT)
logging.getLogger("pika").setLevel(logging.WARNING)
logger = logging.getLogger(__name__)
load_dotenv()
# @asynccontextmanager
# async def lifespan(app: FastAPI):
# try:
# load_nacos_config()
# register_server()
#
# yield
# finally:
# deregister_server()
# logger.info("lifespan down")
def get_application() -> FastAPI:
application = FastAPI(
# lifespan=lifespan,
docs_url="/docs",
redoc_url='/re-docs',
openapi_url=f"/openapi.json",
description='''
Base frame with FastAPI
- Super Resolution API
'''
)
application.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# application.middleware("http")(count_api_calls)
application.include_router(router=router)
return application
app = get_application()
@app.exception_handler(HTTPException)
async def http_exception_handler(exc: HTTPException):
return JSONResponse(
status_code=exc.status_code,
content=ResponseModel(code=exc.status_code, msg=exc.detail, data=exc.detail).dict()
)
@app.get("/health", operation_id="health")
async def health():
logger.info("health check")
return {"ok": True, "env": settings.APP_ENV}
if __name__ == '__main__':
uvicorn.run(app, host="0.0.0.0", port=settings.PORT)