A Python REST micro-service project template that helps developers quickly generate production grade Python REST web applications using FastApi, Pydantic, Postgres, SQLAlchemy, Pytest, Poetry, Docker Compose, and more!
This project serves as a template to facilitate the quick start of the development of new REST micro service(s) in Python 3 runtime.
Currently, following facilities and sample usages are provided off-the-shelf:
- Fastapi application and middleware setup
- Two sample micro services with following features
- FastApi applications with middleware injection including
- Database session management
- Exception handler
- Example JSONAPI schema defination using Pydantic
- Example ORM definition and DAL(i.e., Data Access Layer) implementaion
- FastApi applications with middleware injection including
- Example API endpoints:
GET
endpoint to retrieve an object from DatastorePOST
endpoint to create a new object in DatastoreGET
endpoint to retrieve an object from SQL databasePOST
endpoint to create a new object in SQL database
- Test infrastructure(
pytest
)- Mocked cloud clients
- Test clients for Fastapi apps
- Sample unit tests
- Poetry dependency management
- Dockerized environment for local development and testing
- Sample GAE application configuration files and Cloudbuild scripts
- Download this project(in .zip format so git files will be excluded) to you local workstation, unzip it and rename the project directory to your <PROJECT_NAME>
- Open a command line terminal, go to directory <PROJECT_NAME> and use it as your root working directory,
use a text editor to replace all occurrences ofpy-rest-service-template
with <PROJECT_NAME>. - Run following command to initialize a Python virtual environment and make sure it's Python>=3.7
poetry shell
source .venv/bin/activate
python -V
- Run following command to install dependencies from
poetry.lock
poetry install
- Run following command to start services inside docker container and execute unit tests
make test
- Run following command to start services inside docker container without unit tests
make run
- To speed up your development, if possible, use local (non-docker) virtual environment for you development and test, make sure all required emulators are up and running locally.
- Use
poetry add/remove/update
to manage dependencies, DO NOT manually modifypoetry.lock
. - Use
poetry add/remove some_lib --dev
to manage libraries that are not required for deployment. - Before committing your change or starting up your local docker environment, make sure
requirements.txt
is up to date by running following commandpoetry export -f requirements.txt --output requirements.txt --without-hashes
- Run
poetry export -f requirements.txt --output requirements-dev.txt --without-hashes --dev
to export all dependencies for testing