Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ci and development scripts #1

Merged
merged 4 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
POSTGRES_DEALECT_DRIVER=postgresql

POSTGRES_USER=admin
POSTGRES_PASSWORD=admin
POSTGRES_HOST=localhost
POSTGRES_DB=desbordante
POSTGRES_PORT=5432

RABBITMQ_DEFAULT_USER=guest
RABBITMQ_DEFAULT_PASSWORD=guest
RABBITMQ_PORT=5672
26 changes: 26 additions & 0 deletions .github/workflows/run-linter-and-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Basics checks for code quality (linters and tests)
on:
pull_request:
branches:
- main
push:
jobs:
run-linters-and-tests:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up python
uses: actions/setup-python@v4
with:
python-version: '3.11'

- name: Install deps
run: python3 -m pip install poetry && make init

- name: Run all linters and formatters
run: make lint

- name: Run all tests and count coverage
run: make test
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ default_language_version:

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.291
rev: v0.1.2
hooks:
- id: ruff
args: [ --fix, --exit-non-zero-on-fix ]
- id: ruff-format

- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.9.1
Expand Down
52 changes: 52 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
.PHONY: env install-deps compose-up run-worker run-app init run all lint test

## Create .env file from .env.example
env:
@cp .env.example .env
@echo >> .env
@echo "SECRET_KEY=$$(openssl rand -hex 32)" >> .env

## Install dependencies
install-deps:
poetry install
poetry run pre-commit install

## Up development-only docker containers
compose-up:
(trap 'docker compose -f dev-docker-compose.yaml down' INT; \
docker compose -f dev-docker-compose.yaml up --build --force-recreate --remove-orphans)

## Run celery worker in watch mode
run-worker:
. .venv/bin/activate && watchmedo auto-restart --directory=./ --pattern='*.py' --recursive -- celery -A app.worker worker --loglevel=info --concurrency=1

## Run application server in watch mode
run-app:
poetry run uvicorn --port 8000 app.main:app --reload

## Initiate repository
init:
make env install-deps

## Run full application in watch mode
run:
make compose-up run-worker run-app

## Run make init run
all:
make init run

## Run all formatters and linters in project
lint:
poetry run ruff ./tests/*.py ./app/*.py
poetry run ruff format --check ./tests/*.py ./app/*.py
poetry run black --check ./tests/*.py ./app/*.py
poetry run mypy --ignore-missing-imports ./app/*.py
## Run all tests in project
test:
poetry run pytest --verbosity=2 --showlocals -log-level=DEBUG --cov=app --cov-report term

.DEFAULT_GOAL := help
# See <https://gist.github.com/klmr/575726c7e05d8780505a> for explanation.
help:
@echo "$$(tput setaf 2)Available rules:$$(tput sgr0)";sed -ne"/^## /{h;s/.*//;:d" -e"H;n;s/^## /---/;td" -e"s/:.*//;G;s/\\n## /===/;s/\\n//g;p;}" ${MAKEFILE_LIST}|awk -F === -v n=$$(tput cols) -v i=4 -v a="$$(tput setaf 6)" -v z="$$(tput sgr0)" '{printf"- %s%s%s\n",a,$$1,z;m=split($$2,w,"---");l=n-i;for(j=1;j<=m;j++){l-=length(w[j])+1;if(l<= 0){l=n-i-length(w[j])-1;}printf"%*s%s\n",-i," ",w[j];}}'
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@

1. Required `python3.11` or newer
2. Install [poetry](https://python-poetry.org/) — dependency management tool
3. Install dependencies: `poetry install`
4. (!) Install pre-commit hooks: `poetry run pre-commit install`
5. Run server application: `poetry run uvicorn --port 8000 app.main:app`
3. Install dependencies: `make init`

## Local development

Execute `make` to see all available rules with documentation

1. Activate virtual environment: `source .venv/bin/activate`
2. Don't forget to change values in .env
3. Run **development-only** containers, worker and app: `make run`

## Docs

Expand Down
Empty file added app/__init__.py
Empty file.
11 changes: 10 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
from typing import Literal

from fastapi import FastAPI
from pydantic import UUID4

from app.tasks import add_one

app = FastAPI()


@app.get("/ping")
def ping() -> Literal["Pong!"]:
async def ping() -> Literal["Pong!"]:
return "Pong!"


@app.get("/run")
async def run() -> UUID4:
result = add_one.delay(1)
return UUID4(result.task_id)
12 changes: 12 additions & 0 deletions app/tasks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import ctypes
import time


from app.worker import taskq


@taskq.task(bind=True)
def add_one(self, x):
time.sleep(1)
ctypes.string_at(0)
return x + 1, self.request.id
7 changes: 7 additions & 0 deletions app/worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from celery import Celery

taskq = Celery(
__name__,
broker="amqp://guest:guest@localhost:5672",
include=["app.tasks"],
)
19 changes: 19 additions & 0 deletions dev-docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Development only
version: '3.8'

services:
postgres:
container_name: desbordante-postgres
image: postgres:16.0-alpine
env_file:
- .env
ports:
- '${POSTGRES_PORT}:5432'

rabbitmq:
container_name: desbordante-rabbitmq
image: rabbitmq:3.12-management-alpine
env_file:
- .env
ports:
- "${RABBITMQ_PORT}:5672"
18 changes: 11 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ readme = "README.md"
[tool.poetry.dependencies]
python = "^3.11"
fastapi = { extras = ["all"], version = "^0.103.2" }
sqlalchemy = "^2.0.22"
alembic = "^1.12.1"
celery = "^5.3.4"
sqlalchemy = "^2.0.21"
alembic = "^1.12.0"
python-dotenv = "^1.0.0"


[tool.poetry.group.dev.dependencies]
pytest = "^7.4.2"
ruff = "^0.0.291"
mypy = "^1.5.1"
black = "^23.9.1"
pre-commit = "^3.4.0"
pytest = "^7.4.3"
ruff = "^0.1.2"
mypy = "^1.6.1"
black = "^23.10.1"
pre-commit = "^3.5.0"
celery-types = "^0.20.0"
watchdog = "^3.0.0"
pytest-cov = "^4.1.0"

[build-system]
requires = ["poetry-core"]
Expand Down
Empty file added tests/__init__.py
Empty file.
2 changes: 2 additions & 0 deletions tests/test_dummy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def test_true_is_true():
assert True is True