赞
踩
目录
在apps文件夹下新建modules文件夹
编辑modules文件夹下的__init__.py文件如下:
- from fastapi import APIRouter
- # from .user import user_api
- # from .menu import menu_api
-
- api_router = APIRouter()
-
- # api_router.include_router(user_api, prefix="/user")
- # api_router.include_router(menu_api, prefix="/menu")
-
- __all__ = ['api_router']
①在user文件夹下新建views.py内容如下:
- from fastapi import APIRouter
-
- router = APIRouter()
-
-
- @router.post("/login", summary="用户登录")
- async def login():
- return {'message': '登录成功'}
②将user接口路由导出,编辑user文件夹下的__init__.py文件如下:
from .views import router as user_api
③在menu文件夹下新建views.py内容如下:
- from fastapi import APIRouter
-
- router = APIRouter()
-
-
- @router.get("/get_list", summary="菜单列表")
- async def get_list():
- return {'data': ['用户管理', '菜单管理', '系统监控']}
④将menu接口路由导出,编辑menu文件夹下的__init__.py文件如下:
from .views import router as menu_api
编辑apps/modules文件夹下的__init__.py文件为:
- from fastapi import APIRouter
- from .user import user_api
- from .menu import menu_api
-
- api_router = APIRouter()
-
- api_router.include_router(user_api, prefix="/user")
- api_router.include_router(menu_api, prefix="/menu")
-
- __all__ = ['api_router']
- import uvicorn
- from fastapi import FastAPI
-
- from apps.modules import api_router
-
- app = FastAPI()
-
- # 跨域配置
- origins = [
- "http://localhost",
- "http://localhost:7146",
- "http://localhost:1880",
- "http://localhost:80",
- ]
-
- app.add_middleware(
- CORSMiddleware,
- allow_origins=origins,
- allow_credentials=True,
- allow_methods=["*"],
- allow_headers=["*"],
- )
-
- app.include_router(api_router, prefix='/api')
-
- if __name__ == "__main__":
- uvicorn.run("main:app", host="0.0.0.0", port=8080)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。