一个基于Flask的简易待办事项应用,专为Aicoding训练营设计,综合运用Python、Linux、Git、Docker和数据库开发等技能。
todo_app/ ├── app.py # Flask主应用 ├── requirements.txt # Python依赖 ├── Dockerfile # Docker镜像构建 ├── docker-compose.yml # 容器编排 ├── nginx.conf # Nginx配置 ├── init.sql # 数据库初始化 ├── env.example # 环境变量示例 ├── .gitignore # Git忽略文件 ├── templates/ │ └── index.html # 前端页面 ├── tests/ │ └── test_app.py # 单元测试 └── README.md # 项目文档
确保已安装以下工具:
git clone https://cnb.cool/sashavagals/todo_app.git
cd todo_app
# 复制环境变量文件
cp env.example .env
# 编辑环境变量(可选)
nano .env
# 构建并启动所有服务
docker-compose up -d
# 查看服务状态
docker-compose ps
# 查看日志
docker-compose logs -f todo-app
# 停止并删除所有服务
docker-compose down -v
# 创建虚拟环境
conda create -n todo_app python=3.10
conda activate todo_app
# 安装依赖
pip install -r requirements.txt
# 启动应用
python app.py
GET /api/todos
POST /api/todos Content-Type: application/json { "title": "待办事项标题", "description": "详细描述", "priority": "high|medium|low", "due_date": "2024-12-31T23:59:59" }
GET /api/todos/{id}
PUT /api/todos/{id} Content-Type: application/json { "title": "新标题", "completed": true }
POST /api/todos/{id}/toggle
DELETE /api/todos/{id}
CREATE TABLE todos (
id SERIAL PRIMARY KEY,
title VARCHAR(200) NOT NULL,
description TEXT,
completed BOOLEAN DEFAULT FALSE,
priority VARCHAR(20) DEFAULT 'medium',
due_date TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
# 运行单元测试
pytest tests/
# 运行测试并生成覆盖率报告
pytest --cov=app tests/
# 运行特定测试文件
pytest tests/test_app.py::test_create_todo
export DATABASE_URL=postgresql://user:password@host:port/database
export SECRET_KEY=your-secret-key
export FLASK_ENV=production
docker-compose -f docker-compose.prod.yml up -d
# 复制nginx配置
sudo cp nginx.conf /etc/nginx/sites-available/todo-app
sudo ln -s /etc/nginx/sites-available/todo-app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
通过这个项目,学员将掌握:
docker-compose down -v docker-compose up -d
docker-compose logs -f todo-app
docker-compose exec db pg_dump -U todo_user todo_db > backup.sql
Aicoding训练营 - 让编程学习更简单!