78 lines
2.5 KiB
Python
78 lines
2.5 KiB
Python
# -*- 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
|
||
from application import settings
|
||
from utils.tools import import_modules
|
||
from core.exception import register_exception
|
||
from core.websocket_app import websocket_config
|
||
|
||
import typer
|
||
import uvicorn
|
||
from fastapi import FastAPI
|
||
from starlette.middleware.cors import CORSMiddleware
|
||
|
||
|
||
def create_app():
|
||
"""
|
||
启动项目
|
||
|
||
docs_url:配置交互文档的路由地址,如果禁用则为None,默认为 /docs
|
||
redoc_url: 配置 Redoc 文档的路由地址,如果禁用则为None,默认为 /redoc
|
||
openapi_url:配置接口文件json数据文件路由地址,如果禁用则为None,默认为/openapi.json
|
||
"""
|
||
app = FastAPI(
|
||
title="aicheckv2-api",
|
||
description="aicheckv2.0的后台api",
|
||
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"])
|
||
# 开启websocket
|
||
websocket_config(app)
|
||
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__':
|
||
run(host='0.0.0.0', port=9000)
|