Skip to content

Commit 4a665da

Browse files
Merge pull request #8 from PythonFloripa/feature/BaseFastAPI
Add initial project setup with Poetry and create test structure
2 parents f714081 + 5574e8d commit 4a665da

File tree

21 files changed

+1509
-3
lines changed

21 files changed

+1509
-3
lines changed

.env.example

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Environment variables for PyNews Server
2+
3+
# API Configuration
4+
PYTHONPATH=/server
5+
6+
# Add any additional environment variables your application might need
7+
# DATABASE_URL=postgresql://user:password@localhost:5432/pynews
8+
# DEBUG=false
9+
# LOG_LEVEL=info

.vscode/launch.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Python: Debug Tests",
6+
"type": "python",
7+
"request": "launch",
8+
"module": "pytest",
9+
"args": [
10+
"tests/",
11+
"-v",
12+
"-s"
13+
],
14+
"console": "integratedTerminal",
15+
"justMyCode": false,
16+
},
17+
{
18+
"name": "Python: FastAPI",
19+
"type": "python",
20+
"request": "launch",
21+
"module": "uvicorn",
22+
"args": [
23+
"app.main:app",
24+
"--reload",
25+
"--port",
26+
"8010"
27+
],
28+
"jinja": true,
29+
"justMyCode": false
30+
}
31+
]
32+
}

.vscode/settings.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"[python]": {
3+
"editor.formatOnSave": true,
4+
"editor.codeActionsOnSave": {
5+
"source.fixAll": "explicit",
6+
"source.organizeImports": "explicit"
7+
},
8+
"editor.defaultFormatter": "charliermarsh.ruff"
9+
}
10+
}

