Add router to FastAPI for improved route management

- Implemented a new router for organizing API endpoints.
- Registered the router with the main FastAPI app.
- Enhanced modularity and readability of the codebase.
This commit is contained in:
Anthony Al Lazkani 2024-11-22 00:35:58 +02:00
parent 68143f8ba0
commit 9e00a5f744
2 changed files with 16 additions and 11 deletions

View file

@ -1,20 +1,12 @@
from fastapi import FastAPI, HTTPException
from typing import Annotated from typing import Annotated
from fastapi import FastAPI, HTTPException import src.routers.cards as cards
from src.entities.cards import CARDS
app = FastAPI() app = FastAPI()
app.include_router(cards.router, prefix="/cards")
@app.get("/") @app.get("/")
async def root(): async def root():
return {"message": "Hello World!"} return {"message": "Hello World!"}
@app.get("/cards/{card}")
def get_cards(card: str):
for key in CARDS:
if key in card:
return {"cards": CARDS[card]}
raise HTTPException(status_code=404, detail="Cards with this type not found")

13
src/routers/cards.py Normal file
View file

@ -0,0 +1,13 @@
from fastapi import APIRouter, HTTPException
from src.entities.cards import CARDS
router = APIRouter()
@router.get("/{card}")
async def get_cards(card: str):
for key in CARDS:
if key == card:
return {"cards": CARDS[card]}
raise HTTPException(status_code=404, detail="Cards with this type not found")