Skip to content

Conversation

NFUChen
Copy link
Contributor

@NFUChen NFUChen commented Jul 22, 2025

🚀 Feature: Advanced Middleware Architecture System

📋 Overview

This PR introduces significant improvements to the middleware architecture in PySpring, providing developers with powerful tools for managing middleware execution order with fine-grained control and a comprehensive API for middleware manipulation.

✨ New Features

🔧 Middleware Architecture Overhaul

  • Concrete MiddlewareRegistry: Transformed from abstract class to fully functional concrete implementation
  • MiddlewareConfiguration: New configuration class for declarative middleware setup
  • Advanced Middleware Management: Fine-grained control over middleware execution order
  • Comprehensive API: Rich set of methods for middleware manipulation
  • Enhanced Type Safety: Full type annotation coverage with generic support
  • Execution Order Control: Precise positioning with before/after/index-based placement

🔄 Breaking Changes

MiddlewareRegistry Changes

  • Before: MiddlewareRegistry was an abstract class requiring subclassing
  • After: MiddlewareRegistry is now a concrete class with full implementation
  • Migration: Replace custom registry subclasses with MiddlewareConfiguration implementations

Old Pattern:

class MyMiddlewareRegistry(MiddlewareRegistry):
    def get_middleware_classes(self) -> list[Type[Middleware]]:
        return [AuthMiddleware, LoggingMiddleware]

New Pattern:

class MyMiddlewareConfiguration(MiddlewareConfiguration):
    def configure_middlewares(self, registry: MiddlewareRegistry) -> None:
        registry.add_middleware(AuthMiddleware)
        registry.add_middleware(LoggingMiddleware)

⚙️ MiddlewareConfiguration

class AppMiddlewareConfig(MiddlewareConfiguration):
    def configure_middlewares(self, registry: MiddlewareRegistry) -> None:
        # Security first
        registry.add_middleware(SecurityMiddleware)
        
        # Authentication
        registry.add_middleware(AuthMiddleware)
        
        # Request processing
        registry.add_middleware(ValidationMiddleware)
        registry.add_middleware(LoggingMiddleware)
        
        # Advanced positioning
        registry.add_before(ValidationMiddleware, RateLimitMiddleware)
        registry.add_after(AuthMiddleware, PermissionMiddleware)

🎯 Complete API Reference

Method Description Example
add_middleware(middleware) Add middleware to end of chain registry.add_middleware(AuthMiddleware)
add_before(target, new) Add middleware before target registry.add_before(AuthMiddleware, CorsMiddleware)
add_after(target, new) Add middleware after target registry.add_after(AuthMiddleware, LoggingMiddleware)
add_at_index(index, middleware) Add middleware at specific index registry.add_at_index(0, SecurityMiddleware)
remove_middleware(middleware) Remove specific middleware registry.remove_middleware(OldMiddleware)
has_middleware(middleware) Check if middleware exists registry.has_middleware(AuthMiddleware)
get_middleware_index(middleware) Get middleware position registry.get_middleware_index(AuthMiddleware)
get_middleware_count() Get total middleware count registry.get_middleware_count()
get_middleware_classes() Get all middlewares list registry.get_middleware_classes()
clear_middlewares() Remove all middlewares registry.clear_middlewares()

Introduce a new decorator for exception handling and a registry to manage exception handlers. The decorator allows for the registration of handlers for specific exception types, enhancing error management capabilities in the application.
Updated the MiddlewareRegistry class to remove inheritance from SingleInheritanceRequired and added methods for managing middleware registration, including adding, removing, and querying middlewares. Introduced a new MiddlewareConfiguration class for configuring middleware execution order. Updated related imports and adjusted type hints in PySpringApplication for better type safety.
Refactor the test suite for MiddlewareRegistry and introduce tests for MiddlewareConfiguration. Added fixtures for test middleware classes, improved tests for adding and removing middleware, and ensured proper error handling for duplicate and invalid operations. Updated tests to verify middleware execution order and configuration capabilities, ensuring robust coverage for middleware functionality.
@NFUChen NFUChen changed the title Feat/exception handler Feature: Exception Handler System & Middleware Architecture Improvements Jul 22, 2025
Updated the __init__.py file to include ExceptionHandler in the module exports, enhancing the accessibility of the exception handling functionality within the package.
…ine the public API and improve clarity of available functionalities.
@NFUChen NFUChen changed the title Feature: Exception Handler System & Middleware Architecture Improvements Feature: Middleware Enhancement Jul 22, 2025
@NFUChen NFUChen merged commit 9854de0 into main Jul 22, 2025
1 check passed
@NFUChen NFUChen deleted the feat/exception-handler branch July 22, 2025 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants