A template repository for backend Python REST API server with a database
Stack: Flask Server, Gunicorn WSGI, Postgres DB, Docker, Shell
- Python 3.6
- Docker
Setup and activate a virtual environment (optional but recommended)
pip install -r requirements.txt# Run unit tests with PyTest
python setup.py test# Install application
python setup.py install
# Start server locally
sh scripts/start_server.shTo start server locally with custom settings:
- Number of workers
- Threads per worker
- Worker timeout
- Gunicorn log level
sh scripts/start_server.sh workers=3 threads=2 timeout=30 log-level=DEBUG# Build docker image
python setup.py build_docker
# Start server
sh scripts/run_docker.shFor custom settings, update Dockerfile before building image
Runs the entire setup using docker-compose:
- Flask API server
- Postgres database
- Postgres admin
Make sure you have DATA_DIR environment variable set. This directory will be used as persistent volume mount
for Postgres database.
# Start all services (will always create new backend-api image)
sh scripts/run_all.sh
# Use existing backend-api image as per the VERSION file
sh scripts/run_all.sh false
# Stop all services
sh scripts/stop_all.shThis will start three containers on the same virtual network. Database will run on port5432, admin
console will be available on http://localhost:5433 and API server will be available on http://localhost:8000
# Health check
curl localhost:8000/health
# Sample POST endpoint
curl -X POST http://localhost:8000/postendpoint -H 'Content-Type: application/json' -d '{"param": "value"}'To run the app on a Kubernetes cluster:
- Install kubernetes-cli (
kubectl) - Build and push the docker image to a registry accessible on the cluster
- Replace the placeholders in the deployment files under kubernetes directory and run the following commands
kubectl apply -f kubernetes/deployment.yaml
kubectl apply -f kubernetes/service.yaml