Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: feat: add gzip and brotli middleware #232

Closed
wants to merge 3 commits into from

Conversation

mauri870
Copy link
Contributor

@mauri870 mauri870 commented Jul 4, 2023

goravel/goravel#208

Closes goravel/goravel#208

πŸ“‘ Description

βœ… Checks

  • My pull request adheres to the code style of this project
  • My code requires changes to the documentation
  • I have updated the documentation as required
  • All the tests have passed

β„Ή Additional Information

What is missing:

  • Update docs, add gzip and brotli to Routing
  • Create config files for gzip and brotli

Thx @Marian0 for the help getting gzip compression to work ✌️

@mauri870
Copy link
Contributor Author

mauri870 commented Jul 4, 2023

@hwbrzzl One thing to take into consideration is that the brotli middleware I choose does not handle fallback to gzip. We can't also chain .GlobalMiddleware(middlewares.Brotli(), middlewares.Gzip()) since we'll end up with a brotli compressed gzip payload (or a gzip compressed brotli payload xD).

My take on it is that we should aim for a global Compress middleware with a respective compress.go config file where you can adjust the options and also enable brotli, gzip, deflate or maybe all of them at once. That would make things more easier than maintaining multiple middlewares. Please let me know what you think about it.

@hwbrzzl
Copy link
Contributor

hwbrzzl commented Jul 5, 2023

@hwbrzzl One thing to take into consideration is that the brotli middleware I choose does not handle fallback to gzip. We can't also chain .GlobalMiddleware(middlewares.Brotli(), middlewares.Gzip()) since we'll end up with a brotli compressed gzip payload (or a gzip compressed brotli payload xD).

My take on it is that we should aim for a global Compress middleware with a respective compress.go config file where you can adjust the options and also enable brotli, gzip, deflate or maybe all of them at once. That would make things more easier than maintaining multiple middlewares. Please let me know what you think about it.

Yes, it's better to add a configuration to control it. There's a problem, we want to implement multiple HTTP drivers: goravel/gin, goravel/filber, they are developing, and we have another PR to refactor HTTP and Route modules, so can we make these two middleware in this PR more generic? Change gin to net/http, we will also try to change the CORS middleware.

@mauri870
Copy link
Contributor Author

mauri870 commented Jul 5, 2023

Since we are aiming to support different http drivers it makes sense to try to make the compression midlewares generic. I will work on it πŸ™

Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great PR! I wonder if we can add some unit tests for this PR. If it's hard to add it, please add some screenshots that test this feature locally, and please list the new configurations added in this PR here.

http/middleware/brotli.go Outdated Show resolved Hide resolved
return func(ctx httpcontract.Context) {
switch ctx := ctx.(type) {
case *http.GinContext:
newGzip(facades.Config().GetInt("gzip.compression_level"))(ctx.Instance())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

@mauri870 mauri870 changed the title feat: add gzip and brotli middleware WIP: feat: add gzip and brotli middleware Jul 5, 2023
@Marian0
Copy link
Contributor

Marian0 commented Jul 5, 2023

@mauri870 great job! Thanks for creating this PR - I tested it locally on a sample project and both compressions are working as expected. Thanks again πŸ™Œ

@codecov
Copy link

codecov bot commented Jul 5, 2023

Codecov Report

Patch coverage: 96.25% and project coverage change: +0.18 πŸŽ‰

Comparison is base (989ae8b) 62.76% compared to head (b380a98) 62.94%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #232      +/-   ##
==========================================
+ Coverage   62.76%   62.94%   +0.18%     
==========================================
  Files         128      130       +2     
  Lines        8314     8347      +33     
==========================================
+ Hits         5218     5254      +36     
+ Misses       2735     2733       -2     
+ Partials      361      360       -1     
Impacted Files Coverage Ξ”
schedule/service_provider.go 0.00% <0.00%> (ΓΈ)
support/database/database.go 79.31% <0.00%> (ΓΈ)
contracts/foundation/service_provider.go 50.00% <50.00%> (ΓΈ)
schedule/application.go 94.33% <100.00%> (+1.15%) ⬆️
schedule/event.go 100.00% <100.00%> (ΓΈ)
support/debug/dump.go 100.00% <100.00%> (ΓΈ)

... and 1 file with indirect coverage changes

β˜” View full report in Codecov by Sentry.
πŸ“’ Do you have feedback about the report comment? Let us know in this issue.

gbrotli "github.com/anargu/gin-brotli"
"github.com/andybalholm/brotli"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Can we remove the gin dependency totally?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it should be possible I think. I found this package but it requires an http.Handler as input. Any clue how to use this here instead of the gin one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may need to read the source code to understand how to implement Gzip.

By the way, I missed one thing, we can use Gzip by nginx, is it necessary to implement Gzip through the framework? Most time, we need to use Nginx to make a request forwarding. cc @Marian0

Copy link
Contributor Author

@mauri870 mauri870 Jul 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My 2 cents is that it is something worth it to have in the framework. There was a time until ~2016 that it was preferred to always expose go services being a proxy, (such as nginx),. But then companies started to expose their go services directly and I believe notw it is actually safe to do that. Ofc you do not get the benefits of a true proxy but compression is a nice to have for such cases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense, let's go ahead. πŸ‘

@devhaozi devhaozi changed the title WIP: feat: add gzip and brotli middleware wip: feat: add gzip and brotli middleware Jul 6, 2023
@mauri870
Copy link
Contributor Author

@hwbrzzl I was unable to get this working without relying on gin. I think we should wait for the http refactoring before moving forward with this task.

@hwbrzzl
Copy link
Contributor

hwbrzzl commented Jul 17, 2023

Hi @mauri870 Sorry for the late, I have a travel last week. Sure, make sense, let's wait a bit until the HTTP refactor is finished.

@hwbrzzl
Copy link
Contributor

hwbrzzl commented Aug 29, 2023

Hi @mauri870 The HTTP refactor is completed, we can move on now.

@hwbrzzl hwbrzzl closed this Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

✨ [Feature] Gzip compression support
4 participants