Fiber: 馃悰 logger middleware doesn't work well with etag

Created on 10 Oct 2020  路  8Comments  路  Source: gofiber/fiber

Fiber version
v2.0.6

Issue description
curl -v http://localhost:3000 -H 'If-None-Match: "9-450763418"' return 304 Not Modified with empty body.
but logger print GET / | status: 200 | bytesSent: 9, the status and bytesSent are wrong.

Code snippet

package main

import (
    "github.com/gofiber/fiber/v2"
    "github.com/gofiber/fiber/v2/middleware/logger"
)

func main() {
    app := fiber.New(fiber.Config{ETag: true})

    app.Use(logger.New(logger.Config{
        Format: "${method} ${path} | status: ${status} | bytesSent: ${bytesSent}\n",
    }))
    app.Get("/", func(c *fiber.Ctx) error {
        return c.SendString("with etag")
    })

    app.Listen(":3000")
}
馃彿 Wait for Release

Most helpful comment

a possible sulotion
export etag functionality as a middleware, which can be mounted after logger middleware.
for compatibility, we can mount etag middleware inside app.Listen, and add a deprecate warning to etag option.

All 8 comments

Thanks for opening your first issue here! 馃帀 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

a possible sulotion
export etag functionality as a middleware, which can be mounted after logger middleware.
for compatibility, we can mount etag middleware inside app.Listen, and add a deprecate warning to etag option.

a possible sulotion

export etag functionality as a middleware, which can be mounted after logger middleware.

for compatibility, we can mount etag middleware inside app.Listen, and add a deprecate warning to etag option.

@dhcmrlchtdj Maybe we can use etag option to determine whether we should hit etag middleware

for compatibility, we can mount etag middleware inside app.Listen

hmm, it is infeasible.

package github.com/gofiber/fiber/v2
    imports github.com/gofiber/fiber/v2/middleware/etag
    imports github.com/gofiber/fiber/v2: import cycle not allowed

etag middleware should be placed in fiber core folder.

I encounter some difficulties.

etag middleware should be mounted at a special time.

  1. after logger middleware, otherwise logger maybe print incorrect status code.
  2. before user handlers, otherwise the etag middleware doesn't invoked.

app.Listen does not satisfied the requirements.

Thanks @dhcmrlchtdj for this bug report, the idea to add the ETag logic as a middleware to provide more flexibility was spot on.
https://github.com/gofiber/fiber/pull/926 will be tagged in version v2.1.0 and should solve this problem, I will leave this issue open until it's released 馃憤

ETag middleware is now available in v2.1.0, you can place this mw after the logger 馃憤

package main

import (
    "github.com/gofiber/fiber/v2"
    "github.com/gofiber/fiber/v2/middleware/etag"
    "github.com/gofiber/fiber/v2/middleware/logger"

)

func main() {
    app := fiber.New()

    app.Use(logger.New())
    app.Use(etag.New())

    app.Get("/", func(c *fiber.Ctx) error {
        return c.SendString("with etag")
    })

    app.Listen(":3000")
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

faultable picture faultable  路  3Comments

lucasmdomingues picture lucasmdomingues  路  3Comments

peterbourgon picture peterbourgon  路  4Comments

jeyraj picture jeyraj  路  4Comments

abowloflrf picture abowloflrf  路  4Comments