/bootstrap-python-backend

A template repository for Python REST API using Flask server with Postgres database

Primary LanguagePython

Bootstrap Python Backend

A template repository for backend Python REST API server with a database

Stack: Flask Server, Gunicorn WSGI, Postgres DB, Docker, Shell

Prerequisites

Setup

Setup and activate a virtual environment (optional but recommended)

pip install -r requirements.txt

Testing

# Run unit tests with PyTest
python setup.py test

Running

Running Flask Server Locally

# Install application
python setup.py install

# Start server locally
sh scripts/start_server.sh

To 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

Running Flask Server inside Docker

# Build docker image
python setup.py build_docker

# Start server
sh scripts/run_docker.sh

For custom settings, update Dockerfile before building image

Running Flask Server with Postgres DB

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.sh

This 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

Endpoints

# Health check
curl localhost:8000/health

# Sample POST endpoint
curl -X POST http://localhost:8000/postendpoint -H 'Content-Type: application/json' -d '{"param": "value"}'

Kubernetes

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