Add router to FastAPI for improved route management #1

Merged
elia merged 1 commit from init-router-setup into main 2024-11-23 17:36:42 +00:00
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")