Skip to content

Commit 5df9abc

Browse files
authored
Merge pull request #2 from tomasanchez/develop
v1.0.0
2 parents b262c94 + ce6d0bb commit 5df9abc

File tree

14 files changed

+894
-1382
lines changed

14 files changed

+894
-1382
lines changed

.flake8

Lines changed: 0 additions & 5 deletions
This file was deleted.

.github/workflows/build.yml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@ jobs:
2626

2727
steps:
2828
- uses: actions/checkout@v3
29-
- name: Set up Python 3.10
29+
- name: Install uv
30+
uses: astral-sh/setup-uv@v4
31+
with:
32+
enable-cache: true
33+
- name: Set up Python 3.11
3034
uses: actions/setup-python@v3
3135
with:
32-
python-version: "3.10"
36+
python-version: "3.11"
3337
- name: Install dependencies
3438
run: |
3539
python -m pip install --upgrade pip
36-
make ci-prebuild
37-
make build
40+
make dev
3841
- name: Lint
3942
run: |
4043
make lint
@@ -43,4 +46,4 @@ jobs:
4346
make cover
4447
- name: Build Image
4548
run: |
46-
docker build . --file Dockerfile --tag template:PR-${{ github.event.number }}
49+
docker build . --file Dockerfile --tag template:PR-${{ github.event.number }}

.isort.cfg

Lines changed: 0 additions & 2 deletions
This file was deleted.

.pre-commit-config.yaml

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
repos:
2-
- repo: local
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: "v5.0.0"
34
hooks:
4-
- id: pylint
5-
name: pylint
6-
entry: poetry run pylint
7-
language: system
8-
types: [ python ]
9-
- id: mypy
10-
name: mypy
11-
entry: poetry run mypy
12-
language: system
13-
types: [ python ]
14-
exclude: ^tests/
15-
- id: flake8
16-
name: flake8
17-
entry: poetry run flake8
18-
language: system
19-
types: [ python ]
20-
- id: isort
21-
name: isort
22-
entry: poetry run isort
23-
language: system
24-
types: [ python ]
25-
- id: black
26-
name: black
27-
entry: poetry run black
28-
language: system
29-
types: [ python ]
5+
- id: check-case-conflict
6+
- id: check-merge-conflict
7+
- id: check-toml
8+
- id: check-yaml
9+
- id: check-json
10+
- id: end-of-file-fixer
11+
- id: trailing-whitespace
12+
- repo: https://github.com/astral-sh/ruff-pre-commit
13+
rev: "v0.11.5"
14+
hooks:
15+
- id: ruff
16+
args: [ --exit-non-zero-on-fix ]
17+
- id: ruff-format

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.11.4

Dockerfile

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,43 @@
1-
FROM python:3.11.4-slim as development_build
1+
FROM python:3.11.4-slim AS builder
22

33
ARG APP_DIR=/app
44

55
ARG ENV
66

77
ENV ENV=${ENV} \
8-
PYTHOPNUNBUFFERED=1 \
8+
PYTHONUNBUFFERED=1 \
99
PYTHONHASHSEED=random \
1010
PYTHONFAULTHANDLER=1 \
11-
PIP_NO_CACHE_DIR=on \
12-
PIP_DEFAULT_TIMEOUT=100 \
13-
POETRY_VERSION=1.3.1 \
14-
UVICORN_PORT=8000 \
11+
UV_PYTHON=python3.11 \
12+
UV_COMPILE_BYTE=1 \
13+
UV_LINK_MODE=copy \
14+
UVICORN_PORT=8000 \
1515
UVICORN_HOST=0.0.0.0 \
1616
UVICORN_RELOAD=0
1717

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

23-
# System dependencies
24-
RUN pip install --disable-pip-version-check "poetry==$POETRY_VERSION"
21+
WORKDIR ${APP_DIR}
2522

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

30-
CMD ["poetry", "run", "python","-m", "template.main"]
26+
# Build the virtual environment from the lock file, excluding dev dependencies
27+
# This creates a self-contained .venv directory
28+
# Install dependencies
29+
RUN --mount=type=cache,target=/root/.cache/uv \
30+
--mount=type=bind,source=uv.lock,target=uv.lock \
31+
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
32+
uv sync --frozen --no-install-project --no-dev
33+
34+
35+
# Copy the project into the image
36+
COPY src ${APP_DIR}/src
37+
38+
# Sync the project
39+
RUN --mount=type=cache,target=/root/.cache/uv \
40+
uv sync --frozen --no-dev
41+
42+
# Set the command to run the application using the Python from the venv
43+
CMD ["uv" , "run", "python", "-m", "template.main"]

Makefile

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,46 @@ clean: ## Removes all build and test artifacts
1717
dist-clean: clean ## Removes all build and test artifacts and virtual environment
1818
rm -rf .venv
1919

