GithubHelp home page GithubHelp logo

Middleware documentation about starlette HOT 1 CLOSED

encode avatar encode commented on May 14, 2024
Middleware documentation

from starlette.

Comments (1)

jordaneremieff avatar jordaneremieff commented on May 14, 2024

This is just a rough outline of something I have thought about before in regards to how an ASGI application could use a list of middleware passed to a main application. I've tailored it to a Starlette-like application because this how I would use it - curious if something along these lines (or any part of it) could work here:

class Starlette:
    def __init__(self, middlewares: Sequence[ASGIApp] = []) -> None:
        self.middlewares = middlewares

    def compose_middleware(self, app: ASGIApp, scope: Scope) -> ASGIApp:
        # Apply `self.middlewares` here
        # Perhaps something similar to how AuthMiddlewareStack works in channels?
        return app

    def add_middleware(self, app: ASGIApp) -> None:
        # For when we want to `app.add_middleware(middleware)`
        self.middlewares.append(app)

    def __call__(self, scope: Scope) -> ASGIInstance:
        scope["app"] = self
        return self.compose_middleware(app, scope)

If we could identify a robust means of composing any list of ASGI application middleware, then I'd imagine it would be flexible enough to do things like:

from somepackage import generic_middleware
from mypackage import my_middleware_middleware, my_other_middleware_specific_middleware

my_middleware = Starlette(
    middlewares=[my_middleware_middleware, my_other_middleware_specific_middleware]
)
app = Starlette(middlewares=[generic_middleware, my_middleware])

Obviously this is a strange example, but just wanted to illustrate the composability aspect.

from starlette.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.