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
Showing only changes of commit 9e00a5f744 - Show all commits

View file

@ -1,20 +1,12 @@
from fastapi import FastAPI, HTTPException
from typing import Annotated
from fastapi import FastAPI, HTTPException
from src.entities.cards import CARDS
import src.routers.cards as cards
app = FastAPI()
app.include_router(cards.router, prefix="/cards")
@app.get("/")
async def root():
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")