Skip to content

🐛 [Bug]: FiberApp adaptor initializes fasthttp.RequestCtx with nil logger #3130

@devdodongo

Description

@devdodongo

Bug Description

Using Fiber v3.0.0-beta.3, I found that my application was logging errors when serving static content where the file didn't exist when using the FiberApp adaptor middleware to convert the app to a http.HandlerFunc interface.

Not a huge issue, but rather a small annoyance.

Example:
2024/09/12 17:19:23 0.001 #0000000100000000 - 0.0.0.0:0<->127.0.0.1:53221 - GET http://localhost:8080/notfound - cannot open file "C:\path\to\file\does\not\exist\notfound": open C:\path\to\file\does\not\exist\notfound: The system cannot find the file specified.

It looks like the fasthttp.RequestCtx is being initialized with a nil logger in adaptor.handlerFunc() causing fasthttp to use the default logger when fasthttp.RequestCtx.Init() is invoked

// New fasthttp Ctx
var fctx fasthttp.RequestCtx
fctx.Init(req, remoteAddr, nil) // <-- Nil logger being provided
if len(h) > 0 {
	// New fiber Ctx
	ctx := app.AcquireCtx(&fctx)
	// Execute fiber Ctx
	err := h[0](ctx)
	if err != nil {
		_ = app.Config().ErrorHandler(ctx, err) //nolint:errcheck // not needed
	}
} else {
	// Execute fasthttp Ctx though app.Handler
	app.Handler()(&fctx)
}

How to Reproduce

Steps to reproduce the behavior:

  1. Create a new Fiber App
app := fiber.New()
  1. Serve static files
app.Get("/*", static.New("/path/to/static/files")
  1. Convert app to http.HandlerFunc
var h http.Handler = adaptor.FiberApp(app)
  1. ListenAndServe
server := &http.Server{
    Handler: h,
    Addr: ":8080"
}

panic(server.ListenAndServe())
  1. Navigate to something in browser that doesn't exist and view logs i.e. localhost:8080/notexists

Expected Behavior

Expecting the Noop logger to be invoked, leading to no logs being written.

Fiber Version

v3.0.0-beta.3

Code Snippet (optional)

package main

import (
	"log"
	"net/http"

	"github.com/gofiber/fiber/v3"
	"github.com/gofiber/fiber/v3/middleware/adaptor"
	"github.com/gofiber/fiber/v3/middleware/static"
)

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

	// Contains an index.html only
	app.Get("/*", static.New("./templates"))

	var h http.Handler = adaptor.FiberApp(app)

	server := &http.Server{
		Handler: h,
		Addr:    ":8080",
	}

	log.Fatal(server.ListenAndServe())
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my problem prior to opening this one.
  • I understand that improperly formatted bug reports may be closed without explanation.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions