The repository is a project template for REST microservice built using FastAPI (Python). The latest version supports Python 3.8 and above.
Required
Optional
List of features that comes with default template
-  Use FastAPI as base framework to build the 
RESTAPI. - Use Poetry as a tool for dependency management and packaging in Python.
 -  Predefined 
project scaffoldinglike files and directories, event handlings, routers, middlewares etc. -  Comes with 
default configurationsfor hostname, port, environment etc. Each of these configuration can becustomizeas per microservice needs. -  Predefined common 
loggerfor application logging. -  Preconfigured special routes 
/infoand/health. -  Use 
DockerandKubernetes aka K8sto make it easy to run the app on container and shift it. -  Predefined 
GitHub Actionsfor workflows forPyLintandCodeQL. 
Feel free to modify the layout of the repo as much as you want but the given structure is as follows:
app/
├── __init__.py
├── main.py
├── api.py
├── metadata.py
├── configs/
│   └── development.py
│   └── production.py
│   └── stage.py
├── core/
│   └── common_handlers.py
├── endpoints/
│   └── health.py
│   └── info.py
│   └── router.py
│   └── users.py
├── middlewares/
│   └── validation.py
└── models/
    └── users.py
├── test_main.py
- 
__init__.pydefines and initializes the app configuration. - 
main.pydefines the FastAPI application, adds middleware, includes routers, and creates the Mangum handler. 
export <Name>=<Value>
For example:
export INSTANCE_ENVIRONMENT="DEVELOPMENT"= sign.
| Name | Purpose | Possible Values | 
|---|---|---|
| INSTANCE_ENVIRONMENT | Help to identify system instance type on which the app service is running | DEVELOPMENT, STAGE and PRODUCTION | 
export INSTANCE_ENVIRONMENT="DEVELOPMENT"python3 -m venv .venv
source .venv/bin/activate
pip3 install -r requirements.txt
pip3 install -r requirements-dev.txtRun the service
uvicorn "app.main:app" --host="0.0.0.0" --port=3000 --reloaddocker build -f Dockerfile.dev -t hegdeashwin3/template-py-rest-microservice .
docker run -d -p 3000:3000 hegdeashwin3/template-py-rest-microservicedocker tag pyrest hegdeashwin3/template-py-rest-microservice:1.1.5
docker push hegdeashwin3/template-py-rest-microservice:1.1.5
Run the tests
pytestRun the API Docs
http://localhost:<PORT>/api/v1/info
Run the Swagger API Docs
http://localhost:<PORT>/api/v1/docsRun the lint
Run pylint before committing the changes and ensure code quality at least 9.30/10
pylint --rcfile .pylintrc $(git ls-files '*.py')Run the formatter
Run black & isort before committing the changes
black app
isort **/*.pySetup Minikube
brew install minikubeStart Minikube & Verify status
minikube start
minikube statusCreate K8s deployment
kubectl apply -f k8s-pyrest-deployment.yaml
kubectl get deployments
kubectl get podsCreate K8s service
kubectl apply -f k8s-pyrest-service.yaml
kubectl get servicesDo port forwarding
kubectl port-forward service/pyrest-service 5000:3050Run the API on browser
http://localhost:5000/api/v1/infoUseful Minikube commands
minikube logs
minikube ip
minikube dashboardexport INSTANCE_ENVIRONMENT="STAGE"poetry install --no-devRun the service
uvicorn "app.main:app" --host="<STAGE_HOST_IP>" --port=<STAGE_PORT> --workers 2Run the Swagger API Docs
http://<STAGE_BASE_URL>:<STAGE_PORT>/api/v1/docssexport INSTANCE_ENVIRONMENT="PRODUCTION"poetry install --no-devRun the service
gunicorn app.main:app --workers <NO_OF_WORKERS> --worker-class uvicorn.workers.UvicornWorker --bind <PRODUCTION_HOST_IP>:<PRODUCTION_PORT>
E.g. gunicorn app.main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 127.0.0.1:3000Run the Swagger API Docs
http://<PRODUCTION_BASE_URL>:<PRODUCTION_PORT>/api/v1/docs