Add router to FastAPI for improved route management #1
2 changed files with 16 additions and 11 deletions
14
src/main.py
14
src/main.py
|
@ -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
13
src/routers/cards.py
Normal 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")
|
Loading…
Reference in a new issue