itsthejoker / spiderweb

A small web framework, just big enough for a spider.
https://itsthejoker.github.io/spiderweb/
Other
47 stars 0 forks source link

GzipMiddleware #3

Closed martinnedopil closed 1 day ago

martinnedopil commented 2 days ago

Hi,

I have question. How middleware chain works? I am trying to create GzipMiddleware to gzip content and reduce size... But it looks TemplateResponse came to my middleware too early and looks like not processed by Jinja yet. Do you have any idea, how to solve?

Thanks

Martin

itsthejoker commented 2 days ago

Hi, Martin!

You have a great point — I didn't think about that when I built the middleware system. I have a new fix coming that will add some entry points for this kind of operation — I expect it will be up in a few hours.

itsthejoker commented 2 days ago

@martinnedopil I just released 1.3.0 that adds a new middleware hook. I think this will solve your issue! https://itsthejoker.github.io/spiderweb/#/middleware/custom_middleware?id=post_processself-request-request-rendered_response-str-gt-str

martinnedopil commented 1 day ago

Hi Joe, this scenario is good (and quick) but do not solve my problem. Writting GzipMiddleware I need... In process_response method adapt response.headers["Content-Length"] to size of compressed response. But I am not able to get size of compressed file because compress get action in post_process and there I can change only response content string. And I also need to change header. For me will be nice to adapt def post_process(self, request: Request, rendered_response: str) -> str: signature to def post_process(self, request: Request, response:HTTPResponse, rendered_response: str) -> str: this allows me to change headers.

It is only an idea.

Thank you

Martin

itsthejoker commented 1 day ago

Ohhhh crap you're right, I see. Okay, let me see if there's a better way to do this.

itsthejoker commented 1 day ago

@martinnedopil I've just pushed up 1.3.1 that addresses that, and I've updated the documentation to show how headers can be changed in the final response.

https://itsthejoker.github.io/spiderweb/#/middleware/custom_middleware?id=post_processself-request-request-response-httpresponse-rendered_response-str-gt-str