-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #459 from Fenny/master
Add Favicon middleware
- Loading branch information
Showing
5 changed files
with
124 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|