v.0.11.2 added beautyful soup bs4
This commit is contained in:
@@ -43,7 +43,8 @@
|
||||
// Use 'postCreateCommand' to run commands after the container is created.
|
||||
"postCreateCommand": "uv sync",
|
||||
// "postCreateCommand": "apt-get update && apt-get install -y git && pip3 install -r requirements.txt"
|
||||
"postStartCommand": "uv run app/main.py" // Add this line
|
||||
// "postStartCommand": "uv run app/main.py" // Add this line
|
||||
"postStartCommand": "uv run uvicorn app.main:app --reload --host 0.0.0.0 --port 8000"
|
||||
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
|
||||
// "remoteUser": "root"
|
||||
}
|
||||
@@ -1,75 +1,71 @@
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from pydantic import BaseModel
|
||||
from typing import List, Optional
|
||||
from typing import List
|
||||
|
||||
from app.models import Item, dummy_items
|
||||
|
||||
app = FastAPI(title="FastAPI Server", version="1.0.0")
|
||||
|
||||
# CORS middleware to allow client requests
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["http://localhost:3000", "http://client:3000"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
# app.add_middleware(
|
||||
# CORSMiddleware,
|
||||
# # allow_origins=["http://localhost:3000", "http://client:3000"],
|
||||
# allow_credentials=True,
|
||||
# allow_methods=["*"],
|
||||
# allow_headers=["*"],
|
||||
# )
|
||||
|
||||
class Item(BaseModel):
|
||||
id: int
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
price: float
|
||||
|
||||
# In-memory database
|
||||
items_db = [
|
||||
Item(id=1, name="Laptop", description="High-performance laptop", price=999.99),
|
||||
Item(id=2, name="Mouse", description="Wireless mouse", price=29.99),
|
||||
Item(id=3, name="Keyboard", description="Mechanical keyboard", price=79.99),
|
||||
]
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
return {"message": "FastAPI Server is running!"}
|
||||
|
||||
|
||||
@app.get("/items", response_model=List[Item])
|
||||
async def get_items():
|
||||
return items_db
|
||||
return dummy_items
|
||||
|
||||
|
||||
@app.get("/items/{item_id}", response_model=Item)
|
||||
async def get_item(item_id: int):
|
||||
item = next((item for item in items_db if item.id == item_id), None)
|
||||
item = next((item for item in dummy_items if item.id == item_id), None)
|
||||
if item is None:
|
||||
raise HTTPException(status_code=404, detail="Item not found")
|
||||
return item
|
||||
|
||||
|
||||
@app.post("/items", response_model=Item)
|
||||
async def create_item(item: Item):
|
||||
if any(existing_item.id == item.id for existing_item in items_db):
|
||||
if any(existing_item.id == item.id for existing_item in dummy_items):
|
||||
raise HTTPException(status_code=400, detail="Item ID already exists")
|
||||
items_db.append(item)
|
||||
dummy_items.append(item)
|
||||
return item
|
||||
|
||||
|
||||
@app.put("/items/{item_id}", response_model=Item)
|
||||
async def update_item(item_id: int, item: Item):
|
||||
if item.id != item_id:
|
||||
raise HTTPException(status_code=400, detail="Item ID mismatch")
|
||||
|
||||
for idx, existing_item in enumerate(items_db):
|
||||
|
||||
for idx, existing_item in enumerate(dummy_items):
|
||||
if existing_item.id == item_id:
|
||||
items_db[idx] = item
|
||||
dummy_items[idx] = item
|
||||
return item
|
||||
|
||||
|
||||
raise HTTPException(status_code=404, detail="Item not found")
|
||||
|
||||
|
||||
@app.delete("/items/{item_id}")
|
||||
async def delete_item(item_id: int):
|
||||
for idx, item in enumerate(items_db):
|
||||
for idx, item in enumerate(dummy_items):
|
||||
if item.id == item_id:
|
||||
deleted_item = items_db.pop(idx)
|
||||
deleted_item = dummy_items.pop(idx)
|
||||
return {"message": f"Item {deleted_item.name} deleted successfully"}
|
||||
|
||||
|
||||
raise HTTPException(status_code=404, detail="Item not found")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run(app, host="0.0.0.0", port=8000)
|
||||
|
||||
uvicorn.run(app, host="0.0.0.0", port=8000)
|
||||
|
||||
12
api-server/app/models/__init__.py
Normal file
12
api-server/app/models/__init__.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from .item import Item
|
||||
from typing import List
|
||||
|
||||
# In-memory database
|
||||
dummy_items: List[Item] = [
|
||||
Item(id=1, name="Laptop", description="High-performance laptop", price=999.99),
|
||||
Item(id=2, name="Mouse", description="Wireless mouse", price=29.99),
|
||||
Item(id=3, name="Keyboard", description="Mechanical keyboard", price=79.99),
|
||||
Item(id=4, name="Monitor", description='Samsung 32"', price=187.00),
|
||||
]
|
||||
|
||||
__all__ = ["Item"]
|
||||
9
api-server/app/models/item.py
Normal file
9
api-server/app/models/item.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class Item(BaseModel):
|
||||
id: int
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
price: float
|
||||
@@ -5,6 +5,7 @@ requires-python = ">=3.11"
|
||||
dependencies = [
|
||||
"fastapi>=0.104.0",
|
||||
"uvicorn[standard]>=0.24.0",
|
||||
"beautifulsoup4>=4.14.0",
|
||||
]
|
||||
|
||||
# [build-system]
|
||||
|
||||
Reference in New Issue
Block a user