Replies: 8 comments
-
From the FastAPI project-generator codebase: # app/api/api_v1/endpoints/items.py
...
from fastapi import APIRouter, Depends, HTTPException
...
router = APIRouter()
@router.get("/", response_model=List[Item])
... # app/api/api_v1/api.py
from fastapi import APIRouter
from app.api.api_v1.endpoints import items, login, users, utils
api_router = APIRouter()
api_router.include_router(login.router, tags=["login"])
api_router.include_router(users.router, prefix="/users", tags=["users"])
api_router.include_router(utils.router, prefix="/utils", tags=["utils"])
api_router.include_router(items.router, prefix="/items", tags=["items"]) # app/main.py
from fastapi import FastAPI
...
from app.api.api_v1.api import api_router
...
app = FastAPI(title=config.PROJECT_NAME, openapi_url="/api/v1/openapi.json")
...
app.include_router(api_router, prefix=config.API_V1_STR)
... |
Beta Was this translation helpful? Give feedback.
-
Great, thanks! |
Beta Was this translation helpful? Give feedback.
-
Thanks @dmontagu! : 🍰 @yihuang the docs about that are here: https://fastapi.tiangolo.com/tutorial/bigger-applications/ |
Beta Was this translation helpful? Give feedback.
-
Hi, guys, I think this is great, thanks. But when using this strategy, It's easy to get 302, why? For example: |
Beta Was this translation helpful? Give feedback.
-
@markqiu it’s because you are creating an endpoint with path If you don’t want your paths to end in a |
Beta Was this translation helpful? Give feedback.
-
@dmontagu But If a path includes just a single slash, you can't define the endpoint decorators like this: |
Beta Was this translation helpful? Give feedback.
-
Right, you'd have to give it a name (e.g. You can mount multiple routers with the same prefix if, for example, you want to have multiple distinct resources with the same prefix and different non-slash-ending endpoints: from fastapi import FastAPI, APIRouter
app = FastAPI()
router1 = APIRouter()
router2 = APIRouter()
@router1.get("/items")
async def read_items():
return []
@router2.get("/things")
async def read_things():
return []
app.include_router(router1, prefix="")
app.include_router(router2, prefix="")
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='0.0.0.0', port=8000) |
Beta Was this translation helpful? Give feedback.
-
ok, thanks. |
Beta Was this translation helpful? Give feedback.
-
When I sperate apis into multiple module, I find it hard to structure the code, currently I approach like this:
Any suggestions?
Beta Was this translation helpful? Give feedback.
All reactions