aio-libs / aio-libs/aiohttp-debugtoolbar

Improve documentation on setting up middleware correctly

Đang mở
#476 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
JavaScript
Star
202
Fork
29
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

We need to explain how to setup the middleware correctly so that it doesn't interfere with other middlewares.

This is the function I use in my projects:
```
def setup_debugtoolbar(app: web.Application) -> None:
"""Initialise debugtoolbar for app."""
def is_error_middleware(f: _Middleware) -> bool:
return getattr(f, "func", f) == error_middleware

# Find the index of error_middleware (which may be a functools.partial).
found_middleware = next(filter(is_error_middleware, app.middlewares))

# Add debugtoolbar middleware to just before app's error handler.
# This avoids debugtoolbar interrupting other middlewares.
insert_pos = app.middlewares.index(found_middleware) + 1
app.middlewares.insert(insert_pos, aiohttp_debugtoolbar.middleware)

# setup() won't add the middleware if it's already present.
aiohttp_debugtoolbar.setup(app, check_host=False)
```

Which is called after the app's normal middlewares have already been configured.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.