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
6 changes: 3 additions & 3 deletions .devcontainer/setup.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
curl -sSL https://install.python-poetry.org | python3 -
poetry install
poetry run pre-commit install
curl -LsSf https://astral.sh/uv/install.sh | sh
uv sync --frozen
uv run pre-commit install
60 changes: 24 additions & 36 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,68 +6,56 @@ jobs:
black:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.8.5
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v5
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version-file: 'pyproject.toml'
cache: 'poetry'
python-version-file: "pyproject.toml"
- name: Install dependencies
run: poetry install --no-root
run: uv sync --frozen
- name: Run black check
run: poetry run black --check --diff .
run: uv run black --check --diff .
isort:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.8.5
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v5
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version-file: 'pyproject.toml'
cache: 'poetry'
python-version-file: "pyproject.toml"
- name: Install dependencies
run: poetry install --no-root
run: uv sync --frozen
- name: Run isort check
run: poetry run isort --check-only --diff .
run: uv run isort --check-only --diff .
flake8:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.8.5
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v5
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version-file: 'pyproject.toml'
cache: 'poetry'
python-version-file: "pyproject.toml"
- name: Install dependencies
run: poetry install --no-root
run: uv sync --frozen
- name: Run flake8 check
run: poetry run flake8 --count .
run: uv run flake8 --count .
mypy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.8.5
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v5
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version-file: 'pyproject.toml'
cache: 'poetry'
python-version-file: "pyproject.toml"
- name: Install dependencies
run: poetry install --no-root
run: uv sync --frozen
- name: Run mypy check
run: poetry run mypy .
run: uv run mypy .
15 changes: 6 additions & 9 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ jobs:
pytest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.8.5
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v5
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version-file: 'pyproject.toml'
cache: 'poetry'
python-version-file: "pyproject.toml"
- name: Install dependencies
run: poetry install --no-root
run: uv sync --frozen
- name: Run pytest
run: poetry run pytest
run: uv run pytest
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,34 @@ repos:
hooks:
- id: black
name: Format Python code with Black
entry: poetry run black
entry: uv run black
language: system
types: [python]

- id: isort
name: Sort Python imports with isort
entry: poetry run isort
entry: uv run isort
language: system
types: [python]

- id: autoflake
name: autoflake
entry: poetry run autoflake
entry: uv run autoflake
language: system
types: [python]
args: [--in-place, --remove-all-unused-imports, --remove-duplicate-keys, --ignore-init-module-imports]

- id: flake8
name: Check with Flake8
entry: poetry run flake8
entry: uv run flake8
language: system
pass_filenames: false
types: [python]
args: [--count, .]

- id: mypy
name: Validate types with MyPy
entry: poetry run mypy
entry: uv run mypy
language: system
types: [python]
pass_filenames: false
Expand Down
19 changes: 9 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
FROM python:3.12 AS build

WORKDIR /tmp
RUN pip install poetry
COPY ./pyproject.toml ./poetry.lock /tmp/
RUN poetry export -f requirements.txt --output requirements.txt --without-hashes

FROM python:3.12-slim

WORKDIR /code
COPY --from=build /tmp/requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

# Install the application dependencies.
RUN --mount=from=ghcr.io/astral-sh/uv,source=/uv,target=/bin/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --frozen --no-cache

# Copy the application into the container.
COPY ./app /code/app

RUN groupadd -r nonroot && useradd -r -g nonroot nonroot
USER nonroot

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
CMD ["/code/.venv/bin/uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

[![pytest Action](https://github.com/IbraheemTuffaha/python-fastapi-template/actions/workflows/pytest.yml/badge.svg)](https://github.com/IbraheemTuffaha/python-fastapi-template/actions/workflows/pytest.yml)
[![format Action](https://github.com/IbraheemTuffaha/python-fastapi-template/actions/workflows/format.yml/badge.svg)](https://github.com/IbraheemTuffaha/python-fastapi-template/actions/workflows/format.yml)
[![Python Version](https://img.shields.io/badge/dynamic/toml?url=https%3A%2F%2Fraw.githubusercontent.com%2FIbraheemTuffaha%2Fpython-fastapi-template%2Fmain%2Fpyproject.toml&query=tool.poetry.dependencies.python&label=python&color=greenlime)](https://github.com/IbraheemTuffaha/python-fastapi-template/blob/main/pyproject.toml)
[![Python Version](https://img.shields.io/badge/dynamic/toml?url=https%3A%2F%2Fraw.githubusercontent.com%2FIbraheemTuffaha%2Fpython-fastapi-template%2Fmain%2Fpyproject.toml&query=project.requires-python&label=python&color=greenlime)](https://github.com/IbraheemTuffaha/python-fastapi-template/blob/main/pyproject.toml)
[![GitHub License](https://img.shields.io/github/license/IbraheemTuffaha/python-fastapi-template?color=greenlime)](https://github.com/IbraheemTuffaha/python-fastapi-template/blob/main/LICENSE)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=IbraheemTuffaha_python-fastapi-template&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=IbraheemTuffaha_python-fastapi-template)
</div>

# Python FastAPI Template

A template for a Python FastAPI service:
- [x] [Poetry](https://python-poetry.org/docs/basic-usage/) for dependency management
- [x] [uv](https://docs.astral.sh/uv/getting-started/) for dependency management
- [x] [FastAPI](https://fastapi.tiangolo.com/) for building the API
- [x] [Devcontainer](https://code.visualstudio.com/docs/devcontainers/tutorial) for development environment
- [x] [Docker](https://www.docker.com/) for containerization
Expand All @@ -22,24 +22,25 @@ A template for a Python FastAPI service:

## Build and run locally

Using `python3.12` install `poetry`:
Install `uv` and `python3.12`
```
pip install poetry
curl -LsSf https://astral.sh/uv/install.sh | sh
uv python install 3.12
```

Then install dependencies
```
poetry install
uv sync --frozen
```

Run locally
```
poetry run uvicorn app.main:app --port 8000 --reload
uv run uvicorn app.main:app --port 8000 --reload
```

## Run tests
```
poetry run pytest
uv run pytest
```

## Build and run using Docker
Expand Down
Loading
Loading