Skip to content

Latest commit

Β 

History

History
270 lines (237 loc) Β· 11.3 KB

File metadata and controls

270 lines (237 loc) Β· 11.3 KB

Architecture Overview

System Design

Dev API Vault is built as a modular FastAPI microservice designed for scalability, maintainability, and ease of deployment.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     Client Applications                      β”‚
β”‚              (Web, Mobile, CLI, Third-party APIs)            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                         β”‚ HTTP/REST
                         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    FastAPI Application                       β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚              Middleware Layer                        β”‚   β”‚
β”‚  β”‚  β€’ CORS Middleware (Cross-Origin Resource Sharing)  β”‚   β”‚
β”‚  β”‚  β€’ Rate Limiting Middleware                         β”‚   β”‚
β”‚  β”‚  β€’ Request/Response Logging                         β”‚   β”‚
β”‚  β”‚  β€’ Security Headers (TrustedHost)                   β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                         β”‚                                    β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚              Router Layer (routers.py)              β”‚   β”‚
β”‚  β”‚  β€’ /api/v1/markdown-to-html                        β”‚   β”‚
β”‚  β”‚  β€’ /api/v1/qr-code                                 β”‚   β”‚
β”‚  β”‚  β€’ /api/v1/image-to-base64                         β”‚   β”‚
β”‚  β”‚  β€’ /api/v1/regex-tester                            β”‚   β”‚
β”‚  β”‚  β€’ /api/v1/word-count                              β”‚   β”‚
β”‚  β”‚  β€’ /api/v1/summarize                               β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                         β”‚                                    β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚         Business Logic Layer (utils.py)            β”‚   β”‚
β”‚  β”‚  β€’ Markdown Processing                             β”‚   β”‚
β”‚  β”‚  β€’ QR Code Generation                              β”‚   β”‚
β”‚  β”‚  β€’ Image Encoding                                  β”‚   β”‚
β”‚  β”‚  β€’ Regex Validation                                β”‚   β”‚
β”‚  β”‚  β€’ Web Scraping & Analysis                         β”‚   β”‚
β”‚  β”‚  β€’ Text Summarization (NLTK)                       β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                         β”‚                                    β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚         Data Validation Layer (models.py)          β”‚   β”‚
β”‚  β”‚  β€’ Pydantic Request Models                         β”‚   β”‚
β”‚  β”‚  β€’ Pydantic Response Models                        β”‚   β”‚
β”‚  β”‚  β€’ Type Hints & Validation                         β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Directory Structure

Dev_Api_Vault/
β”œβ”€β”€ app/                          # Main application package
β”‚   β”œβ”€β”€ __init__.py              # Package initialization & version
β”‚   β”œβ”€β”€ main.py                  # FastAPI app setup & lifespan
β”‚   β”œβ”€β”€ config.py                # Configuration management (settings)
β”‚   β”œβ”€β”€ models.py                # Pydantic request/response models
β”‚   β”œβ”€β”€ routers.py               # API endpoint definitions
β”‚   β”œβ”€β”€ utils.py                 # Business logic & utility functions
β”‚   β”œβ”€β”€ security.py              # Authentication & security utilities
β”‚   β”œβ”€β”€ middleware.py            # Custom middleware (rate limiting, etc.)
β”‚   └── openapi.py               # OpenAPI/Swagger customization
β”‚
β”œβ”€β”€ tests/                        # Test suite
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ conftest.py              # Pytest configuration & fixtures
β”‚   β”œβ”€β”€ test_api.py              # API endpoint tests
β”‚   └── test_edge_cases.py       # Edge case & error handling tests
β”‚
β”œβ”€β”€ assets/                       # Static assets
β”‚   └── demo/                    # Demo screenshots & documentation
β”‚
β”œβ”€β”€ static/                       # Static files (if needed)
β”‚
β”œβ”€β”€ .github/                      # GitHub configuration
β”‚   └── workflows/               # CI/CD workflows
β”‚
β”œβ”€β”€ .env.example                 # Example environment variables
β”œβ”€β”€ .gitignore                   # Git ignore patterns
β”œβ”€β”€ .gitattributes               # Git attributes (line endings)
β”œβ”€β”€ CHANGELOG.md                 # Version history
β”œβ”€β”€ CONTRIBUTING.md              # Contribution guidelines
β”œβ”€β”€ CODE_OF_CONDUCT.md           # Community code of conduct
β”œβ”€β”€ DEVELOPMENT.md               # Development setup guide
β”œβ”€β”€ LICENSE                      # MIT License
β”œβ”€β”€ README.md                    # Project documentation
β”œβ”€β”€ ARCHITECTURE.md              # This file
β”œβ”€β”€ Dockerfile                   # Docker image configuration
β”œβ”€β”€ docker-compose.yml           # Multi-container setup
β”œβ”€β”€ pyproject.toml               # Poetry project configuration
β”œβ”€β”€ requirements.txt             # Pip dependencies
└── build.sh                     # Build script for NLTK data

