diff --git a/middleware/compress_test.go b/middleware/compress_test.go index 1f60660395..bed0911a95 100644 --- a/middleware/compress_test.go +++ b/middleware/compress_test.go @@ -2,6 +2,7 @@ package middleware import ( "net/http/httptest" + "os" "testing" "github.com/gofiber/fiber" @@ -26,6 +27,5 @@ func Test_Middleware_Compress(t *testing.T) { utils.AssertEqual(t, 200, resp.StatusCode, "Status code") utils.AssertEqual(t, "gzip", resp.Header.Get(fiber.HeaderContentEncoding)) utils.AssertEqual(t, fiber.MIMETextPlainCharsetUTF8, resp.Header.Get(fiber.HeaderContentType)) - // time.Sleep(1 * time.Second) - // os.Remove("./compress.go.fasthttp.gz") + os.Remove("../ctx.go.fiber.gz") } diff --git a/middleware/favicon.go b/middleware/favicon.go new file mode 100644 index 0000000000..ca64ed1291 --- /dev/null +++ b/middleware/favicon.go @@ -0,0 +1,50 @@ +package middleware + +import ( + "io/ioutil" + "strconv" + + "github.com/gofiber/fiber" +) + +// Favicon adds an UUID indentifier to the request +func Favicon(file ...string) fiber.Handler { + var err error + var icon []byte + + // Set lookup if provided + if len(file) > 0 { + icon, err = ioutil.ReadFile(file[0]) + if err != nil { + panic(err) + } + } + // Return handler + return func(c *fiber.Ctx) { + if len(c.Path()) != 12 || c.Path() != "/favicon.ico" { + c.Next() + return + } + + if c.Method() != fiber.MethodGet && c.Method() != fiber.MethodHead { + if c.Method() != fiber.MethodOptions { + c.Status(405) + } else { + c.Status(200) + } + c.Set(fiber.HeaderAllow, "GET, HEAD, OPTIONS") + c.Set(fiber.HeaderContentLength, "0") + return + } + + if len(icon) > 0 { + c.Set(fiber.HeaderContentLength, strconv.Itoa(len(icon))) + c.Set(fiber.HeaderContentType, "image/x-icon") + c.Set(fiber.HeaderCacheControl, "public, max-age=31536000") + c.Status(200).SendBytes(icon) + return + } + + c.Status(204) + } +} diff --git a/middleware/favicon.md b/middleware/favicon.md new file mode 100644 index 0000000000..a84a3dbd68 --- /dev/null +++ b/middleware/favicon.md @@ -0,0 +1,38 @@ +# Favicon + +Why use this middleware? + +- User agents request favicon.ico frequently and indiscriminately, so you may wish to exclude these requests from your logs by using this middleware before your logger middleware. +- This middleware caches the icon in memory to improve performance by skipping disk access. + +**Note** This middleware is exclusively for serving the "default, implicit favicon", which is `GET /favicon.ico`. + +### Example +```go +package main + +import ( + "github.com/gofiber/fiber" + "github.com/gofiber/fiber/middleware" +) + +func main() { + app := fiber.New() + + // Default ignore favicon + app.Use(middleware.Favicon()) + + // Pass favicon + app.Use(middleware.Favicon("./favicon.ico")) + + + app.Use(middleware.Logger()) + + app.Listen(3000) +} +``` + +### Signatures +```go +func Favicon(file ...string) fiber.Handler {} +``` \ No newline at end of file diff --git a/middleware/favicon_test.go b/middleware/favicon_test.go new file mode 100644 index 0000000000..48a6a469e9 --- /dev/null +++ b/middleware/favicon_test.go @@ -0,0 +1,33 @@ +package middleware + +import ( + "net/http/httptest" + "testing" + + "github.com/gofiber/fiber" + "github.com/gofiber/utils" +) + +// go test -run Test_Middleware_Favicon +func Test_Middleware_Favicon(t *testing.T) { + app := fiber.New() + + app.Use(Favicon()) + + app.Get("/", func(ctx *fiber.Ctx) { + ctx.Send("Hello?") + }) + + resp, err := app.Test(httptest.NewRequest("GET", "/favicon.ico", nil)) + utils.AssertEqual(t, nil, err, "app.Test(req)") + utils.AssertEqual(t, 204, resp.StatusCode, "Status code") + + resp, err = app.Test(httptest.NewRequest("OPTIONS", "/favicon.ico", nil)) + utils.AssertEqual(t, nil, err, "app.Test(req)") + utils.AssertEqual(t, 200, resp.StatusCode, "Status code") + + resp, err = app.Test(httptest.NewRequest("PUT", "/favicon.ico", nil)) + utils.AssertEqual(t, nil, err, "app.Test(req)") + utils.AssertEqual(t, 405, resp.StatusCode, "Status code") + utils.AssertEqual(t, "GET, HEAD, OPTIONS", resp.Header.Get(fiber.HeaderAllow)) +} diff --git a/middleware/request_id.md b/middleware/request_id.md index 62174d40c4..b42c8216b6 100644 --- a/middleware/request_id.md +++ b/middleware/request_id.md @@ -1,4 +1,4 @@ -# Request ID +# RequestID Adds an indentifier to the response using the `X-Request-ID` header