Skip to content

Latest commit

Β 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

README.md

Production-Ready Flask Application

← Back to Python | Main README

A comprehensive, production-ready Flask application with REST API, gRPC, GraphQL, database integration, observability, and testing tools.

πŸš€ Features

  • REST API - Full-featured REST API with Swagger/OpenAPI documentation
  • gRPC Service - High-performance gRPC server for efficient communication
  • GraphQL API - Flexible GraphQL API with GraphiQL interface
  • Database - SQLAlchemy ORM with migration support
  • Observability - Prometheus metrics and health checks
  • Logging - Structured JSON logging for production
  • Security - Rate limiting, CORS, and security headers
  • CLI Tools - Console-based API testing tools
  • GUI - Web-based API testing interface
  • Docker - Containerization with Docker and Docker Compose

πŸ“‹ Prerequisites

  • Python 3.8 or higher
  • pip package manager
  • Virtual environment (recommended)
  • Docker (optional, for containerized deployment)

πŸ”§ Installation

1. Clone and Navigate

cd python/flask

2. Create Virtual Environment

python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

3. Install Dependencies

pip install -r requirements.txt

4. Setup Environment Variables

cp .env.example .env
# Edit .env file with your configuration

5. Generate gRPC Files

cd app/grpc_service
python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. service.proto
cd ../..

πŸƒ Running the Application

Development Server (REST & GraphQL)

python run.py

The server will start on http://localhost:5000

Production Server

gunicorn -w 4 -b 0.0.0.0:5000 --timeout 120 run:app

gRPC Server (separate terminal)

python -m app.grpc_service.server

The gRPC server will start on localhost:50051

Docker Deployment

# Build and run with Docker Compose
docker-compose up --build

# Run in detached mode
docker-compose up -d

πŸ“š API Documentation

REST API

GraphQL

Example Query:

{
  allUsers {
    id
    username
    email
  }
}

Example Mutation:

mutation {
  createUser(username: "john", email: "john@example.com") {
    user {
      id
      username
      email
    }
  }
}

gRPC

gRPC service running on localhost:50051 with:

  • UserService (GetUser, ListUsers, CreateUser)
  • TaskService (GetTask, ListTasks, CreateTask)

πŸ§ͺ Testing APIs

Web GUI

Visit http://localhost:5000/api-tester for an interactive API testing interface

CLI Tools

REST API Testing

# List all users
python -m app.cli.api_client rest list-users

# Create a user
python -m app.cli.api_client rest create-user

# List tasks
python -m app.cli.api_client rest list-tasks

# Create a task
python -m app.cli.api_client rest create-task

# Health check
python -m app.cli.api_client health

GraphQL Testing

# Query users via GraphQL
python -m app.cli.api_client graphql query-users

# Create user via GraphQL mutation
python -m app.cli.api_client graphql create-user-mutation

gRPC Testing

# List users via gRPC
python -m app.cli.grpc_client list-users

# Create user via gRPC
python -m app.cli.grpc_client create-user

# List tasks via gRPC
python -m app.cli.grpc_client list-tasks

πŸ“Š Monitoring & Observability

Prometheus Metrics

Access metrics at: http://localhost:5000/metrics

Health Checks

Logs

Logs are stored in the logs/ directory with structured JSON format for easy parsing.

πŸ—οΈ Project Structure

flask/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ __init__.py           # Application factory
β”‚   β”œβ”€β”€ api/                  # REST API endpoints
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ health.py
β”‚   β”‚   β”œβ”€β”€ users.py
β”‚   β”‚   └── tasks.py
β”‚   β”œβ”€β”€ models/               # Database models
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── models.py
β”‚   β”œβ”€β”€ grpc_service/         # gRPC service
β”‚   β”‚   β”œβ”€β”€ service.proto
β”‚   β”‚   └── server.py
β”‚   β”œβ”€β”€ graphql_service/      # GraphQL service
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── schema.py
β”‚   β”œβ”€β”€ cli/                  # CLI tools
β”‚   β”‚   β”œβ”€β”€ api_client.py
β”‚   β”‚   └── grpc_client.py
β”‚   └── templates/            # HTML templates
β”‚       β”œβ”€β”€ index.html
β”‚       └── api_tester.html
β”œβ”€β”€ config/
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── config.py             # Configuration management
β”œβ”€β”€ tests/                    # Test files
β”œβ”€β”€ run.py                    # Application entry point
β”œβ”€β”€ requirements.txt          # Python dependencies
β”œβ”€β”€ Dockerfile               # Docker configuration
β”œβ”€β”€ docker-compose.yml       # Docker Compose configuration
β”œβ”€β”€ .env.example             # Environment variables template
└── README.md                # This file

πŸ”’ Security Features

  • Rate Limiting: Prevents API abuse
  • CORS: Configurable cross-origin resource sharing
  • Security Headers: Talisman for security headers
  • Input Validation: Marshmallow for data validation
  • SQL Injection Prevention: SQLAlchemy ORM

Known Security Considerations

⚠️ Note: The protobuf library (version 4.25.8) has a known JSON recursion depth bypass vulnerability affecting versions <= 6.33.4. Currently, there is no patched version available.

Mitigations:

  • We use protobuf primarily for gRPC (binary protocol), not JSON parsing
  • REST and GraphQL APIs use Flask's native JSON handling
  • Rate limiting and request size limits are in place
  • See SECURITY.md for detailed security information

For the latest security status and recommendations, see the SECURITY.md file.

πŸ› οΈ Development

Code Formatting

black .

Linting

flake8 .

Running Tests

pytest
pytest --cov=app tests/

πŸ“– API Examples

REST API

Create a User:

curl -X POST http://localhost:5000/api/v1/users \
  -H "Content-Type: application/json" \
  -d '{"username": "john_doe", "email": "john@example.com"}'

Get All Users:

curl http://localhost:5000/api/v1/users

Create a Task:

curl -X POST http://localhost:5000/api/v1/tasks \
  -H "Content-Type: application/json" \
  -d '{"title": "Sample Task", "description": "This is a sample task"}'

GraphQL

Query:

curl -X POST http://localhost:5000/graphql \
  -H "Content-Type: application/json" \
  -d '{"query": "{ allUsers { id username email } }"}'

Mutation:

curl -X POST http://localhost:5000/graphql \
  -H "Content-Type: application/json" \
  -d '{"query": "mutation { createUser(username: \"jane\", email: \"jane@example.com\") { user { id username } } }"}'

🌟 Key Technologies

  • Flask - Web framework
  • Flask-RESTX - REST API with Swagger
  • SQLAlchemy - ORM
  • Alembic - Database migrations
  • gRPC - RPC framework
  • Graphene - GraphQL
  • Gunicorn - WSGI server
  • Prometheus - Metrics
  • Rich - CLI formatting
  • Click - CLI framework

πŸ“ Configuration

Edit .env file to configure:

  • Database connection
  • Server ports
  • Security settings
  • Logging level
  • CORS origins
  • Rate limits

🀝 Contributing

  1. Follow PEP 8 style guide
  2. Write tests for new features
  3. Update documentation
  4. Format code with Black
  5. Check with Flake8

πŸ“„ License

This project is part of the web-study repository.

πŸ”— Resources