Data Flow

Request Processing Pipeline

1. Client Request
   ↓
2. Middleware Processing
   β€’ CORS validation
   β€’ Rate limit check
   β€’ Request logging
   ↓
3. Router Matching
   β€’ Path matching
   β€’ HTTP method validation
   ↓
4. Request Validation
   β€’ Pydantic model validation
   β€’ Type checking
   ↓
5. Business Logic Execution
   β€’ Utility function processing
   β€’ External API calls (if needed)
   ↓
6. Response Formatting
   β€’ Pydantic response model
   β€’ JSON serialization
   ↓
7. Middleware Post-Processing
   β€’ Response logging
   β€’ Security headers
   ↓
8. Client Response

Key Components

1. FastAPI Application (main.py)

  • Entry point for the application
  • Middleware configuration
  • Lifespan management (startup/shutdown)
  • OpenAPI documentation setup
  • Static file serving

2. Configuration Management (config.py)

  • Environment variable loading
  • Settings validation
  • Default values
  • Environment-specific configurations

3. Data Models (models.py)

  • Request payload schemas
  • Response payload schemas
  • Pydantic validation rules
  • Type hints for IDE support

4. API Routes (routers.py)

  • Endpoint definitions
  • HTTP method handlers
  • Request/response documentation
  • Error handling

5. Business Logic (utils.py)

  • Core utility implementations
  • External library integrations
  • Data processing functions
  • Helper methods

6. Security (security.py)

  • API key validation
  • Authentication helpers
  • Password hashing utilities
  • Token management

7. Middleware (middleware.py)

  • Rate limiting implementation
  • Request/response logging
  • Custom headers
  • Error handling middleware

8. OpenAPI Customization (openapi.py)

  • Custom API metadata
  • Tag definitions
  • Operation configurations
  • Security scheme definitions

Technology Stack

Layer Technology Purpose
Framework FastAPI Modern async web framework
Server Uvicorn ASGI application server
Validation Pydantic Data validation & serialization
Markdown markdown Markdown to HTML conversion
QR Codes qrcode + Pillow QR code generation
Web Scraping BeautifulSoup4 + requests HTML parsing & HTTP requests
Text Processing NLTK Natural language processing
Testing Pytest Unit & integration testing
Code Quality Black, isort, Flake8, mypy Linting & formatting
Containerization Docker Application containerization
CI/CD GitHub Actions Automated testing & deployment

Deployment Architecture

Local Development

Developer Machine
β”œβ”€β”€ Python venv
β”œβ”€β”€ FastAPI app (uvicorn)
β”œβ”€β”€ SQLite (optional)
└── NLTK data cache

Docker Deployment

Docker Container
β”œβ”€β”€ Python 3.9+ runtime
β”œβ”€β”€ FastAPI app (uvicorn)
β”œβ”€β”€ All dependencies
└── NLTK data (pre-downloaded)

Cloud Deployment (Render/Heroku)

Cloud Platform
β”œβ”€β”€ Container registry
β”œβ”€β”€ Managed Python runtime
β”œβ”€β”€ Auto-scaling
β”œβ”€β”€ Health checks
└── Environment variables

Security Considerations

  1. Input Validation: All inputs validated via Pydantic models
  2. Rate Limiting: Middleware prevents abuse
  3. CORS Configuration: Restricted to allowed origins
  4. Trusted Hosts: TrustedHostMiddleware prevents header injection
  5. Environment Variables: Sensitive data in .env files
  6. Error Handling: Generic error messages to prevent information leakage
  7. Logging: Comprehensive logging without sensitive data

Performance Optimization

  1. Async/Await: Non-blocking I/O operations
  2. Caching: Response caching where applicable
  3. Connection Pooling: Reused HTTP connections
  4. NLTK Data: Pre-downloaded to avoid runtime downloads
  5. Static File Serving: Efficient static file delivery

Scalability

  • Horizontal Scaling: Stateless design allows multiple instances
  • Load Balancing: Can be deployed behind a load balancer
  • Database Ready: Can integrate with PostgreSQL/MongoDB
  • Microservice Ready: Can be split into separate services

Future Enhancements

  • Database integration (PostgreSQL/MongoDB)
  • Caching layer (Redis)
  • Message queue (Celery/RabbitMQ)
  • WebSocket support for real-time operations
  • GraphQL endpoint
  • Advanced authentication (OAuth2/JWT)
  • API versioning strategy
  • Monitoring & observability (Prometheus/Grafana)

Last Updated: 2025-12-01 Version: 1.0.0