Fiber: 馃敟 Support GZip / Deflate

Created on 23 Feb 2020  路  9Comments  路  Source: gofiber/fiber

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

Most helpful comment

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.

All 9 comments

@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?

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())
//...

@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.

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

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!

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

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

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.

Added in v1.8.1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

renanbastos93 picture renanbastos93  路  3Comments

Aguezz picture Aguezz  路  4Comments

MohamedGouaouri picture MohamedGouaouri  路  3Comments

Terisback picture Terisback  路  3Comments

koddr picture koddr  路  4Comments