Skip to content

Commit

Permalink
refactor(config)!: remove InitDefaults methods
Browse files Browse the repository at this point in the history
  • Loading branch information
iagapie committed Nov 7, 2024
1 parent a69ed6d commit 3930f34
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 25 deletions.
29 changes: 4 additions & 25 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ type MiddlewaresConfig struct {
Logger RequestLoggerConfig `json:"logger,omitempty" yaml:"logger,omitempty"`
}

func (cfg *MiddlewaresConfig) InitDefaults() {
cfg.BodyLimit.InitDefaults()
cfg.Compress.InitDefaults()
cfg.Secure.InitDefaults()
}

type APIConfig struct {
Enabled bool `json:"enabled,omitempty" yaml:"enabled,omitempty"`
Path string `json:"path,omitempty" yaml:"path,omitempty"`
Expand All @@ -46,7 +40,7 @@ type APIConfig struct {
Components huma.Components `json:"components,omitempty" yaml:"components,omitempty"`
}

func (cfg *APIConfig) InitDefaults() {
func (cfg *APIConfig) setDefaults() {
if cfg.OpenAPIPath == "" {
cfg.OpenAPIPath = "/openapi"
}
Expand All @@ -63,13 +57,6 @@ type AreaConfig struct {
API map[string]APIConfig `json:"api,omitempty" yaml:"api,omitempty"`
}

func (cfg *AreaConfig) InitDefaults() {
for key, value := range cfg.API {
value.InitDefaults()
cfg.API[key] = value
}
}

type RoleHierarchyConfig struct {
Role string `json:"role,omitempty" yaml:"role,omitempty"`
Parents []string `json:"parents,omitempty" yaml:"parents,omitempty"`
Expand All @@ -82,14 +69,6 @@ type Config struct {
Areas map[string]AreaConfig `json:"areas,omitempty" yaml:"areas,omitempty"`
}

func (cfg *Config) InitDefaults() {
cfg.Middlewares.InitDefaults()
for key, value := range cfg.Areas {
value.InitDefaults()
cfg.Areas[key] = value
}
}

type SameSiteType string

const (
Expand Down Expand Up @@ -153,7 +132,7 @@ type BodyLimitConfig struct {
Limit string `json:"limit,omitempty" yaml:"limit,omitempty"`
}

func (cfg *BodyLimitConfig) InitDefaults() {
func (cfg *BodyLimitConfig) setDefaults() {
if cfg.Limit == "" {
cfg.Limit = "4KB"
}
Expand Down Expand Up @@ -181,7 +160,7 @@ type GzipConfig struct {
MinLength int `json:"minLength,omitempty" yaml:"minLength,omitempty"`
}

func (cfg *GzipConfig) InitDefaults() {
func (cfg *GzipConfig) setDefaults() {
if cfg.MinLength <= 0 {
cfg.MinLength = 1024
}
Expand Down Expand Up @@ -252,7 +231,7 @@ type SecureConfig struct {
ReferrerPolicy string `json:"referrerPolicy,omitempty" yaml:"referrerPolicy,omitempty"`
}

func (cfg *SecureConfig) InitDefaults() {
func (cfg *SecureConfig) setDefaults() {
if cfg.XSSProtection == "" {
cfg.XSSProtection = middleware.DefaultSecureConfig.XSSProtection
}
Expand Down
1 change: 1 addition & 0 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func NewEcho(params EchoParams) *echo.Echo {
if !cfgAPI.Enabled {
continue
}
cfgAPI.setDefaults()

humaConfig := huma.DefaultConfig("", "")
humaConfig.Servers = []*huma.Server{{URL: cfg.Path + cfgAPI.Path}}
Expand Down
6 changes: 6 additions & 0 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,17 @@ func RecoverMiddleware(cfg RecoverConfig, logger *zap.Logger) Middleware {
}

func BodyLimitMiddleware(cfg BodyLimitConfig) Middleware {
cfg.setDefaults()

return NewMiddleware("body-limit", middleware.BodyLimitWithConfig(middleware.BodyLimitConfig{
Skipper: cfg.Skipper,
Limit: cfg.Limit,
}))
}

func CompressMiddleware(cfg GzipConfig) Middleware {
cfg.setDefaults()

return NewMiddleware("compress", middleware.GzipWithConfig(middleware.GzipConfig{
Skipper: cfg.Skipper,
Level: cfg.Level,
Expand Down Expand Up @@ -162,6 +166,8 @@ func LoggerMiddleware(cfg RequestLoggerConfig, logger *zap.Logger) Middleware {
}

func SecureMiddleware(cfg SecureConfig) Middleware {
cfg.setDefaults()

return NewMiddleware("secure", middleware.SecureWithConfig(middleware.SecureConfig{
Skipper: cfg.Skipper,
XSSProtection: cfg.XSSProtection,
Expand Down

0 comments on commit 3930f34

Please sign in to comment.