def add_middleware(self, middleware: Middleware, index: int | None = None):
        """Add middleware to the app.

        Args:
            middleware: The middleware to add.
            index: The index to add the middleware at.
        """
        if index is None:
            self._middlewares.append(middleware)
        else:
            self._middlewares.insert(index, middleware)
Add middleware to the app.

        Args:
            middleware: The middleware to add.
            index: The index to add the middleware at.
        
reflex/app_mixins/middleware.py