/fastapi-poetry-starter

A starter project using Python, FastAPI and Poetry

Primary LanguagePython

fastapi-poetry-starter

Description

A project starter for personal usage containing the following:

Prerequisites

1. Install Python 3 and Poetry

MacOS (using brew)

brew install python3 poetry

Ubuntu/Debian

sudo apt install python3 python3-venv pipx
pipx ensurepath
pipx install poetry

2. Create a virtual environment with all necessary dependencies

From the root of the project execute:

poetry install

3. Activate your virtual environment

From the root of the project execute:

poetry shell

Run application

Runs the FastAPI web application on port 8000 using uvicorn:

uvicorn fastapi_poetry_starter.main:app --reload

Testing

pytest

With coverage

pytest --cov=app

With coverage and HTML output

pytest --cov-report html --cov=app

Linting

ruff check fastapi_poetry_starter/* tests/*

Formatting

ruff format fastapi_poetry_starter/* tests/*

Containerisation

The following podman commands are direct replacements of the Docker CLI. You can see that their syntax is identical:

1. Build image and tag it as fastapi-poetry-starter

podman image build -t fastapi-poetry-starter .

2. Run a container of the previously tagged image (fastapi-poetry-starter)

Run our FastAPI application and map our local port 8000 to 80 on the running container:

podman container run -d --name fastapi-poetry-starter -p 8000:80 --network bridge fastapi-poetry-starter

3. Check running containers

podman ps
CONTAINER ID  IMAGE                            COMMAND               CREATED         STATUS             PORTS                 NAMES
78586e5b4683  localhost/fastapi-poetry-starter:latest  uvicorn main:app ...  13 minutes ago  Up 5 minutes ago  0.0.0.0:8000->80/tcp  nifty_roentgen

4. Hit sample endpoint

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