Dockerfile

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
FROM python:3.13.3-slim-bookworm AS python-base
2+
3+
ENV PYTHONUNBUFFERED=1 \
4+
PYTHONDONTWRITEBYTECODE=1 \
5+
PIP_DISABLE_PIP_VERSION_CHECK=on \
6+
PIP_DEFAULT_TIMEOUT=100 \
7+
POETRY_VERSION=1.8.2 \
8+
POETRY_HOME="/opt/poetry" \
9+
POETRY_VIRTUALENVS_IN_PROJECT=true \
10+
POETRY_NO_INTERACTION=1 \
11+
PYSETUP_PATH="/opt/pysetup" \
12+
VENV_PATH="/opt/pysetup/.venv" \
13+
PROJECT_PATH="/server"
14+
ENV PATH="$VENV_PATH/bin:$PATH"
15+
16+
FROM python-base AS builder-base
17+
18+
WORKDIR $PYSETUP_PATH
19+
COPY poetry.lock pyproject.toml ./
20+
21+
RUN apt-get update \
22+
&& apt-get install --no-install-recommends -y \
23+
build-essential \
24+
curl \
25+
git \
26+
libpq-dev \
27+
libseccomp2 \
28+
&& apt-get clean \
29+
&& rm -rf /var/lib/apt/lists/*
30+
31+
32+
RUN --mount=type=cache,target=/root/.cache/pip \
33+
pip install "poetry==$POETRY_VERSION";
34+
35+
RUN --mount=type=cache,target=/root/.cache/poetry \
36+
poetry install --only main --no-root --no-interaction
37+
38+
39+
FROM python-base AS production
40+
41+
WORKDIR $PROJECT_PATH
42+
COPY --from=builder-base $VENV_PATH $VENV_PATH
43+
44+
RUN adduser --disabled-password --gecos '' appuser
45+
COPY --chown=appuser:appuser app app
46+
USER appuser
47+
EXPOSE 8000
48+
49+
ENTRYPOINT ["uvicorn"]
50+
CMD ["app.main:app", "--host", "0.0.0.0", "--port", "8000", "--lifespan", "on"]
51+
52+
53+
FROM builder-base AS development
54+
55+
WORKDIR $PYSETUP_PATH
56+
57+
RUN poetry install --no-root --no-interaction
58+
59+
WORKDIR $PROJECT_PATH
60+
COPY app app
61+
COPY tests tests
62+
63+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--lifespan", "on"]

Makefile

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
.PHONY: help build up down logs test lint format clean dev prod restart health
2+
3+
# Colors for terminal output
4+
GREEN=\033[0;32m
5+
YELLOW=\033[1;33m
6+
NC=\033[0m # No Color
7+
8+
help: ## Mostra esta mensagem de ajuda
9+
@echo "$(YELLOW)PyNews Server - Comandos Disponíveis:$(NC)"
10+
@echo ""
11+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " $(GREEN)%-15s$(NC) %s\n", $$1, $$2}'
12+
13+
build: ## Constrói as imagens Docker
14+
@echo "$(YELLOW)Construindo imagens Docker...$(NC)"
15+
docker-compose build
16+
17+
up: ## Inicia os serviços
18+
@echo "$(YELLOW)Iniciando serviços...$(NC)"
19+
docker-compose up -d
20+
21+
down: ## Para os serviços
22+
@echo "$(YELLOW)Parando serviços...$(NC)"
23+
docker-compose down
24+
25+
logs: ## Mostra os logs dos serviços
26+
docker-compose logs -f pynews-api
27+
28+
test: ## Executa os testes
29+
@echo "$(YELLOW)Executando testes...$(NC)"
30+
poetry run pytest
31+
32+
test-cov: ## Executa os testes com coverage
33+
@echo "$(YELLOW)Executando testes com coverage...$(NC)"
34+
poetry run pytest --cov=app --cov-report=html
35+
36+
lint: ## Verifica o código com ruff
37+
@echo "$(YELLOW)Verificando código...$(NC)"
38+
poetry run ruff check .
39+
40+
format: ## Formata o código
41+
@echo "$(YELLOW)Formatando código...$(NC)"
42+
poetry run ruff format .
43+
44+
clean: ## Remove containers, volumes e imagens
45+
@echo "$(YELLOW)Limpando containers e volumes...$(NC)"
46+
docker-compose down -v --remove-orphans
47+
docker system prune -f
48+
49+
dev: build up ## Ambiente de desenvolvimento completo
50+
@echo "$(GREEN)Ambiente de desenvolvimento iniciado!$(NC)"
51+
@echo "API: http://localhost:8000"
52+
@echo "Docs: http://localhost:8000/docs"
53+
54+
prod: ## Inicia em modo produção
55+
@echo "$(YELLOW)Iniciando em modo produção...$(NC)"
56+
docker-compose -f docker-compose.yaml up -d
57+
58+
restart: ## Reinicia os serviços
59+
@echo "$(YELLOW)Reiniciando serviços...$(NC)"
60+
docker-compose restart
61+
62+
health: ## Verifica o health check da API
63+
@echo "$(YELLOW)Verificando saúde da API...$(NC)"
64+
curl -f http://localhost:8000/api/healthcheck || echo "API não está respondendo"
65+
66+
install: ## Instala dependências com Poetry
67+
@echo "$(YELLOW)Instalando dependências...$(NC)"
68+
poetry install
69+
70+
shell: ## Entra no shell do container
71+
docker-compose exec pynews-api bash
72+
73+
setup: install build up ## Setup completo do projeto
74+
@echo "$(GREEN)Setup completo realizado!$(NC)"
75+
@echo "$(GREEN)Acesse: http://localhost:8000/docs$(NC)"

README.md

Lines changed: 159 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,168 @@ sequenceDiagram
8282
```
8383

8484
## ⚙️ Como Rodar
85-
[TBD]
85+
86+
### 📋 Pré-requisitos
87+
- Docker e Docker Compose instalados
88+
- Git (para clonar o repositório)
89+
90+
### 🚀 Início Rápido
91+
92+
1. **Clone o repositório:**
93+
```bash
94+
git clone <repository-url>
95+
cd PyNewsServer
96+
```
97+
98+
2. **Configure as variáveis de ambiente (opcional):**
99+
```bash
100+
cp .env.example .env
101+
# Edite o arquivo .env conforme necessário
102+
```
103+
104+
3. **Inicie o serviço:**
105+
```bash
106+
docker-compose up -d
107+
```
108+
109+
4. **Acesse a aplicação:**
110+
- API: http://localhost:8000
111+
- Documentação Swagger: http://localhost:8000/docs
112+
- Health Check: http://localhost:8000/api/healthcheck
113+
86114
## 🧩 Configuração Inicial
87115

88-
### ▶️ Guia de Execução Dev
116+
### ▶️ Guia de Execução para Desenvolvimento
117+
118+
#### Usando Docker (Recomendado)
119+
```bash
120+
# Construir e iniciar em modo desenvolvimento
121+
docker-compose up --build
122+
123+
# Ver logs em tempo real
124+
docker-compose logs -f pynews-api
125+
126+
# Parar o serviço
127+
docker-compose down
128+
```
129+
130+
#### Usando Poetry (Local)
131+
```bash
132+
# Instalar dependências
133+
poetry install
134+
135+
# Ativar ambiente virtual
136+
poetry shell
137+
138+
# Rodar a aplicação
139+
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
140+
141+
# Rodar testes
142+
poetry run pytest
143+
144+
# Linting
145+
poetry run ruff check .
146+
poetry run ruff format .
147+
```
148+
149+
### ▶️ Guia de Execução para Produção
150+
151+
```bash
152+
# Construir imagem para produção
153+
docker-compose build --target production
154+
155+
# Iniciar em modo produção
156+
docker-compose up -d
157+
158+
# Verificar status dos containers
159+
docker-compose ps
160+
161+
# Ver logs
162+
docker-compose logs pynews-api
163+
164+
# Atualizar aplicação
165+
docker-compose pull
166+
docker-compose up -d --force-recreate
167+
```
168+
169+
### 🔧 Comandos Úteis
89170

90-
### ▶️ Guia de Execução Prod
171+
#### Usando Makefile (Recomendado)
172+
```bash
173+
# Ver todos os comandos disponíveis
174+
make help
175+
176+
# Setup completo do projeto
177+
make setup
178+
179+
# Ambiente de desenvolvimento
180+
make dev
181+
182+
# Construir e iniciar
183+
make build
184+
make up
185+
186+
# Ver logs
187+
make logs
188+
189+
# Executar testes
190+
make test
191+
make test-cov
192+
193+
# Linting e formatação
194+
make lint
195+
make format
196+
197+
# Verificar saúde da API
198+
make health
199+
200+
# Parar serviços
201+
make down
202+
203+
# Limpeza completa
204+
make clean
205+
```
206+
207+
#### Comandos Docker Diretos
208+
```bash
209+
# Entrar no container
210+
docker-compose exec pynews-api bash
211+
212+
# Reiniciar apenas o serviço da API
213+
docker-compose restart pynews-api
214+
215+
# Verificar health check
216+
curl http://localhost:8000/api/healthcheck
217+
218+
# Parar e remover todos os containers e volumes
219+
docker-compose down -v
220+
```
221+
222+
### 🛠️ Desenvolvimento
223+
224+
#### Estrutura de Testes
225+
```bash
226+
# Rodar todos os testes
227+
poetry run pytest
228+
229+
# Rodar testes com coverage
230+
poetry run pytest --cov=app
231+
232+
# Rodar testes específicos
233+
poetry run pytest tests/test_auth.py
234+
```
235+
236+
#### Linting e Formatação
237+
```bash
238+
# Verificar código
239+
poetry run ruff check .
240+
241+
# Formatar código
242+
poetry run ruff format .
243+
244+
# Fix automático de problemas
245+
poetry run ruff check . --fix
246+
```
91247

92248

93249
## referencias

0 commit comments

Comments
 (0)