Skip to content

Commit 3930f34

Browse files
committed
refactor(config)!: remove InitDefaults methods
1 parent a69ed6d commit 3930f34

File tree

3 files changed

+11
-25
lines changed

3 files changed

+11
-25
lines changed

config.go

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ type MiddlewaresConfig struct {
2929
Logger RequestLoggerConfig `json:"logger,omitempty" yaml:"logger,omitempty"`
3030
}
3131

32-
func (cfg *MiddlewaresConfig) InitDefaults() {
33-
cfg.BodyLimit.InitDefaults()
34-
cfg.Compress.InitDefaults()
35-
cfg.Secure.InitDefaults()
36-
}
37-
3832
type APIConfig struct {
3933
Enabled bool `json:"enabled,omitempty" yaml:"enabled,omitempty"`
4034
Path string `json:"path,omitempty" yaml:"path,omitempty"`
@@ -46,7 +40,7 @@ type APIConfig struct {
4640
Components huma.Components `json:"components,omitempty" yaml:"components,omitempty"`
4741
}
4842

49-
func (cfg *APIConfig) InitDefaults() {
43+
func (cfg *APIConfig) setDefaults() {
5044
if cfg.OpenAPIPath == "" {
5145
cfg.OpenAPIPath = "/openapi"
5246
}
@@ -63,13 +57,6 @@ type AreaConfig struct {
6357
API map[string]APIConfig `json:"api,omitempty" yaml:"api,omitempty"`
6458
}
6559

66-
func (cfg *AreaConfig) InitDefaults() {
67-
for key, value := range cfg.API {
68-
value.InitDefaults()
69-
cfg.API[key] = value
70-
}
71-
}
72-
7360
type RoleHierarchyConfig struct {
7461
Role string `json:"role,omitempty" yaml:"role,omitempty"`
7562
Parents []string `json:"parents,omitempty" yaml:"parents,omitempty"`
@@ -82,14 +69,6 @@ type Config struct {
8269
Areas map[string]AreaConfig `json:"areas,omitempty" yaml:"areas,omitempty"`
8370
}
8471

85-
func (cfg *Config) InitDefaults() {
86-
cfg.Middlewares.InitDefaults()
87-
for key, value := range cfg.Areas {
88-
value.InitDefaults()
89-
cfg.Areas[key] = value
90-
}
91-
}
92-
9372
type SameSiteType string
9473

9574
const (
@@ -153,7 +132,7 @@ type BodyLimitConfig struct {
153132
Limit string `json:"limit,omitempty" yaml:"limit,omitempty"`
154133
}
155134

156-
func (cfg *BodyLimitConfig) InitDefaults() {
135+
func (cfg *BodyLimitConfig) setDefaults() {
157136
if cfg.Limit == "" {
158137
cfg.Limit = "4KB"
159138
}
@@ -181,7 +160,7 @@ type GzipConfig struct {
181160
MinLength int `json:"minLength,omitempty" yaml:"minLength,omitempty"`
182161
}
183162

184-
func (cfg *GzipConfig) InitDefaults() {
163+
func (cfg *GzipConfig) setDefaults() {
185164
if cfg.MinLength <= 0 {
186165
cfg.MinLength = 1024
187166
}
@@ -252,7 +231,7 @@ type SecureConfig struct {
252231
ReferrerPolicy string `json:"referrerPolicy,omitempty" yaml:"referrerPolicy,omitempty"`
253232
}
254233

255-
func (cfg *SecureConfig) InitDefaults() {
234+
func (cfg *SecureConfig) setDefaults() {
256235
if cfg.XSSProtection == "" {
257236
cfg.XSSProtection = middleware.DefaultSecureConfig.XSSProtection
258237
}

echo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ func NewEcho(params EchoParams) *echo.Echo {
114114
if !cfgAPI.Enabled {
115115
continue
116116
}
117+
cfgAPI.setDefaults()
117118

118119
humaConfig := huma.DefaultConfig("", "")
119120
humaConfig.Servers = []*huma.Server{{URL: cfg.Path + cfgAPI.Path}}

middleware.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,17 @@ func RecoverMiddleware(cfg RecoverConfig, logger *zap.Logger) Middleware {
7878
}
7979

8080
func BodyLimitMiddleware(cfg BodyLimitConfig) Middleware {
81+
cfg.setDefaults()
82+
8183
return NewMiddleware("body-limit", middleware.BodyLimitWithConfig(middleware.BodyLimitConfig{
8284
Skipper: cfg.Skipper,
8385
Limit: cfg.Limit,
8486
}))
8587
}
8688

8789
func CompressMiddleware(cfg GzipConfig) Middleware {
90+
cfg.setDefaults()
91+
8892
return NewMiddleware("compress", middleware.GzipWithConfig(middleware.GzipConfig{
8993
Skipper: cfg.Skipper,
9094
Level: cfg.Level,
@@ -162,6 +166,8 @@ func LoggerMiddleware(cfg RequestLoggerConfig, logger *zap.Logger) Middleware {
162166
}
163167

164168
func SecureMiddleware(cfg SecureConfig) Middleware {
169+
cfg.setDefaults()
170+
165171
return NewMiddleware("secure", middleware.SecureWithConfig(middleware.SecureConfig{
166172
Skipper: cfg.Skipper,
167173
XSSProtection: cfg.XSSProtection,

0 commit comments

Comments
 (0)