Skip to content

Instantly share code, notes, and snippets.

@Vasiliy566
Created March 4, 2025 14:18
Show Gist options
  • Save Vasiliy566/3c4118b135c8bc53f178395d4123a1c2 to your computer and use it in GitHub Desktop.
Save Vasiliy566/3c4118b135c8bc53f178395d4123a1c2 to your computer and use it in GitHub Desktop.
import uvicorn
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
storage = {
1: Item(name="phone", price=9999.999),
2: Item(name="Plane", price=10000000.0)
}
@app.get("/")
def read_root():
return {"Hello": "FastAPI"}
@app.get("/items/")
def get_item(item_id: int) -> Item:
if item_id not in storage:
raise HTTPException(status_code=404, detail='Not found.')
return storage[item_id]
@app.post("/items/")
def create_item(item: Item) -> int:
item_id = max(storage.keys()) + 1 if storage else 1
storage[max(storage.keys()) + 1] = item
return item_id
uvicorn.run(app, host="0.0.0.0", port=8888)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment