From 8226b1f064ba1dca8f817bb882e0e570fc057e43 Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Thu, 26 Jul 2018 17:05:54 +0300 Subject: default service value as version --- cmd/rr/main.go | 2 +- server_config.go | 2 +- service/env/config.go | 3 +-- service/env/config_test.go | 29 +++++++++++++++++++++++++++++ service/env/service.go | 13 ++++++++++++- 5 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 service/env/config_test.go diff --git a/cmd/rr/main.go b/cmd/rr/main.go index 2abb6321..e95139a3 100644 --- a/cmd/rr/main.go +++ b/cmd/rr/main.go @@ -39,7 +39,7 @@ import ( func main() { rr.Logger.Formatter = &logrus.TextFormatter{ForceColors: true} - rr.Container.Register(env.ID, &env.Service{}) + rr.Container.Register(env.ID, env.NewService(rr.Version)) rr.Container.Register(rpc.ID, &rpc.Service{}) rr.Container.Register(http.ID, &http.Service{}) rr.Container.Register(static.ID, &static.Service{}) diff --git a/server_config.go b/server_config.go index b0680852..50b19d5a 100644 --- a/server_config.go +++ b/server_config.go @@ -29,7 +29,7 @@ type ServerConfig struct { // while server is running. Pool *Config - // Env defines set of values to be passed to the command context. + // Default defines set of values to be passed to the command context. env []string } diff --git a/service/env/config.go b/service/env/config.go index 06342f98..1b743e84 100644 --- a/service/env/config.go +++ b/service/env/config.go @@ -12,6 +12,5 @@ type Config struct { // Hydrate must populate Config values using given Config source. Must return error if Config is not valid. func (c *Config) Hydrate(cfg service.Config) error { - c.Values = map[string]string{"RR": "YES"} return cfg.Unmarshal(&c.Values) -} +} \ No newline at end of file diff --git a/service/env/config_test.go b/service/env/config_test.go new file mode 100644 index 00000000..3c943963 --- /dev/null +++ b/service/env/config_test.go @@ -0,0 +1,29 @@ +package env + +import ( + "github.com/spiral/roadrunner/service" + "encoding/json" + "testing" + "github.com/stretchr/testify/assert" +) + +type mockCfg struct{ cfg string } + +func (cfg *mockCfg) Get(name string) service.Config { return nil } +func (cfg *mockCfg) Unmarshal(out interface{}) error { return json.Unmarshal([]byte(cfg.cfg), out) } + +func Test_Config_Hydrate(t *testing.T) { + cfg := &mockCfg{`{"key":"value"}`} + c := &Config{} + + assert.NoError(t, c.Hydrate(cfg)) + assert.Len(t, c.Values, 1) +} + +func Test_Config_Hydrate_Empty(t *testing.T) { + cfg := &mockCfg{`{}`} + c := &Config{} + + assert.NoError(t, c.Hydrate(cfg)) + assert.Len(t, c.Values, 0) +} \ No newline at end of file diff --git a/service/env/service.go b/service/env/service.go index a90b0c48..b0721971 100644 --- a/service/env/service.go +++ b/service/env/service.go @@ -5,13 +5,24 @@ const ID = "env" // Service provides ability to map _ENV values from config file. type Service struct { - cfg *Config + // Default is default set of values. + Default map[string]string + cfg *Config +} + +// NewService creates new env service instance for given rr version. +func NewService(version string) *Service { + return &Service{Default: map[string]string{"rr": version}} } // Init must return configure svc and return true if svc hasStatus enabled. Must return error in case of // misconfiguration. Services must not be used without proper configuration pushed first. func (s *Service) Init(cfg *Config) (bool, error) { s.cfg = cfg + for k, v := range s.Default { + s.cfg.Values[k] = v + } + return true, nil } -- cgit v1.2.3