Sample shopping cart CRUD service using Python3/FastAPI and Kubernetes
To get started make sure the following requirements (for development and deployment tooling) are installed on your development machine:
- GNU Make (Script Runner)
- Python 3.10 (Project Programming Language)
- Virtualenv (Python Dependency Manager)
- DBMate (Platform-agnostic Database Migrations Tool)
- Docker (Containerization)
- Kubectl (K8s deployment CLI)
- cURL (HTTP REST client)
The goal of this project is to demonstrate what managing, testing, building and deploying a CRUD Python service looks like. By building this service, I was able see how the following works:
- building an API service using fastapi
- setting up integration tests via pytest
- applying type safety (Protocol
, Generic
, TypeVar
) to Python via the typing library and mypy
- using the Result library
- setting up Postgres via Kubernetes
- setting up Database migrations via DBMate
Also, this project uses the make
command quite frequently. Similar to how gradle
is used in Java, Spring, or many other JVM-based projects, make
is used as a tool that acts as an "interface" to the project. A big reason as to why I've chosen make
as my de-facto "project interface tool" is that it acts as a simple, well documented, script runner and it's generally available on most unix-based machines. For more info on the available make
commands, check out the usage section.
To get the health endpoint, run the following command:
curl -X GET localhost:5001/health
To get all cart items, run the following command:
curl -X GET 'localhost:5001/cart?page_number=0&page_size=20'
To get a cart item by id, run the following command:
curl -X GET localhost:5001/cart/1
To add a cart item, run the following command:
curl \
-X POST \
-H "Content-Type: application/json" \
-d '{"name":"70-200mm Lens","price":240000,"manufacturer": "Canon"}' \
localhost:5001/cart/
To update a cart item, run the following command:
curl \
-X PUT \
-H "Content-Type: application/json" \
-d '{"name": "Lens Cap", "price": "888888888", "manufacturer": "Canon"}' \
localhost:5001/cart/1
To remove a cart item, run the following command:
curl -X DELETE localhost:5001/cart/1
First, make sure that the project dependencies have been installed with the following command:
make install
Next, let's make sure the test suite for the application is running as expected.
-
Check to make sure
Docker
is running. -
Since our test suite talks to a database, let's make sure that PostgreSQL is running locally via:
make start_local_db
- Finally, let's run our tests via:
make test
If the test suite is not passing, please provide an issue for the project.
To install project dependencies, run the following command:
make install
To run all tests (make sure database is running), run the following command:
make test
To make sure the database is running, run the following command:
make start_local_db
To start the app and database locally, run the following command:
make start
To debug the local database, run the following command:
make debug_local_db
To build the docker image, run the following command:
make build_image
To debug the docker image, run the following command:
make debug_image
To push the docker image, run the following command:
make push_image
To deploy the shopping-cart-service
, run the following command:
make deploy
To get the project in a clean state, run the following command:
make clean