FastAPI Hello World Application
使用 GitHub MCP Server 创建的测试仓库
概述
这是一个简单的 FastAPI 应用程序,提供基本的 Hello World API 和 OpenAI 集成功能。
先决条件
- Python 3.7+
- pip 包管理器
- OpenAI API 密钥(可选,用于 AI 功能)
- Docker(可选,用于容器化)
快速启动
本地安装
-
克隆仓库
git clone https://github.com/xxradar/mcp-test-repo.git cd mcp-test-repo
-
设置虚拟环境(推荐)
python -m venv venv source venv/bin/activate # Linux/macOS # 或 venv\Scripts\activate # Windows
-
安装依赖
pip install -r requirements.txt
-
运行应用
uvicorn main:app --reload # 或 python main.py
应用将在 http://127.0.0.1:8000 上运行
Docker 安装
-
构建镜像
docker build -t fastapi-hello-world .
-
运行容器
docker run -p 8000:8000 fastapi-hello-world
API 端点
GET /
- 返回 Hello World 消息GET /hello/{name}
- 返回个性化问候GET /openai
- 从 GPT-4o 获取 AI 响应GET /docs
- Swagger UI 文档GET /redoc
- ReDoc 文档
OpenAI 集成
设置 OpenAI API 密钥:
# 本地运行
export OPENAI_API_KEY=your_api_key_here
# Docker 运行
docker run -p 8000:8000 -e OPENAI_API_KEY=your_api_key_here fastapi-hello-world
示例使用
使用 curl
curl http://127.0.0.1:8000/
curl http://127.0.0.1:8000/hello/John
curl "http://127.0.0.1:8000/openai?prompt=Tell%20me%20a%20joke"
使用浏览器
- 访问 http://127.0.0.1:8000/
- 访问 http://127.0.0.1:8000/hello/YourName
- 访问 http://127.0.0.1:8000/docs 查看完整 API 文档
开发
编辑 main.py
文件进行功能开发,使用 --reload
标志可以自动重新加载更改。