This repo hosts FastAPI endpoints that can be deployed in a container run on Google
Cloud Run. Cloud Build is configured so that once a PR is merged into main
, a new
build process is triggered and the Cloud Run container is re-deployed.
We strongly recommend using a Python package management tool to manage the dependencies
of this project. The tool chosen for this project is rye
, a new Python packaging tool
written in Rust with high performances. Since rye
is compatible with PEP 621
pyproject.toml
, other packaging management tools such as hatch
or pdm
should also
work. It, however, won't work with poetry
.
We also use docker
to build the container image that you can test on your local
machine. If you have not already installed docker, please click
here to install it. Once you have installed
docker, make sure that docker -v
runs successfully on your command line.
First, please create a new repo using this template and use the following command to clone this repo to your local development machine:
git clone https://github.com/{YOUR_ORG_NAME}/{YOUR_REPO_NAME}.git
cd {YOUR_REPO_NAME}
By default, the name for this project in pyproject.toml
is fastapi-image
. If this is
not the name you prefer, please feel free to change it. Please also remember to change:
- the path of the project
- the paths in
pyproject.toml
- the paths in
Dockerfile
- the paths in the github workflow
- the package path in tests
If you don't already have rye
installed on your computer, please follow their
installation guide to have it installed.
With rye
, setting up a development environment is very easy. Just run this command:
rye sync
You will have a virtual environment automatically set up under your project directory with all dependencies and dev dependencies installed.
Run the following command to spin up a dev server:
rye run dev
This is equivalent to running
fastapi dev ./src/fastapi_image/main.py
Test scripts are located in the tests
directory. To run tests, use
rye run test
Note: Simply calling the pytest
cli or rye test
will result in errors because
the project is defined as "virtual" in rye
, meaning that the project itself won't be
installed as a package. We need to run python -m pytest
to add the root directory to
sys.path
to allow the correct modules to be installed.
We have also set up the commands to build the docker image and run it in a container:
rye run build
rye run container
The container will be available at http://127.0.0.1:8080/