Spaces:
Runtime error
Runtime error
|
Download README.md from shancode/py-server-demo: direct link, hf CLI and curl.
- Browser
- Download file 2.12 kB
-
https://huggingface.co/spaces/shancode/py-server-demo/resolve/main/README.md
- Command line
-
hf download hf://spaces/shancode/py-server-demo/README.md
-
curl -L -o README.md https://huggingface.co/spaces/shancode/py-server-demo/resolve/main/README.md
2.12 kB
| title: Comment System Demo | |
| emoji: 💬 | |
| colorFrom: blue | |
| colorTo: purple | |
| sdk: docker | |
| pinned: false | |
| license: mit | |
| # Web Server Demo | |
| 基于 FastAPI 的用户管理 API 示例项目。 | |
| ## 项目结构 | |
| ``` | |
| web-server-demo/ | |
| ├── main.py # 应用入口 | |
| ├── schemas/ | |
| │ ├── __init__.py | |
| │ └── user.py # 用户数据模型 | |
| ├── routers/ | |
| │ ├── __init__.py | |
| │ └── user.py # CRUD 路由 | |
| ├── venv/ # 虚拟环境 | |
| └── requirements.txt # 依赖清单 | |
| ``` | |
| ## 快速开始 | |
| ### 1. 创建并激活虚拟环境 | |
| ```bash | |
| python3 -m venv venv | |
| source venv/bin/activate | |
| ``` | |
| ### 2. 安装依赖 | |
| ```bash | |
| pip install fastapi uvicorn | |
| ``` | |
| ### 3. 启动服务 | |
| ```bash | |
| uvicorn main:app --reload | |
| ``` | |
| ### 4. 访问文档 | |
| - Swagger UI: http://127.0.0.1:8000/docs | |
| - ReDoc: http://127.0.0.1:8000/redoc | |
| ## API 接口 | |
| ### 用户管理 `/users` | |
| | 方法 | 路径 | 功能 | | |
| |------|------|------| | |
| | POST | `/users/` | 创建用户 | | |
| | GET | `/users/` | 获取所有用户 | | |
| | GET | `/users/{user_id}` | 获取单个用户 | | |
| | PUT | `/users/{user_id}` | 更新用户 | | |
| | DELETE | `/users/{user_id}` | 删除用户 | | |
| ## 请求示例 | |
| ### 创建用户 | |
| ```bash | |
| curl -X POST "http://127.0.0.1:8000/users/" \ | |
| -H "Content-Type: application/json" \ | |
| -d '{"username": "test", "email": "test@example.com", "age": 25}' | |
| ``` | |
| ### 获取所有用户 | |
| ```bash | |
| curl http://127.0.0.1:8000/users/ | |
| ``` | |
| ### 获取单个用户 | |
| ```bash | |
| curl http://127.0.0.1:8000/users/1 | |
| ``` | |
| ### 更新用户 | |
| ```bash | |
| curl -X PUT "http://127.0.0.1:8000/users/1" \ | |
| -H "Content-Type: application/json" \ | |
| -d '{"username": "updated_name"}' | |
| ``` | |
| ### 删除用户 | |
| ```bash | |
| curl -X DELETE http://127.0.0.1:8000/users/1 | |
| ``` | |
| ## Docker 部署 | |
| ### 本地构建运行 | |
| ```bash | |
| # 构建镜像 | |
| docker build -t web-server-demo . | |
| # 运行容器 | |
| docker run -p 7860:7860 web-server-demo | |
| ``` | |
| ### 部署到 Hugging Face Spaces | |
| 1. 创建新的 Hugging Face Space,选择 Docker 类型 | |
| 2. 将项目代码推送到 Space 仓库 | |
| 3. Hugging Face 会自动构建并部署 | |