
import uvicorn
import logging

from fastapi.openapi.docs import get_swagger_ui_html
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles


from app_instance import get_app
from common.global_logger import logger

app = get_app()

# 挂载静态文件夹
app.mount("/static", StaticFiles(directory="static"), name="static")


# 自定义 Swagger 文档路由，指向本地的 Swagger UI 文件
@app.get("/docs", include_in_schema=False)
async def custom_swagger_ui_html():
    return get_swagger_ui_html(
        openapi_url="/openapi.json",
        title= '安全生产一体机' + " - Swagger UI",
        swagger_js_url="/static/swagger-ui/swagger-ui-bundle.js",
        swagger_css_url="/static/swagger-ui/swagger-ui.css"
    )

# 延迟导入 router 并注册路由
from apis.router import router
app.include_router(router, prefix="/api")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

if __name__ == "__main__":
    # 重定向 uvicorn 的日志
    uvicorn_logger = logging.getLogger("uvicorn")
    uvicorn_logger.handlers = logger.handlers
    uvicorn_logger.setLevel(logging.INFO)


    uvicorn.run(app, host="0.0.0.0", port=9000, log_config=None)
