Skip to content

Latest commit

Β 

History

History
308 lines (236 loc) Β· 10.1 KB

File metadata and controls

308 lines (236 loc) Β· 10.1 KB

🎨 System Architecture

This document provides an overview of the audiobook automation system architecture, designed for developers who want to understand how the system works internally.

πŸ—οΈ High-Level Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Web Browser   │◄──►│   FastAPI Web   │◄──►│   SQLite DB     β”‚
β”‚   (Frontend)    β”‚    β”‚   Application   β”‚    β”‚   (Storage)     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                β”‚
                                β–Ό
                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                       β”‚  Notification   β”‚
                       β”‚    Services     β”‚
                       β”‚ (Discord, etc.) β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“ Project Structure

audiobook_dev/
β”œβ”€β”€ docs/                    # Documentation (this folder)
β”‚   β”œβ”€β”€ user-guide/         # User documentation
β”‚   β”œβ”€β”€ development/        # Developer documentation
β”‚   └── api/                # API documentation
β”œβ”€β”€ src/                    # Source code
β”‚   β”œβ”€β”€ main.py            # Application entry point
β”‚   β”œβ”€β”€ webui.py           # FastAPI web interface
β”‚   β”œβ”€β”€ db.py              # Database operations
β”‚   β”œβ”€β”€ config.py          # Configuration management
β”‚   β”œβ”€β”€ utils.py           # Utility functions
β”‚   β”œβ”€β”€ metadata.py        # Audiobook metadata handling
β”‚   β”œβ”€β”€ qbittorrent.py     # qBittorrent integration
β”‚   β”œβ”€β”€ token_gen.py       # Token generation/validation
β”‚   β”œβ”€β”€ html.py            # HTML template utilities
β”‚   └── notify/            # Notification modules
β”‚       β”œβ”€β”€ discord.py     # Discord notifications
β”‚       β”œβ”€β”€ gotify.py      # Gotify notifications
β”‚       β”œβ”€β”€ ntfy.py        # Ntfy notifications
β”‚       └── pushover.py    # Pushover notifications
β”œβ”€β”€ templates/             # Jinja2 HTML templates
β”‚   β”œβ”€β”€ base.html         # Base template
β”‚   β”œβ”€β”€ index.html        # Home page
β”‚   β”œβ”€β”€ approval.html     # Approval page
β”‚   β”œβ”€β”€ rejection.html    # Rejection page
β”‚   β”œβ”€β”€ success.html      # Success page
β”‚   β”œβ”€β”€ failure.html      # Failure page
β”‚   β”œβ”€β”€ token_expired.html # Token expired page
β”‚   └── 401_page.html     # Unauthorized page
β”œβ”€β”€ static/               # Static web assets
β”‚   β”œβ”€β”€ css/
β”‚   β”‚   └── style.css     # Main stylesheet
β”‚   └── js/
β”‚       └── app.js        # Main JavaScript
β”œβ”€β”€ tests/                # Test suite
β”œβ”€β”€ config/               # Configuration files
β”‚   └── config.yaml       # Main configuration
β”œβ”€β”€ logs/                 # Application logs
└── db.sqlite            # SQLite database

πŸ”§ Core Components

1. Web Application (webui.py)

FastAPI-based web server that handles:

  • HTTP Routes - All web endpoints and API routes
  • Template Rendering - Jinja2 template processing
  • Request Handling - Form processing and validation
  • Authentication - Token-based security
  • Static File Serving - CSS, JS, and asset delivery

Key endpoints:

  • GET / - Home page
  • POST /audiobook-requests - Submit new requests
  • GET /approve/{token} - Approval endpoint
  • GET /reject/{token} - Rejection endpoint
  • GET /requests - Browse requests

2. Database Layer (db.py)

SQLite-based data persistence with:

  • Request Storage - Audiobook request data
  • Token Management - Approval/rejection tokens
  • Audit Logging - Request history and changes
  • Schema Management - Database initialization and migrations

Database schema:

