Skip to content
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
5 changes: 0 additions & 5 deletions .flake8

This file was deleted.

13 changes: 8 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
- name: Install uv
uses: astral-sh/setup-uv@v4
with:
enable-cache: true
- name: Set up Python 3.11
uses: actions/setup-python@v3
with:
python-version: "3.10"
python-version: "3.11"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
make ci-prebuild
make build
make dev
- name: Lint
run: |
make lint
Expand All @@ -43,4 +46,4 @@ jobs:
make cover
- name: Build Image
run: |
docker build . --file Dockerfile --tag template:PR-${{ github.event.number }}
docker build . --file Dockerfile --tag template:PR-${{ github.event.number }}
2 changes: 0 additions & 2 deletions .isort.cfg

This file was deleted.

42 changes: 15 additions & 27 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
repos:
- repo: local
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: "v5.0.0"
hooks:
- id: pylint
name: pylint
entry: poetry run pylint
language: system
types: [ python ]
- id: mypy
name: mypy
entry: poetry run mypy
language: system
types: [ python ]
exclude: ^tests/
- id: flake8
name: flake8
entry: poetry run flake8
language: system
types: [ python ]
- id: isort
name: isort
entry: poetry run isort
language: system
types: [ python ]
- id: black
name: black
entry: poetry run black
language: system
types: [ python ]
- id: check-case-conflict
- id: check-merge-conflict
- id: check-toml
- id: check-yaml
- id: check-json
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.11.5"
hooks:
- id: ruff
args: [ --exit-non-zero-on-fix ]
- id: ruff-format
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.11.4
43 changes: 28 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.11.4-slim as development_build
FROM python:3.11.4-slim AS builder

ARG APP_DIR=/app

Expand All @@ -8,23 +8,36 @@ ENV ENV=${ENV} \
PYTHOPNUNBUFFERED=1 \
PYTHONHASHSEED=random \
PYTHONFAULTHANDLER=1 \
PIP_NO_CACHE_DIR=on \
PIP_DEFAULT_TIMEOUT=100 \
POETRY_VERSION=1.3.1 \
UVICORN_PORT=8000 \
UV_PYTHON=python3.11 \
UV_COMPILE_BYTE=1 \
UV_LINK_MODE=copy \
UVICORN_PORT=8000 \
UVICORN_HOST=0.0.0.0 \
UVICORN_RELOAD=0

# Deploy application
WORKDIR $APP_DIR
COPY pyproject.toml poetry.lock README.md ${APP_DIR}/
ADD src ${APP_DIR}/src
# Install uv.
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/

# System dependencies
RUN pip install --disable-pip-version-check "poetry==$POETRY_VERSION"
WORKDIR ${APP_DIR}

# Project initialization:
RUN poetry config virtualenvs.create false \
&& poetry install --only main
# Copy dependency definition files
COPY pyproject.toml uv.lock .python-version README.md ${APP_DIR}/

CMD ["poetry", "run", "python","-m", "template.main"]
# Build the virtual environment from the lock file, excluding dev dependencies
# This creates a self-contained .venv directory
# Install dependencies
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --frozen --no-install-project --no-dev


# Copy the project into the image
COPY src ${APP_DIR}/src

# Sync the project
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --frozen --no-dev

# Set the command to run the application using the Python from the venv
CMD ["uv" , "run", "python", "-m", "template.main"]
38 changes: 27 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,46 @@ clean: ## Removes all build and test artifacts
dist-clean: clean ## Removes all build and test artifacts and virtual environment
rm -rf .venv

.PHONY: install
install: ## Install dependencies
uv sync

.PHONY: dev
dev: ## Install dev dependencies
uv sync --dev

.PHONY: build
build: ## Creates a virtual environment and installs development dependencies
poetry install
build: ## Creates a virtual environment
uv venv

.PHONY: test
test: ## Executes tests cases
poetry run pytest
uv run pytest

.PHONY: cover
cover: ## Executes tests cases with coverage reports
poetry run pytest --cov . --junitxml reports/xunit.xml \
uv run pytest --cov . --cov-fail-under=100 --junitxml reports/xunit.xml \
--cov-report xml:reports/coverage.xml --cov-report term-missing

.PHONY: format
format: ## Formats the code using Ruff
uv run ruff check ./src

.PHONY: pre-commit
pre-commit: ## Runs pre-commit hooks on all files
uv run pre-commit run --all-files

.PHONY: lint
lint: ## Applies static analysis, checks and code formatting
poetry run pre-commit run --all-files
lint: ## Applies static analysis and type checks
uv run ruff check ./src

