30 lines
1.7 KiB
Python
30 lines
1.7 KiB
Python
Here's the `__init__.py` file for the `app/api/v1/routes/` directory, which aggregates the routers for posts, comments, tags, and users in the `blog_app_igblf` FastAPI backend:
|
|
|
|
from fastapi import APIRouter
|
|
|
|
from app.api.v1.routes.posts import router as posts_router
|
|
from app.api.v1.routes.comments import router as comments_router
|
|
from app.api.v1.routes.tags import router as tags_router
|
|
from app.api.v1.routes.users import router as users_router
|
|
|
|
router = APIRouter()
|
|
|
|
router.include_router(posts_router, prefix="/posts", tags=["posts"])
|
|
router.include_router(comments_router, prefix="/comments", tags=["comments"])
|
|
router.include_router(tags_router, prefix="/tags", tags=["tags"])
|
|
router.include_router(users_router, prefix="/users", tags=["users"])
|
|
|
|
This `__init__.py` file imports the routers for posts, comments, tags, and users from their respective modules (`app.api.v1.routes.posts`, `app.api.v1.routes.comments`, `app.api.v1.routes.tags`, and `app.api.v1.routes.users`). It then creates an instance of `APIRouter` and includes each of the imported routers with their respective prefixes and tags.
|
|
|
|
|
|
|
|
By including all the routers in this `__init__.py` file, you can easily mount the combined router in your FastAPI application. For example, in your `main.py` file, you can import this combined router and include it like this:
|
|
|
|
from fastapi import FastAPI
|
|
from app.api.v1.routes import router as api_router
|
|
|
|
app = FastAPI()
|
|
app.include_router(api_router, prefix="/api/v1")
|
|
|
|
|
|
Note: This code assumes that you have separate modules for each of the routers (`app.api.v1.routes.posts`, `app.api.v1.routes.comments`, `app.api.v1.routes.tags`, and `app.api.v1.routes.users`) and that they have been properly defined and imported. |