Skip to content

Commit 164fbb3

Browse files
authored
Merge pull request #459 from Fenny/master
Add Favicon middleware
2 parents 89bcc76 + 2b722aa commit 164fbb3

File tree

5 files changed

+124
-3
lines changed

5 files changed

+124
-3
lines changed

middleware/compress_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package middleware
22

33
import (
44
"net/http/httptest"
5+
"os"
56
"testing"
67

78
"github.com/gofiber/fiber"
@@ -26,6 +27,5 @@ func Test_Middleware_Compress(t *testing.T) {
2627
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
2728
utils.AssertEqual(t, "gzip", resp.Header.Get(fiber.HeaderContentEncoding))
2829
utils.AssertEqual(t, fiber.MIMETextPlainCharsetUTF8, resp.Header.Get(fiber.HeaderContentType))
29-
// time.Sleep(1 * time.Second)
30-
// os.Remove("./compress.go.fasthttp.gz")
30+
os.Remove("../ctx.go.fiber.gz")
3131
}

middleware/favicon.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package middleware
2+
3+
import (
4+
"io/ioutil"
5+
"strconv"
6+
7+
"github.com/gofiber/fiber"
8+
)
9+
10+
// Favicon adds an UUID indentifier to the request
11+
func Favicon(file ...string) fiber.Handler {
12+
var err error
13+
var icon []byte
14+
15+
// Set lookup if provided
16+
if len(file) > 0 {
17+
icon, err = ioutil.ReadFile(file[0])
18+
if err != nil {
19+
panic(err)
20+
}
21+
}
22+
// Return handler
23+
return func(c *fiber.Ctx) {
24+
if len(c.Path()) != 12 || c.Path() != "/favicon.ico" {
25+
c.Next()
26+
return
27+
}
28+
29+
if c.Method() != fiber.MethodGet && c.Method() != fiber.MethodHead {
30+
if c.Method() != fiber.MethodOptions {
31+
c.Status(405)
32+
} else {
33+
c.Status(200)
34+
}
35+
c.Set(fiber.HeaderAllow, "GET, HEAD, OPTIONS")
36+
c.Set(fiber.HeaderContentLength, "0")
37+
return
38+
}
39+
40+
if len(icon) > 0 {
41+
c.Set(fiber.HeaderContentLength, strconv.Itoa(len(icon)))
42+
c.Set(fiber.HeaderContentType, "image/x-icon")
43+
c.Set(fiber.HeaderCacheControl, "public, max-age=31536000")
44+
c.Status(200).SendBytes(icon)
45+
return
46+
}
47+
48+
c.Status(204)
49+
}
50+
}

middleware/favicon.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Favicon
2+
3+
Why use this middleware?
4+
5+
- 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.
6+
- This middleware caches the icon in memory to improve performance by skipping disk access.
7+
8+
**Note** This middleware is exclusively for serving the "default, implicit favicon", which is `GET /favicon.ico`.
9+
10+
### Example
11+
```go
12+
package main
13+
14+
import (
15+
"github.com/gofiber/fiber"
16+
"github.com/gofiber/fiber/middleware"
17+
)
18+
19+
func main() {
20+
app := fiber.New()
21+
22+
// Default ignore favicon
23+
app.Use(middleware.Favicon())
24+
25+
// Pass favicon
26+
app.Use(middleware.Favicon("./favicon.ico"))
27+
28+
29+
app.Use(middleware.Logger())
30+
31+
app.Listen(3000)
32+
}
33+
```
34+
35+
### Signatures
36+
```go
37+
func Favicon(file ...string) fiber.Handler {}
38+
```

middleware/favicon_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package middleware
2+
3+
import (
4+
"net/http/httptest"
5+
"testing"
6+
7+
"github.com/gofiber/fiber"
8+
"github.com/gofiber/utils"
9+
)
10+
11+
// go test -run Test_Middleware_Favicon
12+
func Test_Middleware_Favicon(t *testing.T) {
13+
app := fiber.New()
14+
15+
app.Use(Favicon())
16+
17+
app.Get("/", func(ctx *fiber.Ctx) {
18+
ctx.Send("Hello?")
19+
})
20+
21+
resp, err := app.Test(httptest.NewRequest("GET", "/favicon.ico", nil))
22+
utils.AssertEqual(t, nil, err, "app.Test(req)")
23+
utils.AssertEqual(t, 204, resp.StatusCode, "Status code")
24+
25+
resp, err = app.Test(httptest.NewRequest("OPTIONS", "/favicon.ico", nil))
26+
utils.AssertEqual(t, nil, err, "app.Test(req)")
27+
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
28+
29+
resp, err = app.Test(httptest.NewRequest("PUT", "/favicon.ico", nil))
30+
utils.AssertEqual(t, nil, err, "app.Test(req)")
31+
utils.AssertEqual(t, 405, resp.StatusCode, "Status code")
32+
utils.AssertEqual(t, "GET, HEAD, OPTIONS", resp.Header.Get(fiber.HeaderAllow))
33+
}

middleware/request_id.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Request ID
1+
# RequestID
22

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

0 commit comments

Comments
 (0)