first commit
This commit is contained in:
46
.dockerignore
Normal file
46
.dockerignore
Normal file
@@ -0,0 +1,46 @@
|
||||
# Git
|
||||
.git
|
||||
.gitignore
|
||||
|
||||
# Python
|
||||
__pycache__
|
||||
*.pyc
|
||||
*.pyo
|
||||
*.pyd
|
||||
.Python
|
||||
env
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Virtual Environment
|
||||
.venv
|
||||
venv
|
||||
ENV
|
||||
|
||||
# IDE
|
||||
.vscode
|
||||
.idea
|
||||
*.swp
|
||||
*.swo
|
||||
*~
|
||||
|
||||
# OS
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
# Docker
|
||||
Dockerfile
|
||||
docker-compose.yml
|
||||
.dockerignore
|
||||
|
||||
# Documentation
|
||||
README.md
|
||||
docs/
|
||||
|
||||
# Testing
|
||||
.pytest_cache
|
||||
.coverage
|
||||
htmlcov/
|
||||
|
||||
# uv
|
||||
.uv/
|
||||
36
Dockerfile
Normal file
36
Dockerfile
Normal file
@@ -0,0 +1,36 @@
|
||||
# 使用官方Python运行时作为基础镜像
|
||||
FROM registry.dev.maimaiag.com/library/python:3.11-slim
|
||||
|
||||
# 设置工作目录
|
||||
WORKDIR /app
|
||||
|
||||
# 设置环境变量
|
||||
ENV PYTHONDONTWRITEBYTECODE 1
|
||||
ENV PYTHONUNBUFFERED 1
|
||||
|
||||
# 配置uv使用阿里云源
|
||||
ENV UV_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/
|
||||
ENV UV_EXTRA_INDEX_URL=https://pypi.org/simple/
|
||||
|
||||
# 安装uv
|
||||
RUN pip install uv --index-url https://mirrors.aliyun.com/pypi/simple/
|
||||
|
||||
# 复制依赖文件
|
||||
COPY pyproject.toml requirements.txt ./
|
||||
|
||||
# 安装依赖
|
||||
RUN uv sync
|
||||
|
||||
# 复制应用代码
|
||||
COPY . .
|
||||
|
||||
# 创建非root用户
|
||||
RUN adduser --disabled-password --gecos '' appuser
|
||||
RUN chown -R appuser:appuser /app
|
||||
USER appuser
|
||||
|
||||
# 暴露端口
|
||||
EXPOSE 8000
|
||||
|
||||
# 启动命令
|
||||
CMD ["uv", "run", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
|
||||
0
app/__init__.py
Normal file
0
app/__init__.py
Normal file
BIN
app/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
app/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
BIN
app/__pycache__/main.cpython-313.pyc
Normal file
BIN
app/__pycache__/main.cpython-313.pyc
Normal file
Binary file not shown.
27
app/main.py
Normal file
27
app/main.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from app.routes.items import router as items_router
|
||||
|
||||
app = FastAPI(
|
||||
title="FastAPI 示例应用",
|
||||
description="一个使用uv管理的FastAPI示例应用",
|
||||
version="0.1.0"
|
||||
)
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
app.include_router(items_router, prefix="/api/v1")
|
||||
|
||||
@app.get("/")
|
||||
def read_root():
|
||||
return {"message": "欢迎使用FastAPI示例应用"}
|
||||
|
||||
@app.get("/health")
|
||||
def health_check():
|
||||
return {"status": "healthy"}
|
||||
0
app/routes/__init__.py
Normal file
0
app/routes/__init__.py
Normal file
BIN
app/routes/__pycache__/__init__.cpython-313.pyc
Normal file
BIN
app/routes/__pycache__/__init__.cpython-313.pyc
Normal file
Binary file not shown.
BIN
app/routes/__pycache__/items.cpython-313.pyc
Normal file
BIN
app/routes/__pycache__/items.cpython-313.pyc
Normal file
Binary file not shown.
61
app/routes/items.py
Normal file
61
app/routes/items.py
Normal file
@@ -0,0 +1,61 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from typing import List, Optional
|
||||
from pydantic import BaseModel
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class Item(BaseModel):
|
||||
id: int
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
price: float
|
||||
in_stock: bool = True
|
||||
|
||||
# 模拟数据
|
||||
items_db = [
|
||||
Item(id=1, name="商品1", description="这是商品1", price=99.99, in_stock=True),
|
||||
Item(id=2, name="商品2", description="这是商品2", price=199.99, in_stock=False),
|
||||
Item(id=3, name="商品3", price=299.99, in_stock=True),
|
||||
]
|
||||
|
||||
@router.get("/items", response_model=List[Item])
|
||||
def get_all_items():
|
||||
"""获取所有商品"""
|
||||
return items_db
|
||||
|
||||
@router.get("/items/{item_id}", response_model=Item)
|
||||
def get_item(item_id: int):
|
||||
"""根据ID获取商品"""
|
||||
for item in items_db:
|
||||
if item.id == item_id:
|
||||
return item
|
||||
raise HTTPException(status_code=404, detail="商品未找到")
|
||||
|
||||
@router.post("/items", response_model=Item)
|
||||
def create_item(item: Item):
|
||||
"""创建新商品"""
|
||||
# 检查ID是否已存在
|
||||
for existing_item in items_db:
|
||||
if existing_item.id == item.id:
|
||||
raise HTTPException(status_code=400, detail="商品ID已存在")
|
||||
|
||||
items_db.append(item)
|
||||
return item
|
||||
|
||||
@router.put("/items/{item_id}", response_model=Item)
|
||||
def update_item(item_id: int, item: Item):
|
||||
"""更新商品"""
|
||||
for index, existing_item in enumerate(items_db):
|
||||
if existing_item.id == item_id:
|
||||
items_db[index] = item
|
||||
return item
|
||||
raise HTTPException(status_code=404, detail="商品未找到")
|
||||
|
||||
@router.delete("/items/{item_id}")
|
||||
def delete_item(item_id: int):
|
||||
"""删除商品"""
|
||||
for index, item in enumerate(items_db):
|
||||
if item.id == item_id:
|
||||
del items_db[index]
|
||||
return {"message": "商品已删除"}
|
||||
raise HTTPException(status_code=404, detail="商品未找到")
|
||||
12
docker-compose.yml
Normal file
12
docker-compose.yml
Normal file
@@ -0,0 +1,12 @@
|
||||
version: '3.8'
|
||||
|
||||
services:
|
||||
web:
|
||||
build: .
|
||||
ports:
|
||||
- "8000:8000"
|
||||
environment:
|
||||
- ENVIRONMENT=development
|
||||
volumes:
|
||||
- ./app:/app/app
|
||||
restart: unless-stopped
|
||||
19
pyproject.toml
Normal file
19
pyproject.toml
Normal file
@@ -0,0 +1,19 @@
|
||||
[project]
|
||||
name = "fastapi-demo"
|
||||
version = "0.1.0"
|
||||
description = "一个使用uv管理的FastAPI示例应用"
|
||||
requires-python = ">=3.8"
|
||||
dependencies = [
|
||||
"fastapi>=0.104.1",
|
||||
"uvicorn[standard]>=0.24.0",
|
||||
"pydantic>=2.5.0",
|
||||
]
|
||||
|
||||
[project.scripts]
|
||||
start = "uvicorn app.main:app --reload --host 0.0.0.0 --port 8000"
|
||||
|
||||
[tool.uv]
|
||||
dev-dependencies = [
|
||||
"pytest>=7.0.0",
|
||||
"httpx>=0.25.0",
|
||||
]
|
||||
3
requirements.txt
Normal file
3
requirements.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
fastapi==0.104.1
|
||||
uvicorn[standard]==0.24.0
|
||||
pydantic==2.5.0
|
||||
Reference in New Issue
Block a user