-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Description
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:
- Create a new Fiber App
app := fiber.New()- Serve static files
app.Get("/*", static.New("/path/to/static/files")- Convert app to http.HandlerFunc
var h http.Handler = adaptor.FiberApp(app)- ListenAndServe
server := &http.Server{
Handler: h,
Addr: ":8080"
}
panic(server.ListenAndServe())- 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.