Skip to content

Commit

Permalink
PR: add url for favicon middleware, for correct handling different of… (
Browse files Browse the repository at this point in the history
#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>
  • Loading branch information
3 people committed Feb 3, 2023
1 parent 2820aef commit 21cd45b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
23 changes: 21 additions & 2 deletions middleware/favicon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -42,6 +43,7 @@ app.Use(favicon.New())
```go
app.Use(favicon.New(favicon.Config{
File: "./favicon.ico",
URL: "/favicon.ico",
}))
```

Expand All @@ -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
}
```

Expand All @@ -67,6 +85,7 @@ type Config struct {
```go
var ConfigDefault = Config{
Next: nil,
File: ""
File: "",
URL: "/favicon.ico",
}
```
17 changes: 13 additions & 4 deletions middleware/favicon/favicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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()
}

Expand Down
20 changes: 20 additions & 0 deletions middleware/favicon/favicon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 21cd45b

Please sign in to comment.