Starlette: [Document] understanding middleware system

Created on 13 Apr 2019  路  6Comments  路  Source: encode/starlette

I'm trying to understand the base middleware layer. For example I don't get the prupose of this part

https://github.com/encode/starlette/blob/master/starlette/middleware/base.py#L30

Anyone can help me?

Most helpful comment

Thanks again! We are starting a new core product, mostly based on your toolkit (starlette, typesystem and databases). For the orm like, we just produced our own Object Mapper, based mostly on sqlalchemy declarative layer (https://github.com/vinissimus/asyncom).
A discussion group will be awesome, we will try to contribute to the project as much as possible (as active users of it). On the near future, we can try to ask our employer if it can be an encode sponsor :)
Anyway, as an old django user (thought from the 1.0 release).. I see this, like a clean and nicer alternative.. You had made an awesome job. Congrats!

All 6 comments

Okay, it's a usage question rather than an issue, but we don't yet have a discussion group for this project so I guess this is a reasonable place to ask.

The middleware in Starlette is ASGI based, rather than request/response.

However in BaseHTTPMiddleware we need to return a response instance to the user.

The way we're able to do this is to ensure that the ASGI send argument that we pass on to the rest of the middleware chain doesn't fire messages back directly, but instead pushes them into a queue that we pull messages off, and use to construct a StreamingResponse instance.

Really this is all implementation detail here, the end devloper shouldn't need to know how it works in order to use BaseHTTPMiddleware correctly.

Thanks again! We are starting a new core product, mostly based on your toolkit (starlette, typesystem and databases). For the orm like, we just produced our own Object Mapper, based mostly on sqlalchemy declarative layer (https://github.com/vinissimus/asyncom).
A discussion group will be awesome, we will try to contribute to the project as much as possible (as active users of it). On the near future, we can try to ask our employer if it can be an encode sponsor :)
Anyway, as an old django user (thought from the 1.0 release).. I see this, like a clean and nicer alternative.. You had made an awesome job. Congrats!

Just checking the implementations of the BaseHTTPMiddleware, found that every middelware will add two queues and one coro per middleware per request... Seems like too much, for just providing a friendly API to the middleware chain, don't?

@jordic - The alternative is to drop down into writing the middleware at the ASGI level instead - which isn't really too complicated, but it's nice to have an easy option. All the built-in options are just at the ASGI level, so I'm not really concerned about any modest overheads that the BaseHTTPmiddleware implementation might (or might not) introduce.

so many thanks, we have go this way... With asgi3 and your examples, it's easy to write pure middlewares.. got it! Anyway, so many thanks on this awesome lib. Soon, will try to start doing some blog posts about how we use starlette for our asyncio based services :)
Will try to OS some other modules: A transaction middleware, some typsystem schema helpers.. (like django forms... )

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hyperknot picture hyperknot  路  6Comments

tomchristie picture tomchristie  路  5Comments

zhammer picture zhammer  路  5Comments

berislavlopac picture berislavlopac  路  5Comments

dgrahn picture dgrahn  路  4Comments