Skip to content

Commit 0ca295a

Browse files
authored
🧹 chore: Refactor configuration management for favicon and envvar middlewares (#3898)
1 parent 35ac7db commit 0ca295a

File tree

4 files changed

+100
-73
lines changed

4 files changed

+100
-73
lines changed

middleware/envvar/config.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package envvar
2+
3+
// Config defines the config for middleware.
4+
type Config struct {
5+
// ExportVars specifies the environment variables that should export
6+
ExportVars map[string]string
7+
}
8+
9+
// ConfigDefault is the default config.
10+
var ConfigDefault = Config{
11+
ExportVars: map[string]string{},
12+
}
13+
14+
func configDefault(config ...Config) Config {
15+
if len(config) == 0 {
16+
return ConfigDefault
17+
}
18+
19+
cfg := config[0]
20+
21+
if cfg.ExportVars == nil {
22+
cfg.ExportVars = ConfigDefault.ExportVars
23+
}
24+
25+
return cfg
26+
}

middleware/envvar/envvar.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ import (
88

99
const hAllow = fiber.MethodGet + ", " + fiber.MethodHead
1010

11-
// Config defines the config for middleware.
12-
type Config struct {
13-
// ExportVars specifies the environment variables that should export
14-
ExportVars map[string]string
15-
}
16-
1711
// EnvVar captures environment variables that are exposed through the
1812
// middleware response.
1913
type EnvVar struct {
@@ -27,10 +21,7 @@ func (envVar *EnvVar) set(key, val string) {
2721
// New creates a handler that returns configured environment variables as a
2822
// JSON response.
2923
func New(config ...Config) fiber.Handler {
30-
var cfg Config
31-
if len(config) > 0 {
32-
cfg = config[0]
33-
}
24+
cfg := configDefault(config...)
3425

3526
return func(c fiber.Ctx) error {
3627
method := c.Method()

middleware/favicon/config.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package favicon
2+
3+
import (
4+
"io/fs"
5+
6+
"github.com/gofiber/fiber/v3"
7+
)
8+
9+
// Config defines the config for middleware.
10+
type Config struct {
11+
// FileSystem is an optional alternate filesystem to search for the favicon in.
12+
// An example of this could be an embedded or network filesystem
13+
//
14+
// Optional. Default: nil
15+
FileSystem fs.FS `json:"-"`
16+
17+
// Next defines a function to skip this middleware when returned true.
18+
//
19+
// Optional. Default: nil
20+
Next func(c fiber.Ctx) bool
21+
22+
// File holds the path to an actual favicon that will be cached
23+
//
24+
// Optional. Default: ""
25+
File string `json:"file"`
26+
27+
// URL for favicon handler
28+
//
29+
// Optional. Default: "/favicon.ico"
30+
URL string `json:"url"`
31+
32+
// CacheControl defines how the Cache-Control header in the response should be set
33+
//
34+
// Optional. Default: "public, max-age=31536000"
35+
CacheControl string `json:"cache_control"`
36+
37+
// Raw data of the favicon file
38+
//
39+
// Optional. Default: nil
40+
Data []byte `json:"-"`
41+
}
42+
43+
// ConfigDefault is the default config
44+
var ConfigDefault = Config{
45+
Next: nil,
46+
File: "",
47+
URL: fPath,
48+
CacheControl: "public, max-age=31536000",
49+
}
50+
51+
func configDefault(config ...Config) Config {
52+
if len(config) == 0 {
53+
return ConfigDefault
54+
}
55+
56+
cfg := config[0]
57+
58+
if cfg.Next == nil {
59+
cfg.Next = ConfigDefault.Next
60+
}
61+
if cfg.URL == "" {
62+
cfg.URL = ConfigDefault.URL
63+
}
64+
if cfg.File == "" {
65+
cfg.File = ConfigDefault.File
66+
}
67+
if cfg.CacheControl == "" {
68+
cfg.CacheControl = ConfigDefault.CacheControl
69+
}
70+
71+
return cfg
72+
}

middleware/favicon/favicon.go

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,6 @@ import (
99
"github.com/gofiber/fiber/v3"
1010
)
1111

12-
// Config defines the config for middleware.
13-
type Config struct {
14-
// FileSystem is an optional alternate filesystem to search for the favicon in.
15-
// An example of this could be an embedded or network filesystem
16-
//
17-
// Optional. Default: nil
18-
FileSystem fs.FS `json:"-"`
19-
20-
// Next defines a function to skip this middleware when returned true.
21-
//
22-
// Optional. Default: nil
23-
Next func(c fiber.Ctx) bool
24-
25-
// File holds the path to an actual favicon that will be cached
26-
//
27-
// Optional. Default: ""
28-
File string `json:"file"`
29-
30-
// URL for favicon handler
31-
//
32-
// Optional. Default: "/favicon.ico"
33-
URL string `json:"url"`
34-
35-
// CacheControl defines how the Cache-Control header in the response should be set
36-
//
37-
// Optional. Default: "public, max-age=31536000"
38-
CacheControl string `json:"cache_control"`
39-
40-
// Raw data of the favicon file
41-
//
42-
// Optional. Default: nil
43-
Data []byte `json:"-"`
44-
}
45-
46-
// ConfigDefault is the default config
47-
var ConfigDefault = Config{
48-
Next: nil,
49-
File: "",
50-
URL: fPath,
51-
CacheControl: "public, max-age=31536000",
52-
}
53-
5412
const (
5513
fPath = "/favicon.ico"
5614
hType = "image/x-icon"
@@ -60,27 +18,7 @@ const (
6018

6119
// New creates a new middleware handler
6220
func New(config ...Config) fiber.Handler {
63-
// Set default config
64-
cfg := ConfigDefault
65-
66-
// Override config if provided
67-
if len(config) > 0 {
68-
cfg = config[0]
69-
70-
// Set default values
71-
if cfg.Next == nil {
72-
cfg.Next = ConfigDefault.Next
73-
}
74-
if cfg.URL == "" {
75-
cfg.URL = ConfigDefault.URL
76-
}
77-
if cfg.File == "" {
78-
cfg.File = ConfigDefault.File
79-
}
80-
if cfg.CacheControl == "" {
81-
cfg.CacheControl = ConfigDefault.CacheControl
82-
}
83-
}
21+
cfg := configDefault(config...)
8422

8523
// Load iconData if provided
8624
var (

0 commit comments

Comments
 (0)