diff --git a/README.en.md b/README.en.md new file mode 100644 index 0000000000000000000000000000000000000000..bfce7d3879e4ebc18dbf0466c574377e18cdfe36 --- /dev/null +++ b/README.en.md @@ -0,0 +1,115 @@ +# FastAPI Knowledge Base Project + +This project is a simple web application example built on the FastAPI framework, demonstrating core FastAPI functionalities, including the use of path parameters, query parameters, and request bodies. + +## Features + +- **Basic Routing**: Provides a root path `/` returning a welcome message +- **Path Parameters**: Demonstrates the use of path parameters with validation rules +- **Query Parameters**: Supports list query functionality with pagination and search +- **Request Body**: User registration endpoint supporting data validation + +## Quick Start + +### Requirements + +- Python 3.7+ +- FastAPI +- Uvicorn (ASGI server) + +### Install Dependencies + +```bash +pip install fastapi uvicorn +``` + +### Start Service + +```bash +uvicorn main:app --reload +``` + +After the service starts, visit `http://127.0.0.1:8000` + +## API Documentation + +After starting the service, you can access the interactive documentation via the following addresses: + +- Swagger UI: `http://127.0.0.1:8000/docs` +- ReDoc: `http://127.0.0.1:8000/redoc` + +## Endpoint Details + +### 1. Root Path +- **URL**: `GET /` +- **Function**: Returns welcome message + +### 2. Simple Path Parameter +- **URL**: `GET /hi` +- **Function**: Returns a simple greeting + +### 3. Basic Path Parameter +- **URL**: `GET /items/{item_id}` +- **Function**: Get item by specified ID +- **Parameter**: `item_id` (integer) + +### 4. Validated User Path Parameter +- **URL**: `GET /user/{uid}` +- **Function**: Get information for specified user +- **Parameter**: `uid` (integer, range 1-99) + +### 5. Author Path Parameter +- **URL**: `GET /author/{name}` +- **Function**: Get information for specified author +- **Parameter**: `name` (string, length 2-10) + +### 6. News Category Path Parameter +- **URL**: `GET /news_category/{cid}` +- **Function**: Get specified news category +- **Parameter**: `cid` (integer, range 2-99) + +### 7. News List Query +- **URL**: `GET /news/list` +- **Function**: Paginated retrieval of news list +- **Query Parameters**: + - `name`: News title (optional, max length 40) + - `page`: Page number (required, greater than 0) + - `page_size`: Items per page (required, greater than 0) + +### 8. User Registration +- **URL**: `POST /user/register` +- **Function**: Register new user +- **Request Body**: User information (JSON format) + +## Example Requests + +### User Registration +```bash +curl -X 'POST' \ + 'http://127.0.0.1:8000/user/register' \ + -H 'Content-Type: application/json' \ + -d '{"username": "your_username", "password": "your_password"}' +``` + +### Get User Information +```bash +curl http://127.0.0.1:8000/user/25 +``` + +### Query News List +```bash +curl "http://127.0.0.1:8000/news/list?name=科技&page=1&page_size=10" +``` + +## Project Structure + +``` +fastapi-knowledge/ +├── main.py # Main application code +├── test.txt # Test file +└── README.md # Project documentation +``` + +## License + +This project is for learning and communication purposes only. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..bc8c898d660720f223fa2a3ce4b68b8f45a624cf --- /dev/null +++ b/README.md @@ -0,0 +1,117 @@ + + +# FastAPI 知识库项目 + +本项目是一个基于 FastAPI 框架构建的简单 Web 应用示例,展示了 FastAPI 的核心功能,包括路径参数、查询参数和请求体的使用。 + +## 功能特性 + +- **基础路由**:提供根路径 `/` 返回欢迎信息 +- **路径参数**:演示带验证规则的路径参数使用 +- **查询参数**:支持分页和搜索的列表查询功能 +- **请求体**:用户注册接口,支持数据验证 + +## 快速开始 + +### 环境要求 + +- Python 3.7+ +- FastAPI +- Uvicorn(ASGI 服务器) + +### 安装依赖 + +```bash +pip install fastapi uvicorn +``` + +### 启动服务 + +```bash +uvicorn main:app --reload +``` + +服务启动后访问 `http://127.0.0.1:8000` + +## API 文档 + +启动服务后,可通过以下地址访问交互式文档: + +- Swagger UI:`http://127.0.0.1:8000/docs` +- ReDoc:`http://127.0.0.1:8000/redoc` + +## 接口说明 + +### 1. 根路径 +- **URL**:`GET /` +- **功能**:返回欢迎信息 + +### 2. 简单路径参数 +- **URL**:`GET /hi` +- **功能**:返回简单问候 + +### 3. 基础路径参数 +- **URL**:`GET /items/{item_id}` +- **功能**:获取指定 ID 的项目 +- **参数**:`item_id`(整数) + +### 4. 带验证的用户路径参数 +- **URL**:`GET /user/{uid}` +- **功能**:获取指定用户信息 +- **参数**:`uid`(整数,范围 1-99) + +### 5. 作者路径参数 +- **URL**:`GET /author/{name}` +- **功能**:获取指定作者信息 +- **参数**:`name`(字符串,长度 2-10) + +### 6. 新闻分类路径参数 +- **URL**:`GET /news_category/{cid}` +- **功能**:获取指定新闻分类 +- **参数**:`cid`(整数,范围 2-99) + +### 7. 新闻列表查询 +- **URL**:`GET /news/list` +- **功能**:分页获取新闻列表 +- **查询参数**: + - `name`:新闻名称(可选,最大长度 40) + - `page`:页码(必填,大于 0) + - `page_size`:每页数量(必填,大于 0) + +### 8. 用户注册 +- **URL**:`POST /user/register` +- **功能**:注册新用户 +- **请求体**:用户信息(JSON 格式) + +## 示例请求 + +### 用户注册 +```bash +curl -X 'POST' \ + 'http://127.0.0.1:8000/user/register' \ + -H 'Content-Type: application/json' \ + -d '{"username": "your_username", "password": "your_password"}' +``` + +### 获取用户信息 +```bash +curl http://127.0.0.1:8000/user/25 +``` + +### 查询新闻列表 +```bash +curl "http://127.0.0.1:8000/news/list?name=科技&page=1&page_size=10" +``` + +## 项目结构 + +``` +fastapi-knowledge/ +├── main.py # 主应用代码 +├── test.txt # 测试文件 +└── README.md # 项目文档 +``` + +## 许可证 + +本项目仅供学习交流使用。 \ No newline at end of file