litestar-middleware

Configure Litestar middleware for cross-cutting HTTP and WebSocket request behavior.

Updated Aug 17, 2026
One-click install
npx skills add https://github.com/renjianguo666/litecms --skill litestar-middleware-renjianguo666
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: litestar-middleware
Source: https://github.com/renjianguo666/litecms/tree/main/.agents/skills/litestar-middleware
Command: npx skills add https://github.com/renjianguo666/litecms --skill litestar-middleware-renjianguo666

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires litestar, structlog, and includes references (resource) components.

What problem does it solve? It guides developers through configuring Litestar middleware correctly, covering built-in configs, custom ASGI middleware, scope filtering, ordering, and auth user loading without mixing in business logic. ## Core Features & Use Cases - Built-in Middleware Configs: Apply CORSConfig, CSRFConfig, AllowedHostsConfig, and CompressionConfig before writing custom middleware. - Custom ASGI Middleware: Implement AbstractMiddleware with scope filtering and path exclusion for timing, logging, and request IDs. - Auth Middleware: Use AbstractAuthenticationMiddleware to populate connection.user and connection.auth for Guards. - Use Case: Add structured request logging that skips health check endpoints and applies only to HTTP scopes while leaving WebSocket connections untouched. ## Quick Start Ask the assistant to add a timing middleware to your Litestar app that logs request duration for HTTP routes only.

Frequently Asked Questions about litestar-middleware

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I create custom middleware in Litestar?

Subclass AbstractMiddleware and implement an async __call__ method that receives scope, receive, and send. Register it in the Litestar app's middleware list, and use the scopes attribute to limit it to HTTP or WebSocket traffic.

How do I add authentication middleware in Litestar?

Extend AbstractAuthenticationMiddleware and implement authenticate_request to return an AuthenticationResult with the user and auth payload. This populates connection.user and connection.auth so Guards can read them without re-decoding tokens.

Does Litestar middleware apply to WebSocket connections?

By default middleware applies to both HTTP and WebSocket scopes. Set scopes = {ScopeType.HTTP} on your AbstractMiddleware subclass to skip WebSocket connections, or include both scope types explicitly.

How do I exclude certain paths from Litestar middleware?

Set the exclude attribute on your AbstractMiddleware subclass to a list of regex patterns matched against scope["path"]. This is commonly used for health checks, metrics endpoints, and schema routes.

When should I use Guards instead of middleware in Litestar?

Use Guards for business policy and permission checks, and middleware only for cross-cutting concerns like logging, timing, and auth user loading. Duplicating Guard policy inside middleware is discouraged.