2025-04-11 08:54:28 +08:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
# @version : 1.0
|
|
|
|
|
# @Create Time : 2021/10/19 15:47
|
|
|
|
|
# @File : main.py
|
|
|
|
|
# @IDE : PyCharm
|
|
|
|
|
# @desc : 主程序入口
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
FastApi 更新文档:https://github.com/tiangolo/fastapi/releases
|
|
|
|
|
FastApi Github:https://github.com/tiangolo/fastapi
|
|
|
|
|
Typer 官方文档:https://typer.tiangolo.com/
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from application import urls
|
|
|
|
|
from core.event import lifespan
|
2025-04-17 16:25:44 +08:00
|
|
|
|
from application import settings
|
2025-04-11 08:54:28 +08:00
|
|
|
|
from utils.tools import import_modules
|
2025-04-17 16:25:44 +08:00
|
|
|
|
from core.exception import register_exception
|
|
|
|
|
from core.websocket_app import websocket_config
|
2025-04-11 08:54:28 +08:00
|
|
|
|
|
2025-04-17 16:25:44 +08:00
|
|
|
|
import typer
|
|
|
|
|
import uvicorn
|
|
|
|
|
from fastapi import FastAPI
|
|
|
|
|
from starlette.middleware.cors import CORSMiddleware
|
2025-04-11 08:54:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_app():
|
|
|
|
|
"""
|
|
|
|
|
启动项目
|
|
|
|
|
|
|
|
|
|
docs_url:配置交互文档的路由地址,如果禁用则为None,默认为 /docs
|
|
|
|
|
redoc_url: 配置 Redoc 文档的路由地址,如果禁用则为None,默认为 /redoc
|
|
|
|
|
openapi_url:配置接口文件json数据文件路由地址,如果禁用则为None,默认为/openapi.json
|
|
|
|
|
"""
|
|
|
|
|
app = FastAPI(
|
|
|
|
|
title="aicheckv2-api",
|
2025-04-17 16:25:44 +08:00
|
|
|
|
description="aicheckv2.0的后台api",
|
2025-04-11 08:54:28 +08:00
|
|
|
|
version=settings.VERSION,
|
|
|
|
|
lifespan=lifespan,
|
|
|
|
|
docs_url=None,
|
|
|
|
|
redoc_url=None
|
|
|
|
|
)
|
|
|
|
|
import_modules(settings.MIDDLEWARES, "中间件", app=app)
|
|
|
|
|
# 全局异常捕捉处理
|
|
|
|
|
register_exception(app)
|
|
|
|
|
# 跨域解决
|
|
|
|
|
if settings.CORS_ORIGIN_ENABLE:
|
|
|
|
|
app.add_middleware(
|
|
|
|
|
CORSMiddleware,
|
|
|
|
|
allow_origins=settings.ALLOW_ORIGINS,
|
|
|
|
|
allow_credentials=settings.ALLOW_CREDENTIALS,
|
|
|
|
|
allow_methods=settings.ALLOW_METHODS,
|
|
|
|
|
allow_headers=settings.ALLOW_HEADERS
|
|
|
|
|
)
|
|
|
|
|
# 引入应用中的路由
|
|
|
|
|
for url in urls.urlpatterns:
|
|
|
|
|
app.include_router(url["ApiRouter"], prefix=url["prefix"], tags=url["tags"])
|
2025-04-17 11:03:05 +08:00
|
|
|
|
# 开启websocket
|
|
|
|
|
websocket_config(app)
|
2025-04-11 08:54:28 +08:00
|
|
|
|
return app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run(
|
|
|
|
|
host: str = typer.Option(default='0.0.0.0', help='监听主机IP,默认开放给本网络所有主机'),
|
|
|
|
|
port: int = typer.Option(default=9000, help='监听端口')
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
启动项目
|
|
|
|
|
|
|
|
|
|
factory: 在使用 uvicorn.run() 启动 ASGI 应用程序时,可以通过设置 factory 参数来指定应用程序工厂。
|
|
|
|
|
应用程序工厂是一个返回 ASGI 应用程序实例的可调用对象,它可以在启动时动态创建应用程序实例。
|
|
|
|
|
"""
|
|
|
|
|
uvicorn.run(app='main:create_app', host=host, port=port, lifespan="on", factory=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2025-04-17 16:25:44 +08:00
|
|
|
|
run(host='0.0.0.0', port=9000)
|