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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃敟 Support GZip / Deflate #181

Closed
hyingreborn opened this issue Feb 23, 2020 · 9 comments
Closed

馃敟 Support GZip / Deflate #181

hyingreborn opened this issue Feb 23, 2020 · 9 comments

Comments

@hyingreborn
Copy link

Is your feature request related to a problem?
Now, the gzip is built in Static()锛宻o I can't customize which paths to open gzip and which file sizes to open gzip

Describe the solution you'd like
In my project锛宨 need open gzip for staticfiles锛宎t the same time, i want open gzip for restful api module(when the response is bigger than 4kb)

Describe alternatives you've considered
gzip be removed from Static() and exist as a official middleware

Additional context

@Fenny
Copy link
Member

Fenny commented Feb 23, 2020

@hyingreborn, we have the option to disable gzipping for the Static files and c.SendFile method. If I'm correct you would like to have an option to enable gzipping for any kind of response besides files?

@hyingreborn
Copy link
Author

I want gzip to work in my custom path锛宯ot only static files锛宖or example

// gzip work in static path
app.Static("/app/", "./public/")
app.Use("/app", gzip())

// gzip work in api module
app := app.Group("/api", gzip())
//...

@koddr
Copy link
Contributor

koddr commented Feb 25, 2020

@hyingreborn after Fiber v1.7.0, we can use multiple middlewares at app.Method and app.Group. You can write your own midware func and use it.

@Fenny good idea to exclude gzip feature to github.com/gofiber/middlewares for easy use on everywhere in app code.

@hyingreborn
Copy link
Author

hyingreborn commented Feb 25, 2020

I will use these two middleware when developing with gin, hoping to provide you with inspiration
github.com/gin-contrib/static
github.com/nanmu42/gzip

@Fenny
Copy link
Member

Fenny commented Feb 27, 2020

Thanks @hyingreborn for your suggestion, we will release a middleware folder soon and I will take a look if a gzipping middleware is possible with fasthttp!

@renanbastos93
Copy link
Member

Hi guys

Can I help you? I wish this challenge.

For context about it.
Do we you wishing implement GZIP in all routes don't just files, right?
Using according to example

app.Static("/app/", "./public/")
app.Use("/app", fiber.gzip())
app := app.Group("/api", fiber.gzip())

See ya

@renanbastos93
Copy link
Member

I saw now and FastHTTP have a compress gzip
https://github.com/valyala/fasthttp/blob/32793db72d04141d333eb04ce60170db6e79e6d2/compress.go

So, we should use Content-Encoding how gzip.
example:

func Gzip(c *fiber.Ctx) {
	c.Set("Content-Encoding", "gzip")
	c.Next()
}
app.Group("/api", gzip)

Well, I believe that is possible using gzip on Fiber then I liked this approach to create a package for our middlewares.

Can we create a todo list to implement these middlewares?

I can't test this approach now, when possible I'll test.

see ya

@Fenny
Copy link
Member

Fenny commented Feb 28, 2020

I currently got the compression working, here is my proposal for v1.8.1:
PS: This API is not final yet, just want to hear your thoughts.

func main() {
  app := fiber.New(&fiber.Settings{
    Compression: true, // global compression enabled
  })
  app.Get("/", func(c *fiber.Ctx) {
    c.Send("Hello, World!") // compressed
  })
  app.Get("/demo", func(c *fiber.Ctx) {
    c.Compress(false)
    c.Send("hello, World!") // not compressed
  })
}
func main() {
  app := fiber.New() // compression disabled by default
  app.Get("/", func(c *fiber.Ctx) {
    c.Compress()
    c.Send("Hello, World!") // compressed
  })
  app.Get("/demo", func(c *fiber.Ctx) {
    c.Send("hello, World!") // not compressed
  })
}
// Write own middleware
func gzip(c *fiber.Ctx) {
  c.Compress()
  c.Next()
}
func main() {
  app := fiber.New() // compression disabled by default
  app.Get("/", gzip, func(c *fiber.Ctx) {
    c.Send("Hello, World!") // compressed
  })
  app.Get("/demo", func(c *fiber.Ctx) {
    c.Send("hello, World!") // not compressed
  })
}

@renanbastos93, @hyingreborn, @koddr, let me know what you guys think.

@Fenny Fenny changed the title 馃敟 Can gzip be removed from static() and exist as a official middleware? 馃敟 Support GZip / Deflate Feb 28, 2020
@Fenny
Copy link
Member

Fenny commented Mar 1, 2020

Added in v1.8.1

@Fenny Fenny closed this as completed Mar 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants