Skip to content

Commit e2d299b

Browse files
authored
Migrate to uv, Workflows, Bump Gunicorn (#176)
- migrate from poetry to uv - better isolated tests - workflows added - typing fixes - bump gunicorn to fix vulnerability issue
1 parent d224aad commit e2d299b

37 files changed

+2021
-438
lines changed

.github/workflows/linting.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Linting
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
lint:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v4
10+
11+
- name: Set up Python
12+
uses: actions/setup-python@v5
13+
with:
14+
python-version: 3.11
15+
16+
- name: Install uv
17+
run: pip install uv
18+
19+
- name: Create venv
20+
run: uv venv
21+
22+
- name: Install package with dev dependencies
23+
run: uv pip install -e ".[dev]"
24+
25+
- name: Run Ruff
26+
run: uv run ruff check src

.github/workflows/tests.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Tests
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
tests:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v4
11+
12+
- name: Set up Python
13+
uses: actions/setup-python@v5
14+
with:
15+
python-version: 3.11
16+
17+
- name: Install uv
18+
run: pip install uv
19+
20+
- name: Create venv
21+
run: uv venv
22+
23+
- name: Install package with dev dependencies
24+
run: uv pip install -e ".[dev]"
25+
26+
- name: Run tests
27+
run: uv run pytest
28+
env:
29+
ENVIRONMENT: local
30+
SECRET_KEY: test-secret-key-for-testing-only
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Type Checking
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
type-check:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v4
11+
12+
- name: Set up Python
13+
uses: actions/setup-python@v5
14+
with:
15+
python-version: 3.11
16+
17+
- name: Install uv
18+
run: pip install uv
19+
20+
- name: Create venv
21+
run: uv venv
22+
23+
- name: Install package with dev dependencies
24+
run: uv pip install -e ".[dev]"
25+
26+
- name: Run mypy
27+
run: uv run mypy src --config-file pyproject.toml
28+
env:
29+
ENVIRONMENT: local
30+
SECRET_KEY: test-secret-key-for-testing-only

.gitignore

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -82,34 +82,7 @@ target/
8282
profile_default/
8383
ipython_config.py
8484

85-
# pyenv
86-
# For a library or package, you might want to ignore these files since the code is
87-
# intended to run in multiple environments; otherwise, check them in:
88-
# .python-version
89-
90-
# pipenv
91-
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92-
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93-
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94-
# install all needed dependencies.
95-
#Pipfile.lock
96-
97-
# poetry
98-
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99-
# This is especially recommended for binary packages to ensure reproducibility, and is more
100-
# commonly ignored for libraries.
101-
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102-
poetry.lock
103-
104-
# pdm
105-
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106-
#pdm.lock
107-
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108-
# in version control.
109-
# https://pdm.fming.dev/#use-with-ide
11085
.pdm.toml
111-
poetry.lock
112-
src/poetry.lock
11386

11487
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
11588
__pypackages__/
@@ -154,13 +127,6 @@ dmypy.json
154127
# Cython debug symbols
155128
cython_debug/
156129

157-
# PyCharm
158-
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
159-
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
160-
# and can be added to the global gitignore or merged into this file. For a more nuclear
161-
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
162-
#.idea/
163-
164130
# Macos
165131
.DS_Store
166132

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ repos:
6464
- id: unit_test
6565
name: Unit test
6666
language: system
67-
entry: poetry run pytest
67+
entry: uv run pytest
6868
pass_filenames: false
6969
always_run: true
7070
types: [python]

CONTRIBUTING.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,27 @@ Start by forking and cloning the FastAPI-boilerplate repository:
1010

1111
1. **Fork the Repository**: Begin by forking the project repository. You can do this by visiting https://github.com/igormagalhaesr/FastAPI-boilerplate and clicking the "Fork" button.
1212
1. **Create a Feature Branch**: Once you've forked the repo, create a branch for your feature by running `git checkout -b feature/fooBar`.
13-
1. **Testing Changes**: Ensure that your changes do not break existing functionality by running tests. In the root folder, execute poetry run `python -m pytest` to run the tests.
13+
1. **Testing Changes**: Ensure that your changes do not break existing functionality by running tests. In the root folder, execute `uv run pytest` to run the tests.
1414

15-
### Using Poetry for Dependency Management
16-
FastAPI-boilerplate uses Poetry for managing dependencies. If you don't have Poetry installed, follow the instructions on the [official Poetry website](https://python-poetry.org/docs/).
15+
### Using uv for Dependency Management
16+
FastAPI-boilerplate uses uv for managing dependencies. If you don't have uv installed, follow the instructions on the [official uv website](https://docs.astral.sh/uv/).
1717

18-
Once Poetry is installed, navigate to the cloned repository and install the dependencies:
18+
Once uv is installed, navigate to the cloned repository and install the dependencies:
1919
```sh
2020
cd FastAPI-boilerplate
21-
poetry install
21+
uv sync
2222
```
2323

2424
### Activating the Virtual Environment
25-
Poetry creates a virtual environment for your project. Activate it using:
25+
uv creates a virtual environment for your project. Activate it using:
2626

2727
```sh
28-
poetry shell
28+
source .venv/bin/activate
29+
```
30+
31+
Alternatively, you can run commands directly with `uv run` without activating the environment:
32+
```sh
33+
uv run python your_script.py
2934
```
3035

3136
## Making Contributions
@@ -37,7 +42,7 @@ poetry shell
3742
### Testing with Pytest
3843
FastAPI-boilerplate uses pytest for testing. Run tests using:
3944
```sh
40-
poetry run pytest
45+
uv run pytest
4146
```
4247

4348
### Linting
@@ -59,7 +64,7 @@ Ensure your code passes linting before submitting.
5964
It helps in identifying simple issues before submission to code review. By running automated checks, pre-commit can ensure code quality and consistency.
6065

6166
1. **Install Pre-commit**:
62-
- **Installation**: Install pre-commit in your development environment. Use the command `pip install pre-commit`.
67+
- **Installation**: Install pre-commit in your development environment. Use the command `uv add --dev pre-commit` or `pip install pre-commit`.
6368
- **Setting Up Hooks**: After installing pre-commit, set up the hooks with `pre-commit install`. This command will install hooks into your .git/ directory which will automatically check your commits for issues.
6469
1. **Committing Your Changes**:
6570
After making your changes, use `git commit -am 'Add some fooBar'` to commit them. Pre-commit will run automatically on your files when you commit, ensuring that they meet the required standards.

Dockerfile

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,44 @@
1-
# --------- requirements ---------
1+
# --------- Builder Stage ---------
2+
FROM ghcr.io/astral-sh/uv:python3.11-bookworm-slim AS builder
23

3-
FROM python:3.11 as requirements-stage
4+
# Set environment variables for uv
5+
ENV UV_COMPILE_BYTECODE=1
6+
ENV UV_LINK_MODE=copy
47

5-
WORKDIR /tmp
8+
WORKDIR /app
69

7-
RUN pip install poetry poetry-plugin-export
10+
# Install dependencies first (for better layer caching)
11+
RUN --mount=type=cache,target=/root/.cache/uv \
12+
--mount=type=bind,source=uv.lock,target=uv.lock \
13+
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
14+
uv sync --locked --no-install-project
815

9-
COPY ./pyproject.toml ./poetry.lock* /tmp/
16+
# Copy the project source code
17+
COPY . /app
1018

11-
RUN poetry export -f requirements.txt --output requirements.txt --without-hashes
19+
# Install the project in non-editable mode
20+
RUN --mount=type=cache,target=/root/.cache/uv \
21+
uv sync --locked --no-editable
1222

23+
# --------- Final Stage ---------
24+
FROM python:3.11-slim-bookworm
1325

14-
# --------- final image build ---------
15-
FROM python:3.11
26+
# Create a non-root user for security
27+
RUN groupadd --gid 1000 app \
28+
&& useradd --uid 1000 --gid app --shell /bin/bash --create-home app
1629

17-
WORKDIR /code
30+
# Copy the virtual environment from the builder stage
31+
COPY --from=builder --chown=app:app /app/.venv /app/.venv
1832

19-
COPY --from=requirements-stage /tmp/requirements.txt /code/requirements.txt
33+
# Ensure the virtual environment is in the PATH
34+
ENV PATH="/app/.venv/bin:$PATH"
2035

21-
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
36+
# Switch to the non-root user
37+
USER app
2238

23-
COPY ./src/app /code/app
39+
# Set the working directory
40+
WORKDIR /code
2441

2542
# -------- replace with comment to run with gunicorn --------
2643
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
27-
# CMD ["gunicorn", "app.main:app", "-w", "4", "-k", "uvicorn.workers.UvicornWorker". "-b", "0.0.0.0:8000"]
44+
# CMD ["gunicorn", "app.main:app", "-w", "4", "-k", "uvicorn.workers.UvicornWorker", "-b", "0.0.0.0:8000"]

0 commit comments

Comments
 (0)