20+
.PHONY: install
21+
install: ## Install dependencies
22+
uv sync
23+
24+
.PHONY: dev
25+
dev: ## Install dev dependencies
26+
uv sync --dev
27+
2028
.PHONY: build
21-
build: ## Creates a virtual environment and installs development dependencies
22-
poetry install
29+
build: ## Creates a virtual environment
30+
uv venv
2331

2432
.PHONY: test
2533
test: ## Executes tests cases
26-
poetry run pytest
34+
uv run pytest
2735

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

41+
.PHONY: format
42+
format: ## Formats the code using Ruff
43+
uv run ruff check ./src
44+
45+
.PHONY: pre-commit
46+
pre-commit: ## Runs pre-commit hooks on all files
47+
uv run pre-commit run --all-files
48+
3349
.PHONY: lint
34-
lint: ## Applies static analysis, checks and code formatting
35-
poetry run pre-commit run --all-files
50+
lint: ## Applies static analysis and type checks
51+
uv run ruff check ./src
3652

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

4258
.PHONY: help
4359
help: ## Show make target documentation
4460
@awk -F ':|##' '/^[^\t].+?:.*?##/ {\
4561
printf "\033[36m%-30s\033[0m %s\n", $$1, $$NF \
46-
}' $(MAKEFILE_LIST)
62+
}' $(MAKEFILE_LIST)

README.md

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ the [Cosmic Python](https://www.cosmicpython.com/) guidelines.
2323
* [Event Driven Architecture](#event-driven-architecture)
2424
* [Commands](#commands)
2525
* [Events](#events)
26-
* [Clean Architecture](#clean-architecture)
26+
* [Clean Architecture](#clean-architecture)
2727
* [Continuous Integration](#continuous-integration)
2828
* [Development Environment](#development-environment)
2929
* [Installing Poetry](#installing-poetry)
@@ -248,16 +248,16 @@ Run `make help` to see all available commands.
248248

249249
## Development Environment
250250

251-
### Installing Poetry
251+
### Installing UV
252252

253-
This package uses poetry for dependency management.
253+
This package uses `uv` for dependency management.
254254

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

257-
To install poetry, run:
257+
To install `uv`, run:
258258

259259
```bash
260-
pip install poetry
260+
pip install uv
261261
```
262262

263263
### Building the Development Environment
@@ -270,27 +270,9 @@ pip install poetry
270270
2. Install dependencies
271271

272272
```bash
273-
cd cosmic-fastapi && poetry install
273+
cd cosmic-fastapi && uv sync --dev
274274
```
275275

276-
Note that poetry doesn't activate the virtual environment for you. You have to do it manually.
277-
Or prefix subsequent the commands with
278-
279-
```bash
280-
poetry run
281-
```
282-
283-
You can view the environment that poetry uses with
284-
285-
```bash
286-
poetry env info
287-
```
288-
289-
To activate run:
290-
291-
```bash
292-
poetry shell
293-
```
294276
3. Activate pre-commit hooks (Optional)
295277

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

306288
```bash
307-
poetry run pre-commit run --all-files
289+
uv run pre-commit run --all-files
308290
```
309291

310-
The following checks are run: `black`, `flake8`, `isort`, `mypy`, `pylint`.
311-
312292
## Running Local
313293

314294
1. Run:
315295

316296
```bash
317-
poetry run python -m template.main
297+
uv run python -m template.main
318298
```
319299
2. Go to http://localhost:8000/docs to see the API documentation.
320300

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

325305
```bash
326-
poetry run pytest
306+
uv run pytest
327307
```
328308

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

337317
```bash
338-
poetry run pytest --cov src
318+
uv run pytest --cov src
339319
```
340320

341321
Or with the `make` command:
@@ -344,14 +324,6 @@ Or with the `make` command:
344324
make cover
345325
```
346326

347-
## Updating Dependencies
348-
349-
To update the dependencies run:
350-
351-
```bash
352-
poetry update
353-
```
354-
355327
## Recommended Readings
356328

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

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

374-
If you find this project useful, please consider supporting its development by sponsoring it.
346+
If you find this project useful, please consider supporting its development by sponsoring it.

docker-compose.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
services:
2+
app:
3+
# This tells Docker Compose to build the image from the Dockerfile
4+
# in the current directory.
5+
build: .
6+
# Name the container for easier identification.
7+
container_name: cosmic-fastapi-app
8+
# This maps port 8000 on your local machine to port 8000 inside the container,
9+
# allowing you to access the running application at http://localhost:8000.
10+
ports:
11+
- "8000:8000"
12+
# This mounts your local 'src' folder into the container at '/app/src'.
13+
# This is the key to live-reloading: changes you make to your code locally
14+
# will be immediately reflected inside the container.
15+
volumes:
16+
- ./src:/app/src
17+
# Override environment variables defined in the Dockerfile.
18+
# Here, we enable Uvicorn's auto-reload feature for development.
19+
environment:
20+
- ENV=development

mypy.ini

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)