mirror of https://github.com/gofiber/fiber.git
PR: add url for favicon middleware, for correct handling different of… (#2231)
* PR: add url for favicon middleware, for correct handling different of ico formats * pr: efectn > URL would be better naming i think * pr: add test case * apply reviews * remove json annotinos, since they are unnecessary * readme fixes * linting fixes --------- Co-authored-by: koalan <kolesnikov.khv@gmail.com> Co-authored-by: Muhammed Efe Çetin <efectn@protonmail.com>pull/2324/head
parent
2820aef585
commit
21cd45b750
|
@ -2,7 +2,7 @@
|
|||
|
||||
Favicon middleware for [Fiber](https://github.com/gofiber/fiber) that ignores favicon requests or caches a provided icon in memory to improve performance by skipping disk access. 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.
|
||||
|
||||
**Note** This middleware is exclusively for serving the default, implicit favicon, which is GET /favicon.ico.
|
||||
**Note** This middleware is exclusively for serving the default, implicit favicon, which is GET /favicon.ico or [custom favicon URL](#config).
|
||||
|
||||
## Table of Contents
|
||||
- [Favicon Middleware](#favicon-middleware)
|
||||
|
@ -13,6 +13,7 @@ Favicon middleware for [Fiber](https://github.com/gofiber/fiber) that ignores fa
|
|||
- [Custom Config](#custom-config)
|
||||
- [Config](#config)
|
||||
- [Default Config](#default-config-1)
|
||||
|
||||
## Signatures
|
||||
|
||||
```go
|
||||
|
@ -42,6 +43,7 @@ app.Use(favicon.New())
|
|||
```go
|
||||
app.Use(favicon.New(favicon.Config{
|
||||
File: "./favicon.ico",
|
||||
URL: "/favicon.ico",
|
||||
}))
|
||||
```
|
||||
|
||||
|
@ -59,6 +61,22 @@ type Config struct {
|
|||
//
|
||||
// Optional. Default: ""
|
||||
File string
|
||||
|
||||
// URL for favicon handler
|
||||
//
|
||||
// Optional. Default: "/favicon.ico"
|
||||
URL string
|
||||
|
||||
// FileSystem is an optional alternate filesystem to search for the favicon in.
|
||||
// An example of this could be an embedded or network filesystem
|
||||
//
|
||||
// Optional. Default: nil
|
||||
FileSystem http.FileSystem
|
||||
|
||||
// CacheControl defines how the Cache-Control header in the response should be set
|
||||
//
|
||||
// Optional. Default: "public, max-age=31536000"
|
||||
CacheControl string
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -67,6 +85,7 @@ type Config struct {
|
|||
```go
|
||||
var ConfigDefault = Config{
|
||||
Next: nil,
|
||||
File: ""
|
||||
File: "",
|
||||
URL: "/favicon.ico",
|
||||
}
|
||||
```
|
||||
|
|
|
@ -19,24 +19,30 @@ type Config struct {
|
|||
// File holds the path to an actual favicon that will be cached
|
||||
//
|
||||
// Optional. Default: ""
|
||||
File string `json:"file"`
|
||||
File string
|
||||
|
||||
// URL for favicon handler
|
||||
//
|
||||
// Optional. Default: "/favicon.ico"
|
||||
URL string
|
||||
|
||||
// FileSystem is an optional alternate filesystem to search for the favicon in.
|
||||
// An example of this could be an embedded or network filesystem
|
||||
//
|
||||
// Optional. Default: nil
|
||||
FileSystem http.FileSystem `json:"-"`
|
||||
FileSystem http.FileSystem
|
||||
|
||||
// CacheControl defines how the Cache-Control header in the response should be set
|
||||
//
|
||||
// Optional. Default: "public, max-age=31536000"
|
||||
CacheControl string `json:"cache_control"`
|
||||
CacheControl string
|
||||
}
|
||||
|
||||
// ConfigDefault is the default config
|
||||
var ConfigDefault = Config{
|
||||
Next: nil,
|
||||
File: "",
|
||||
URL: fPath,
|
||||
CacheControl: "public, max-age=31536000",
|
||||
}
|
||||
|
||||
|
@ -60,6 +66,9 @@ func New(config ...Config) fiber.Handler {
|
|||
if cfg.Next == nil {
|
||||
cfg.Next = ConfigDefault.Next
|
||||
}
|
||||
if cfg.URL == "" {
|
||||
cfg.URL = ConfigDefault.URL
|
||||
}
|
||||
if cfg.File == "" {
|
||||
cfg.File = ConfigDefault.File
|
||||
}
|
||||
|
@ -99,7 +108,7 @@ func New(config ...Config) fiber.Handler {
|
|||
}
|
||||
|
||||
// Only respond to favicon requests
|
||||
if c.Path() != fPath {
|
||||
if c.Path() != cfg.URL {
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
|
|
|
@ -79,6 +79,26 @@ func Test_Middleware_Favicon_Found(t *testing.T) {
|
|||
utils.AssertEqual(t, "public, max-age=31536000", resp.Header.Get(fiber.HeaderCacheControl), "CacheControl Control")
|
||||
}
|
||||
|
||||
// go test -run Test_Custom_Favicon_Url
|
||||
func Test_Custom_Favicon_Url(t *testing.T) {
|
||||
app := fiber.New()
|
||||
const customURL = "/favicon.svg"
|
||||
app.Use(New(Config{
|
||||
File: "../../.github/testdata/favicon.ico",
|
||||
URL: customURL,
|
||||
}))
|
||||
|
||||
app.Get("/", func(c *fiber.Ctx) error {
|
||||
return nil
|
||||
})
|
||||
|
||||
resp, err := app.Test(httptest.NewRequest(http.MethodGet, customURL, nil))
|
||||
|
||||
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode, "Status code")
|
||||
utils.AssertEqual(t, "image/x-icon", resp.Header.Get(fiber.HeaderContentType))
|
||||
}
|
||||
|
||||
// mockFS wraps local filesystem for the purposes of
|
||||
// Test_Middleware_Favicon_FileSystem located below
|
||||
// TODO use os.Dir if fiber upgrades to 1.16
|
||||
|
|
Loading…
Reference in New Issue