API REST para gerenciamento de tarefas (To-Do List) com Flask e PostgreSQL.
- ✅ Criar tarefas
- ✅ Listar todas as tarefas
- ✅ Atualizar tarefas
- ✅ Marcar como concluída
- ✅ Deletar tarefas
- ✅ Persistência em PostgreSQL
┌─────────────────┐
│ Frontend │
│ (Browser) │
└────────┬────────┘
│
v
┌─────────────────┐
│ Nginx Ingress │
│ todo-api.*.nip.io│
└────────┬────────┘
│
v
┌─────────────────┐ ┌──────────────┐
│ Todo API │──────>│ PostgreSQL │
│ (Flask/Python) │ │ (StatefulSet)│
└─────────────────┘ └──────────────┘
todo-api/
├── app.py # Aplicação Flask
├── requirements.txt # Dependências Python
├── Dockerfile # Build da imagem
├── k8s/ # Manifestos Kubernetes
│ ├── deployment.yaml # Deployment da API
│ ├── service.yaml # Service ClusterIP
│ ├── ingress.yaml # Nginx Ingress
│ ├── postgres.yaml # PostgreSQL StatefulSet
│ └── postgres-secret.yaml # Credenciais do banco
└── .github/workflows/
└── build-and-push.yaml # CI/CD Pipeline
GET /healthGET /api/todosPOST /api/todos
Content-Type: application/json
{
"title": "Minha tarefa",
"description": "Descrição da tarefa"
}PUT /api/todos/:id
Content-Type: application/json
{
"title": "Tarefa atualizada",
"completed": true
}DELETE /api/todos/:id- Python 3.11+
- PostgreSQL 15+
# Instalar dependências
pip install -r requirements.txt
# Configurar variáveis de ambiente
export DB_HOST=localhost
export DB_PORT=5432
export DB_NAME=tododb
export DB_USER=todouser
export DB_PASSWORD=todopass123
# Executar
python app.py# Health check
curl http://localhost:5000/health
# Criar tarefa
curl -X POST http://localhost:5000/api/todos \
-H "Content-Type: application/json" \
-d '{"title": "Test", "description": "Testing API"}'
# Listar tarefas
curl http://localhost:5000/api/todosdocker build -t prhramos/todo-api:latest .docker run -p 5000:5000 \
-e DB_HOST=postgres \
-e DB_NAME=tododb \
-e DB_USER=todouser \
-e DB_PASSWORD=todopass123 \
prhramos/todo-api:latest- Cluster Kubernetes
- ArgoCD instalado
- Longhorn para storage
- Secret
dockerhub-secretconfigurado
# Aplicar manifestos
kubectl apply -f k8s/
# Verificar pods
kubectl get pods -l app=todo-api
kubectl get pods -l app=postgres
# Verificar ingress
kubectl get ingress todo-apiapiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: todo-api
namespace: argocd
spec:
project: default
source:
repoURL: https://github.com/paulorhramos/todo-api.git
targetRevision: main
path: k8s
destination:
server: https://kubernetes.default.svc
namespace: default
syncPolicy:
automated:
prune: true
selfHeal: truekubectl create secret generic postgres-secret \
--from-literal=username=todouser \
--from-literal=password=todopass123 \
-n defaultkubectl create secret docker-registry dockerhub-secret \
--docker-server=https://index.docker.io/v1/ \
--docker-username=prhramos \
--docker-password=<TOKEN> \
-n defaultkubectl logs -f -l app=todo-apikubectl logs -f -l app=postgreskubectl exec -it postgres-0 -- psql -U todouser -d tododb# Verificar se o PostgreSQL está rodando
kubectl get pods -l app=postgres
# Verificar logs do PostgreSQL
kubectl logs -l app=postgres
# Testar conexão
kubectl exec -it postgres-0 -- psql -U todouser -d tododb -c "SELECT 1"# Ver logs
kubectl logs -l app=todo-api --tail=50
# Descrever pod
kubectl describe pod -l app=todo-api- Fazer mudanças no código
- Commit e push para GitHub
- GitHub Actions builda e publica imagem
- Atualiza deployment.yaml com nova tag
- ArgoCD detecta mudança e faz sync
- Aplicação atualizada automaticamente
| Variável | Descrição | Padrão |
|---|---|---|
| DB_HOST | Host do PostgreSQL | postgres |
| DB_PORT | Porta do PostgreSQL | 5432 |
| DB_NAME | Nome do banco | tododb |
| DB_USER | Usuário do banco | todouser |
| DB_PASSWORD | Senha do banco | (secret) |
- API: http://todo-api.10.20.20.50.nip.io
- Health: http://todo-api.10.20.20.50.nip.io/health
- Todos: http://todo-api.10.20.20.50.nip.io/api/todos
- Backend: Python 3.11 + Flask
- Database: PostgreSQL 15
- Container: Docker
- Orchestration: Kubernetes
- GitOps: ArgoCD
- CI/CD: GitHub Actions
- Storage: Longhorn
- Ingress: Nginx
Repositório: https://github.com/paulorhramos/todo-api Status: 🚀 Pronto para produção