init:测试
This commit is contained in:
77
.gitignore
vendored
Normal file
77
.gitignore
vendored
Normal file
@@ -0,0 +1,77 @@
|
||||
# Python
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
*.so
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# Virtual Environment
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# IDE
|
||||
.idea/
|
||||
.vscode/
|
||||
*.swp
|
||||
*.swo
|
||||
*~
|
||||
|
||||
# OS
|
||||
.DS_Store
|
||||
.DS_Store?
|
||||
._*
|
||||
.Spotlight-V100
|
||||
.Trashes
|
||||
ehthumbs.db
|
||||
Thumbs.db
|
||||
|
||||
# uv
|
||||
.uv/
|
||||
|
||||
# Testing
|
||||
.pytest_cache/
|
||||
.coverage
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
coverage.xml
|
||||
*.cover
|
||||
.hypothesis/
|
||||
|
||||
# Docker
|
||||
.dockerignore
|
||||
docker-compose.override.yml
|
||||
|
||||
# Logs
|
||||
*.log
|
||||
|
||||
# Temporary files
|
||||
*.tmp
|
||||
*.temp
|
||||
|
||||
# Node.js (if using frontend)
|
||||
node_modules/
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
38
Dockerfile
Normal file
38
Dockerfile
Normal file
@@ -0,0 +1,38 @@
|
||||
# 使用官方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/
|
||||
|
||||
RUN python -m pip install --upgrade pip setuptools wheel --index-url https://mirrors.aliyun.com/pypi/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"]
|
||||
81
README copy.md
Normal file
81
README copy.md
Normal file
@@ -0,0 +1,81 @@
|
||||
# FastAPI 示例应用
|
||||
|
||||
这是一个使用uv管理的FastAPI示例应用。
|
||||
|
||||
## 安装和运行
|
||||
|
||||
### 使用uv(推荐)
|
||||
|
||||
1. 初始化项目:
|
||||
```bash
|
||||
uv init
|
||||
```
|
||||
|
||||
2. 安装依赖:
|
||||
```bash
|
||||
uv sync
|
||||
```
|
||||
|
||||
3. 启动开发服务器:
|
||||
```bash
|
||||
uv run uvicorn app.main:app --reload --host 0.0.0.0
|
||||
```
|
||||
|
||||
### 使用pip
|
||||
|
||||
1. 安装依赖:
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
2. 启动开发服务器:
|
||||
```bash
|
||||
uvicorn app.main:app --reload
|
||||
```
|
||||
|
||||
### 使用Docker
|
||||
|
||||
1. 构建并启动:
|
||||
```bash
|
||||
docker-compose up --build
|
||||
```
|
||||
|
||||
2. 访问应用:http://localhost:8000
|
||||
|
||||
## API文档
|
||||
|
||||
启动服务器后,访问以下地址查看API文档:
|
||||
|
||||
- Swagger UI: http://localhost:8000/docs
|
||||
- ReDoc: http://localhost:8000/redoc
|
||||
|
||||
## API端点
|
||||
|
||||
### 基础端点
|
||||
- `GET /` - 欢迎信息
|
||||
- `GET /health` - 健康检查
|
||||
|
||||
### 商品管理API
|
||||
- `GET /api/v1/items` - 获取所有商品
|
||||
- `GET /api/v1/items/{item_id}` - 获取指定商品
|
||||
- `POST /api/v1/items` - 创建新商品
|
||||
- `PUT /api/v1/items/{item_id}` - 更新商品
|
||||
- `DELETE /api/v1/items/{item_id}` - 删除商品
|
||||
|
||||
## 项目结构
|
||||
|
||||
```
|
||||
├── app/
|
||||
│ ├── __init__.py
|
||||
│ ├── main.py # FastAPI应用主文件
|
||||
│ └── routes/
|
||||
│ ├── __init__.py
|
||||
│ └── items.py # 商品管理API路由
|
||||
├── .dockerignore # Docker构建忽略文件
|
||||
├── .gitignore # Git忽略文件
|
||||
├── Dockerfile # Docker镜像配置
|
||||
├── docker-compose.yml # Docker编排配置
|
||||
├── pyproject.toml # uv项目配置
|
||||
├── requirements.txt # Python依赖
|
||||
└── README.md # 项目说明
|
||||
```
|
||||
0
app/__init__.py
Normal file
0
app/__init__.py
Normal file
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示例应用 v1.2"}
|
||||
|
||||
@app.get("/health")
|
||||
def health_check():
|
||||
return {"status": "healthy"}
|
||||
0
app/routes/__init__.py
Normal file
0
app/routes/__init__.py
Normal file
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