CREATE TABLE requests (
    id INTEGER PRIMARY KEY,
    title TEXT NOT NULL,
    author TEXT,
    status TEXT DEFAULT 'pending',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE tokens (
    id INTEGER PRIMARY KEY,
    token TEXT UNIQUE NOT NULL,
    request_id INTEGER,
    action TEXT NOT NULL,
    expires_at TIMESTAMP NOT NULL,
    used_at TIMESTAMP,
    FOREIGN KEY (request_id) REFERENCES requests (id)
);

3. Configuration System (config.py)

YAML-based configuration handling:

  • Environment-aware - Development vs production settings
  • Validation - Configuration validation and defaults
  • Hot-reloading - Dynamic configuration updates
  • Security - Sensitive data handling

4. Notification System (notify/)

Multi-platform notification delivery supporting:

  • Discord - Webhook-based Discord notifications
  • Gotify - Self-hosted push notifications
  • Ntfy - Simple push notification service
  • Pushover - Mobile push notifications

Each notification module implements a common interface:

class NotificationService:
    def send_approval_request(self, request_data: dict) -> bool
    def send_approval_notification(self, request_data: dict) -> bool
    def send_rejection_notification(self, request_data: dict) -> bool

5. Token System (token_gen.py)

Cryptographically secure token management with:

  • URL-safe tokens - Base64-encoded random tokens
  • Expiration handling - Time-based token validity
  • Single-use enforcement - Prevents token reuse
  • Secure validation - Constant-time comparison

6. Frontend Architecture (static/)

Modern, CSP-compliant frontend featuring:

  • External CSS/JS - No inline styles or scripts
  • Responsive Design - Mobile-first approach
  • Progressive Enhancement - Works without JavaScript
  • Accessibility - WCAG 2.1 AA compliance
  • Modern CSS - CSS Grid, Flexbox, custom properties

πŸ” Security Architecture

Content Security Policy (CSP)

Content-Security-Policy:
    default-src 'self';
    style-src 'self' fonts.googleapis.com;
    font-src 'self' fonts.gstatic.com;
    img-src 'self' example.com;
    script-src 'self'

Token Security

  • Cryptographically random tokens (32 bytes)
  • Time-limited validity (configurable expiration)
  • Single-use enforcement
  • Secure transmission (HTTPS recommended)

Input Validation

  • Server-side validation for all user inputs
  • SQL injection prevention via parameterized queries
  • XSS protection via template escaping
  • CSRF protection via token validation

πŸš€ Request Flow

1. Request Submission

User submits form β†’ FastAPI validates β†’ Database stores β†’ Notification sent

2. Approval Process

User clicks approval link β†’ Token validated β†’ Database updated β†’ Success page shown

3. Notification Flow

Event triggered β†’ Notification service called β†’ Message formatted β†’ External service delivery

πŸ“Š Data Flow

graph TD
    A[User Request] --> B[FastAPI Validation]
    B --> C[Database Storage]
    C --> D[Token Generation]
    D --> E[Notification Dispatch]
    E --> F[External Services]

    G[Approval Click] --> H[Token Validation]
    H --> I[Database Update]
    I --> J[Success Response]

    K[Admin Interface] --> L[Request Management]
    L --> M[Bulk Operations]
Loading

πŸ”„ Error Handling

Application Errors

  • Graceful degradation - System continues operating with reduced functionality
  • User-friendly messages - Clear error communication
  • Detailed logging - Comprehensive error tracking
  • Recovery mechanisms - Automatic retry and fallback options

Database Errors

  • Connection pooling - Manages database connections efficiently
  • Transaction rollback - Maintains data consistency
  • Backup strategies - Regular database backups
  • Migration support - Schema version management

πŸ§ͺ Testing Architecture

Test Categories

  • Unit Tests - Individual component testing
  • Integration Tests - Cross-component functionality
  • End-to-End Tests - Full workflow validation
  • Performance Tests - Load and stress testing

Test Structure

tests/
β”œβ”€β”€ conftest.py              # Test configuration
β”œβ”€β”€ test_config.py           # Configuration tests
β”œβ”€β”€ test_database_integration.py # Database tests
β”œβ”€β”€ test_end_to_end.py       # E2E tests
β”œβ”€β”€ test_main_integration.py # Application tests
β”œβ”€β”€ test_webui.py           # Web interface tests
└── test_*.py               # Feature-specific tests

πŸ“ˆ Performance Considerations

Optimization Strategies

  • Database indexing - Optimized query performance
  • Static file caching - Browser cache optimization
  • Template caching - Jinja2 template compilation caching
  • Connection pooling - Efficient resource utilization

Monitoring

  • Request logging - Detailed request/response logging
  • Performance metrics - Response time tracking
  • Error monitoring - Exception tracking and alerting
  • Resource usage - CPU, memory, and disk monitoring

πŸ”§ Development Workflow

Local Development

  1. Setup - Virtual environment and dependencies
  2. Configuration - Development-specific settings
  3. Database - Local SQLite database
  4. Testing - Automated test execution
  5. Debugging - Enhanced logging and debugging tools

Production Deployment

  1. Environment setup - Production server configuration
  2. Database migration - Schema updates and data migration
  3. Static asset optimization - Minification and compression
  4. Process management - Service management and monitoring
  5. Backup strategies - Data protection and recovery

This architecture supports the system's core principles:

  • Simplicity - Easy to understand and maintain
  • Security - Built with security best practices
  • Scalability - Designed to handle growth
  • Maintainability - Clean, documented codebase
  • User Experience - Intuitive and responsive interface