init
This commit is contained in:
commit
3a168404ca
17
Dockerfile
Normal file
17
Dockerfile
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# Step 1: Use an official Python runtime as the base image
|
||||||
|
FROM python:3.9-slim
|
||||||
|
|
||||||
|
# Step 2: Set the working directory inside the container
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Step 3: Copy the current directory contents into the container
|
||||||
|
COPY . /app
|
||||||
|
|
||||||
|
# Step 4: Install the dependencies
|
||||||
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
# Step 5: Expose port 8000 to communicate with the outside world
|
||||||
|
EXPOSE 8000
|
||||||
|
|
||||||
|
# Step 6: Define the command to run the FastAPI app
|
||||||
|
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
||||||
32
app.py
Normal file
32
app.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
from fastapi import FastAPI
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
@app.get("/")
|
||||||
|
async def home():
|
||||||
|
return {"data": "Hello World"}
|
||||||
|
|
||||||
|
@app.get("/items/")
|
||||||
|
def get_items():
|
||||||
|
return {"items": ["apple", "banana", "cherry"]}
|
||||||
|
|
||||||
|
|
||||||
|
class Item(BaseModel):
|
||||||
|
name: str
|
||||||
|
price: float
|
||||||
|
|
||||||
|
@app.post("/items/")
|
||||||
|
def create_item(item: Item):
|
||||||
|
return {"message": "Item created successfully", "item": item}
|
||||||
|
|
||||||
|
|
||||||
|
@app.put("/items/{item_id}")
|
||||||
|
def update_item(item_id: int, item: Item):
|
||||||
|
return {"item_id": item_id, "updated_item": item}
|
||||||
|
|
||||||
|
|
||||||
|
@app.delete("/items/{item_id}")
|
||||||
|
def delete_item(item_id: int):
|
||||||
|
return {"message": f"Item with ID {item_id} deleted successfully"}
|
||||||
|
|
||||||
21
app_2.py
Normal file
21
app_2.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
from pydantic import BaseModel
|
||||||
|
from fastapi import FastAPI
|
||||||
|
|
||||||
|
fastapi = FastAPI()
|
||||||
|
|
||||||
|
class Item(BaseModel):
|
||||||
|
name: str
|
||||||
|
price: float
|
||||||
|
|
||||||
|
@fastapi.get("/{name}")
|
||||||
|
async def get_name(name: str):
|
||||||
|
return { "name": name }
|
||||||
|
|
||||||
|
@fastapi.get("/")
|
||||||
|
async def get_data(name: str, age: int = 0, height: int = 10):
|
||||||
|
return { "name": name, "age": age, "height": height }
|
||||||
|
|
||||||
|
|
||||||
|
@fastapi.post("/items")
|
||||||
|
async def create_item (item: Item):
|
||||||
|
return {"item": item}
|
||||||
7
main.py
Normal file
7
main.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
from fastapi import FastAPI
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
@app.get("/")
|
||||||
|
def read_root():
|
||||||
|
return {"message": "Hello, Docker!"}
|
||||||
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
fastapi
|
||||||
|
uvicorn
|
||||||
Loading…
x
Reference in New Issue
Block a user