An example of a FastAPI application managed as a uv project.
Based on the multi-file example from the FastAPI documentation.
This fork of the Astral example repository adds a detailed README of
all
used commands, structured
logging with
structlog
and testing examples with
pytest
, optionally with coverage
with pytest-cov
.
- uv-fastapi-example
A project starter for personal usage containing the following:
- Python 3.13.*
- FastAPI web framework
- Structured logging using
structlog
- Dependency management using
uv
- Containerisation using a Dockerfile
- Testing with
pytest
and optionally with coverage withpytest-cov
- Linting/formatting using
ruff
.gitignore
MacOS (using brew
)
brew install python3 uv
Ubuntu/Debian
sudo apt install python3 python3-venv
curl -LsSf https://astral.sh/uv/install.sh | sh
From the root of the project execute:
uv sync
uv run fastapi dev
uv run fastapi run
uv run pytest
uv run pytest --cov=app
uv run pytest --cov-report html --cov=app
uv run ruff check app/* tests/*
uv run ruff format app/* tests/*
The following podman
commands are direct replacements of the Docker CLI. You can see that their syntax is identical:
podman image build -t uv-fastapi-example .
Run our FastAPI application and map our local port 8000
to 80
on the running container:
podman container run -d --name uv-fastapi-example -p 8000:80 --network bridge uv-fastapi-example
podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
78586e5b4683 localhost/uv-fastapi-example:latest uvicorn main:app ... 13 minutes ago Up 5 minutes ago 0.0.0.0:8000->80/tcp nifty_roentgen
Our FastAPI server now runs on port 8000
on our local machine. We can test it with:
curl -i http://localhost:8000/healthcheck
Output:
HTTP/1.1 200 OK
server: uvicorn
content-length: 0
MIT