基于 FastAPI 框架开发的图书管理系统 API,实现了基本的图书 CRUD 操作,并集成了 SQLite 数据库存储和 Redis 缓存机制。
- 完整的图书 CRUD 操作
- SQLite 数据库持久化存储
- Redis 缓存支持
- 自动生成 API 文档
- 支持图书搜索功能
- Python 3.7+
- Redis
首先确保已安装 Python 3.7+ 和 Redis。
安装 Redis(如果未安装):
brew install redis启动 Redis 服务:
brew services start rediscd /Users/kaiyao/AI/fastapi-learn
python -m venv venv
source venv/bin/activatepip install -r requirements.txt生成测试数据:
python generate_data.pyuvicorn main:app --reload启动应用后,可以通过以下地址访问 API 文档:
- Swagger UI:http://127.0.0.1:8000/docs
- ReDoc:http://127.0.0.1:8000/redoc
curl http://127.0.0.1:8000/bookscurl http://127.0.0.1:8000/books/1curl "http://127.0.0.1:8000/books/search/?keyword=春"curl -X POST "http://127.0.0.1:8000/books" \
-H "Content-Type: application/json" \
-d '{"title":"测试书籍","author":"测试作者","price":59.9,"description":"测试描述"}'curl -X PUT "http://127.0.0.1:8000/books/1" \
-H "Content-Type: application/json" \
-d '{"title":"更新的书名","author":"更新的作者","price":69.9,"description":"更新的描述"}'curl -X DELETE "http://127.0.0.1:8000/books/1"fastapi-learn/
├── main.py # 主应用文件
├── database.py # 数据库配置
├── models.py # 数据库模型
├── generate_data.py # 测试数据生成脚本
└── requirements.txt # 项目依赖
- 确保 Redis 服务正在运行
- 数据库文件
books.db会自动在项目根目录创建 - 修改代码后应用会自动重新加载
- 缓存默认过期时间为60秒
停止 FastAPI 应用:按 Ctrl+C
停止 Redis 服务:
brew services stop redis退出虚拟环境:
deactivate- 安装必要的系统包:
sudo apt update
sudo apt install python3.9 python3.9-venv python3-pip redis-server nginx- 创建项目目录:
mkdir -p /var/www/fastapi-learn
cd /var/www/fastapi-learn- 克隆项目:
git clone <你的项目仓库URL> .- 创建虚拟环境:
python3.9 -m venv venv
source venv/bin/activate- 安装依赖:
pip install -r requirements.txt
pip install gunicorn- 创建系统服务配置文件:
sudo nano /etc/systemd/system/fastapi-learn.service添加以下内容:
[Unit]
Description=FastAPI Book Management System
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/fastapi-learn
Environment="PATH=/var/www/fastapi-learn/venv/bin"
ExecStart=/var/www/fastapi-learn/venv/bin/gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker -b unix:/tmp/fastapi-learn.sock
[Install]
WantedBy=multi-user.target- 配置 Nginx:
sudo nano /etc/nginx/sites-available/fastapi-learn添加以下内容:
server {
listen 80;
server_name your_domain.com; # 替换为你的域名
location / {
proxy_pass http://unix:/tmp/fastapi-learn.sock;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}- 启用网站配置:
sudo ln -s /etc/nginx/sites-available/fastapi-learn /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx- 启动 Redis:
sudo systemctl start redis
sudo systemctl enable redis- 初始化数据库:
cd /var/www/fastapi-learn
source venv/bin/activate
python generate_data.py- 启动应用服务:
sudo systemctl start fastapi-learn
sudo systemctl enable fastapi-learn- 查看应用日志:
sudo journalctl -u fastapi-learn- 重启服务:
sudo systemctl restart fastapi-learn- 查看服务状态:
sudo systemctl status fastapi-learn- 配置防火墙只开放必要端口:
sudo ufw allow 80
sudo ufw allow 443
sudo ufw allow 22
sudo ufw enable- 设置 SSL 证书(推荐使用 Let's Encrypt):
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d your_domain.com- 定期更新系统和依赖:
sudo apt update && sudo apt upgrade
pip install --upgrade -r requirements.txt- 确保服务器防火墙配置正确
- 定期备份数据库文件
- 监控服务器资源使用情况
- 配置日志轮转防止日志文件过大
- 根据实际需求调整 Gunicorn 工作进程数