标注一下,这是一个可以启动的版本
This commit is contained in:
72
main.py
72
main.py
@ -11,22 +11,17 @@ FastApi Github:https://github.com/tiangolo/fastapi
|
||||
Typer 官方文档:https://typer.tiangolo.com/
|
||||
"""
|
||||
|
||||
from fastapi import FastAPI
|
||||
import uvicorn
|
||||
from starlette.middleware.cors import CORSMiddleware
|
||||
from application import settings
|
||||
from application import urls
|
||||
from starlette.staticfiles import StaticFiles # 依赖安装:pip install aiofiles
|
||||
from core.websocket_app import websocket_config
|
||||
from core.exception import register_exception
|
||||
import typer
|
||||
from scripts.initialize.initialize import InitializeData, Environment
|
||||
import asyncio
|
||||
from scripts.create_app.main import CreateApp
|
||||
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
|
||||
|
||||
shell_app = typer.Typer()
|
||||
import typer
|
||||
import uvicorn
|
||||
from fastapi import FastAPI
|
||||
from starlette.middleware.cors import CORSMiddleware
|
||||
|
||||
|
||||
def create_app():
|
||||
@ -39,7 +34,7 @@ def create_app():
|
||||
"""
|
||||
app = FastAPI(
|
||||
title="aicheckv2-api",
|
||||
description="aicheckv2.1版本,在之前v2.0的版本上找一个框架对项目,用户和部门进行统一管理",
|
||||
description="aicheckv2.0的后台api",
|
||||
version=settings.VERSION,
|
||||
lifespan=lifespan,
|
||||
docs_url=None,
|
||||
@ -57,20 +52,14 @@ def create_app():
|
||||
allow_methods=settings.ALLOW_METHODS,
|
||||
allow_headers=settings.ALLOW_HEADERS
|
||||
)
|
||||
# 挂在静态目录
|
||||
if settings.STATIC_ENABLE:
|
||||
app.mount(settings.STATIC_URL, app=StaticFiles(directory=settings.STATIC_ROOT))
|
||||
# 引入应用中的路由
|
||||
for url in urls.urlpatterns:
|
||||
app.include_router(url["ApiRouter"], prefix=url["prefix"], tags=url["tags"])
|
||||
# 配置接口文档静态资源
|
||||
# custom_api_docs(app)
|
||||
# 开启websocket
|
||||
websocket_config(app)
|
||||
return app
|
||||
|
||||
|
||||
@shell_app.command()
|
||||
def run(
|
||||
host: str = typer.Option(default='0.0.0.0', help='监听主机IP,默认开放给本网络所有主机'),
|
||||
port: int = typer.Option(default=9000, help='监听端口')
|
||||
@ -84,48 +73,5 @@ def run(
|
||||
uvicorn.run(app='main:create_app', host=host, port=port, lifespan="on", factory=True)
|
||||
|
||||
|
||||
@shell_app.command()
|
||||
def init(env: Environment = Environment.pro):
|
||||
"""
|
||||
初始化数据
|
||||
|
||||
在执行前一定要确认要操作的环境与application/settings.DEBUG 设置的环境是一致的,
|
||||
不然会导致创建表和生成数据不在一个数据库中!!!!!!!!!!!!!!!!!!!!!!
|
||||
|
||||
比如要初始化开发环境,那么env参数应该为 dev,并且 application/settings.DEBUG 应该 = True
|
||||
比如要初始化生产环境,那么env参数应该为 pro,并且 application/settings.DEBUG 应该 = False
|
||||
|
||||
:param env: 数据库环境
|
||||
"""
|
||||
print("开始初始化数据")
|
||||
data = InitializeData()
|
||||
asyncio.run(data.run(env))
|
||||
|
||||
|
||||
@shell_app.command()
|
||||
def migrate(env: Environment = Environment.pro):
|
||||
"""
|
||||
将模型迁移到数据库,更新数据库表结构
|
||||
|
||||
:param env: 数据库环境
|
||||
"""
|
||||
print("开始更新数据库表")
|
||||
InitializeData.migrate_model(env)
|
||||
|
||||
|
||||
@shell_app.command()
|
||||
def init_app(path: str):
|
||||
"""
|
||||
自动创建初始化 APP 结构
|
||||
|
||||
命令例子:python main.py init-app vadmin/test
|
||||
|
||||
:param path: app 路径,根目录为apps,填写apps后面路径即可,例子:vadmin/auth
|
||||
"""
|
||||
print(f"开始创建并初始化 {path} APP")
|
||||
app = CreateApp(path)
|
||||
app.run()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
shell_app()
|
||||
run(host='0.0.0.0', port=9000)
|
||||
|
Reference in New Issue
Block a user