.PHONY: ci-prebuild
ci-prebuild: ## Install build tools and prepare project directory for the CI pipeline
pip install --disable-pip-version-check poetry
cat /dev/null > requirements.txt
.PHONY: fix
fix: ## Fix lint errors
uv run ruff check ./src ./tests --fix
uv run ruff format ./src ./tests

.PHONY: help
help: ## Show make target documentation
@awk -F ':|##' '/^[^\t].+?:.*?##/ {\
printf "\033[36m%-30s\033[0m %s\n", $$1, $$NF \
}' $(MAKEFILE_LIST)
}' $(MAKEFILE_LIST)
54 changes: 13 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ the [Cosmic Python](https://www.cosmicpython.com/) guidelines.
* [Event Driven Architecture](#event-driven-architecture)
* [Commands](#commands)
* [Events](#events)
* [Clean Architecture](#clean-architecture)
* [Clean Architecture](#clean-architecture)
* [Continuous Integration](#continuous-integration)
* [Development Environment](#development-environment)
* [Installing Poetry](#installing-poetry)
Expand Down Expand Up @@ -248,16 +248,16 @@ Run `make help` to see all available commands.

## Development Environment

### Installing Poetry
### Installing UV

This package uses poetry for dependency management.
This package uses `uv` for dependency management.

Install poetry in the system `site_packages`. DO NOT INSTALL IT in a virtual environment itself.
Install `uv` in the system `site_packages`. DO NOT INSTALL IT in a virtual environment itself.

To install poetry, run:
To install `uv`, run:

```bash
pip install poetry
pip install uv
```

### Building the Development Environment
Expand All @@ -270,27 +270,9 @@ pip install poetry
2. Install dependencies

```bash
cd cosmic-fastapi && poetry install
cd cosmic-fastapi && uv sync --dev
```

Note that poetry doesn't activate the virtual environment for you. You have to do it manually.
Or prefix subsequent the commands with

```bash
poetry run
```

You can view the environment that poetry uses with

```bash
poetry env info
```

To activate run:

```bash
poetry shell
```
3. Activate pre-commit hooks (Optional)

Using [pre-commit](https://pre-commit.com/) to run some checks before committing is highly recommended.
Expand All @@ -304,17 +286,15 @@ pip install poetry
To run the checks manually run:

```bash
poetry run pre-commit run --all-files
uv run pre-commit run --all-files
```

The following checks are run: `black`, `flake8`, `isort`, `mypy`, `pylint`.

## Running Local

1. Run:

```bash
poetry run python -m template.main
uv run python -m template.main
```
2. Go to http://localhost:8000/docs to see the API documentation.

Expand All @@ -323,7 +303,7 @@ pip install poetry
You can run the tests with:

```bash
poetry run pytest
uv run pytest
```

or with the `make` command:
Expand All @@ -335,7 +315,7 @@ make test
To generate a coverage report add `--cov src`.

```bash
poetry run pytest --cov src
uv run pytest --cov src
```

Or with the `make` command:
Expand All @@ -344,14 +324,6 @@ Or with the `make` command:
make cover
```

## Updating Dependencies

To update the dependencies run:

```bash
poetry update
```

## Recommended Readings

- [FastAPI official Documentation](https://fastapi.tiangolo.com/)
Expand All @@ -368,7 +340,7 @@ more details or visit https://mit-license.org/.
This project was designed and developed
by [Tomás Sánchez](https://tomsanchez.com.ar/about/) <[info@tomsanchez.com.ar](mailto:info@tomsanchez.com.ar)>.

Deeply inspired by [FastAPI-MVC](https://fastapi-mvc.netlify.app/)
Deeply inspired by [FastAPI-MVC](https://github.com/fastapi-mvc/fastapi-mvc)
following [Cosmic Python](https://www.cosmicpython.com/) guidelines for project structure.

If you find this project useful, please consider supporting its development by sponsoring it.
If you find this project useful, please consider supporting its development by sponsoring it.
20 changes: 20 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
services:
app:
# This tells Docker Compose to build the image from the Dockerfile
# in the current directory.
build: .
# Name the container for easier identification.
container_name: cosmic-fastapi-app
# This maps port 8000 on your local machine to port 8000 inside the container,
# allowing you to access the running application at http://localhost:8000.
ports:
- "8000:8000"
# This mounts your local 'src' folder into the container at '/app/src'.
# This is the key to live-reloading: changes you make to your code locally
# will be immediately reflected inside the container.
volumes:
- ./src:/app/src
# Override environment variables defined in the Dockerfile.
# Here, we enable Uvicorn's auto-reload feature for development.
environment:
- ENV=development
3 changes: 0 additions & 3 deletions mypy.ini

This file was deleted.

Loading
Loading