diff options
-rw-r--r-- | config.go | 13 | ||||
-rw-r--r-- | server_config.go | 12 | ||||
-rw-r--r-- | server_config_test.go | 2 | ||||
-rw-r--r-- | service/container.go | 17 | ||||
-rw-r--r-- | service/env/config.go | 6 | ||||
-rw-r--r-- | service/http/config.go | 2 | ||||
-rw-r--r-- | service/http/service.go | 7 | ||||
-rw-r--r-- | service/rpc/config.go | 8 |
8 files changed, 49 insertions, 18 deletions
@@ -25,6 +25,19 @@ type Config struct { DestroyTimeout time.Duration } +// InitDefaults allows to init blank config with pre-defined set of default values. +func (cfg *Config) InitDefaults() error { + if cfg.AllocateTimeout == 0 { + cfg.AllocateTimeout = time.Minute + } + + if cfg.DestroyTimeout == 0 { + cfg.DestroyTimeout = time.Minute + } + + return nil +} + // Valid returns error if config not valid. func (cfg *Config) Valid() error { if cfg.NumWorkers == 0 { diff --git a/server_config.go b/server_config.go index e2f9266b..b2d8845b 100644 --- a/server_config.go +++ b/server_config.go @@ -33,8 +33,8 @@ type ServerConfig struct { env []string } -// SetDefaults sets missing values to their default values. -func (cfg *ServerConfig) SetDefaults() { +// InitDefaults sets missing values to their default values. +func (cfg *ServerConfig) InitDefaults() error { if cfg.Relay == "" { cfg.Relay = "pipes" } @@ -47,13 +47,7 @@ func (cfg *ServerConfig) SetDefaults() { cfg.Pool = &Config{} } - if cfg.Pool.AllocateTimeout == 0 { - cfg.Pool.AllocateTimeout = time.Minute - } - - if cfg.Pool.DestroyTimeout == 0 { - cfg.Pool.DestroyTimeout = time.Minute - } + return cfg.Pool.InitDefaults() } // UpscaleDurations converts duration values from nanoseconds to seconds. diff --git a/server_config_test.go b/server_config_test.go index 5e874649..ec29412e 100644 --- a/server_config_test.go +++ b/server_config_test.go @@ -112,7 +112,7 @@ func Test_ServerConfigDefaults(t *testing.T) { Command: "php tests/client.php pipes", } - cfg.SetDefaults() + cfg.InitDefaults() assert.Equal(t, "pipes", cfg.Relay) assert.Equal(t, time.Minute, cfg.Pool.AllocateTimeout) diff --git a/service/container.go b/service/container.go index 861e1aac..fc1012c8 100644 --- a/service/container.go +++ b/service/container.go @@ -66,6 +66,11 @@ type HydrateConfig interface { Hydrate(cfg Config) error } +type DefaultsConfig interface { + // InitDefaults allows to init blank config with pre-defined set of default values. + InitDefaults() error +} + type container struct { log logrus.FieldLogger mu sync.Mutex @@ -257,11 +262,19 @@ func (c *container) resolveValues(s interface{}, m reflect.Method, cfg Config) ( values = append(values, reflect.ValueOf(c.log)) case v.Implements(reflect.TypeOf((*HydrateConfig)(nil)).Elem()): // injectable config - if cfg == nil { + sc := reflect.New(v.Elem()) + + if dsc, ok := sc.Interface().(DefaultsConfig); ok { + dsc.InitDefaults() + if cfg == nil { + values = append(values, sc) + continue + } + + } else if cfg == nil { return nil, errNoConfig } - sc := reflect.New(v.Elem()) if err := sc.Interface().(HydrateConfig).Hydrate(cfg); err != nil { return nil, err } diff --git a/service/env/config.go b/service/env/config.go index d0ba686b..a7da695e 100644 --- a/service/env/config.go +++ b/service/env/config.go @@ -14,3 +14,9 @@ type Config struct { func (c *Config) Hydrate(cfg service.Config) error { return cfg.Unmarshal(&c.Values) } + +// InitDefaults allows to init blank config with pre-defined set of default values. +func (c *Config) InitDefaults() error { + c.Values = make(map[string]string) + return nil +} diff --git a/service/http/config.go b/service/http/config.go index 3e1071ac..321072c5 100644 --- a/service/http/config.go +++ b/service/http/config.go @@ -36,7 +36,7 @@ func (c *Config) Hydrate(cfg service.Config) error { } if c.Workers != nil { - c.Workers.SetDefaults() + c.Workers.InitDefaults() } if err := c.Valid(); err != nil { diff --git a/service/http/service.go b/service/http/service.go index 94524897..6bf6e33f 100644 --- a/service/http/service.go +++ b/service/http/service.go @@ -87,11 +87,8 @@ func (s *Service) Serve() error { s.rr.Listen(s.listener) s.srv.Listen(s.listener) - if len(s.middleware) == 0 { - s.http.Handler = s.srv - } else { - s.http.Handler = s - } + s.http.Handler = s + s.mu.Unlock() if err := rr.Start(); err != nil { diff --git a/service/rpc/config.go b/service/rpc/config.go index 93fce7ca..653da6ea 100644 --- a/service/rpc/config.go +++ b/service/rpc/config.go @@ -26,6 +26,14 @@ func (c *Config) Hydrate(cfg service.Config) error { return c.Valid() } +// InitDefaults allows to init blank config with pre-defined set of default values. +func (c *Config) InitDefaults() error { + c.Enable = true + c.Listen = "tcp://127.0.0.1:6001" + + return nil +} + // Valid returns nil if config is valid. func (c *Config) Valid() error { if dsn := strings.Split(c.Listen, "://"); len(dsn) != 2 { |