Skip to content

Commit

Permalink
Merge pull request #459 from Fenny/master
Browse files Browse the repository at this point in the history
Add Favicon middleware
  • Loading branch information
Fenny authored Jun 8, 2020
2 parents 89bcc76 + 2b722aa commit 164fbb3
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 3 deletions.
4 changes: 2 additions & 2 deletions middleware/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package middleware

import (
"net/http/httptest"
"os"
"testing"

"github.com/gofiber/fiber"
Expand All @@ -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")
}
50 changes: 50 additions & 0 deletions middleware/favicon.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
38 changes: 38 additions & 0 deletions middleware/favicon.md
Original file line number Diff line number Diff line change
@@ -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 {}
```
33 changes: 33 additions & 0 deletions middleware/favicon_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
2 changes: 1 addition & 1 deletion middleware/request_id.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Request ID
# RequestID

Adds an indentifier to the response using the `X-Request-ID` header

Expand Down

0 comments on commit 164fbb3

Please sign in to comment.