diff options
author | Wolfy-J <[email protected]> | 2018-09-07 23:26:26 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-09-07 23:26:26 +0300 |
commit | cac8bf48bab69ff3468cf7d30ac1c18904885a47 (patch) | |
tree | 5ffec205a9b51a933339d535de198a267be0275a /service | |
parent | ea97c188a4a74c00b585cb50fa1ed4db7d190e09 (diff) |
added ability to alter env values
Diffstat (limited to 'service')
-rw-r--r-- | service/env/provider.go | 7 | ||||
-rw-r--r-- | service/env/service.go | 7 | ||||
-rw-r--r-- | service/env/service_test.go | 18 | ||||
-rw-r--r-- | service/http/config.go | 6 | ||||
-rw-r--r-- | service/http/service.go | 4 | ||||
-rw-r--r-- | service/rpc/service.go | 16 |
6 files changed, 49 insertions, 9 deletions
diff --git a/service/env/provider.go b/service/env/provider.go index 2918f18c..d095009c 100644 --- a/service/env/provider.go +++ b/service/env/provider.go @@ -2,7 +2,10 @@ package env // Provider aggregates list of environment variables. This interface can be used in custom implementation to drive // values from external sources. -type Provider interface { +type Environment interface { // GetEnv must return list of env variables. GetEnv() (map[string]string, error) -} + + // SetEnv sets or creates environment value. + SetEnv(key, value string) +}
\ No newline at end of file diff --git a/service/env/service.go b/service/env/service.go index 0822d55a..b32a2f2b 100644 --- a/service/env/service.go +++ b/service/env/service.go @@ -15,7 +15,7 @@ func NewService(defaults map[string]string) *Service { if s.values == nil { s.values = make(map[string]string) } - + return s } @@ -33,3 +33,8 @@ func (s *Service) Init(cfg *Config) (bool, error) { func (s *Service) GetEnv() (map[string]string, error) { return s.values, nil } + +// SetEnv sets or creates environment value. +func (s *Service) SetEnv(key, value string) { + s.values[key] = value +} diff --git a/service/env/service_test.go b/service/env/service_test.go index 69a2a375..10159857 100644 --- a/service/env/service_test.go +++ b/service/env/service_test.go @@ -22,3 +22,21 @@ func Test_Extend(t *testing.T) { assert.Equal(t, "version", values["rr"]) assert.Equal(t, "value", values["key"]) } + + +func Test_Set(t *testing.T) { + s := NewService(map[string]string{"rr": "version"}) + + s.Init(&Config{Values: map[string]string{"key": "value"}}) + assert.Len(t, s.values, 2) + + s.SetEnv("key","value-new") + s.SetEnv("other","new") + + values, err := s.GetEnv() + assert.NoError(t, err) + assert.Len(t, values, 3) + assert.Equal(t, "version", values["rr"]) + assert.Equal(t, "value-new", values["key"]) + assert.Equal(t, "new", values["other"]) +}
\ No newline at end of file diff --git a/service/http/config.go b/service/http/config.go index 20a247fb..3e08c72d 100644 --- a/service/http/config.go +++ b/service/http/config.go @@ -10,7 +10,7 @@ import ( // Config configures RoadRunner HTTP server. type Config struct { - // Enable enables http svc. + // Enable enables http service. Enable bool // Address and port to handle as http server. @@ -32,6 +32,10 @@ func (c *Config) Hydrate(cfg service.Config) error { return err } + if !c.Enable { + return nil + } + if err := c.Valid(); err != nil { return err } diff --git a/service/http/service.go b/service/http/service.go index 9f62f5af..f988a843 100644 --- a/service/http/service.go +++ b/service/http/service.go @@ -20,7 +20,7 @@ type middleware func(f http.HandlerFunc) http.HandlerFunc // Service manages rr, http servers. type Service struct { cfg *Config - env env.Provider + env env.Environment lsns []func(event int, ctx interface{}) mdws []middleware mu sync.Mutex @@ -42,7 +42,7 @@ func (s *Service) AddListener(l func(event int, ctx interface{})) { // 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, r *rpc.Service, e env.Provider) (bool, error) { +func (s *Service) Init(cfg *Config, r *rpc.Service, e env.Environment) (bool, error) { if !cfg.Enable { return false, nil } diff --git a/service/rpc/service.go b/service/rpc/service.go index 6e231048..abeae240 100644 --- a/service/rpc/service.go +++ b/service/rpc/service.go @@ -5,11 +5,17 @@ import ( "github.com/spiral/goridge" "net/rpc" "sync" + "github.com/spiral/roadrunner/service/env" ) -// ID contains default service name. -const ID = "rpc" +const ( + // ID contains default service name. + ID = "rpc" + // ENV_KEY defines environment key to be used to store information about + // rpc server connection. + ENV_KEY = "RR_RPC" +) // Service is RPC service. type Service struct { cfg *Config @@ -20,7 +26,7 @@ type Service struct { } // Init rpc service. Must return true if service is enabled. -func (s *Service) Init(cfg *Config) (bool, error) { +func (s *Service) Init(cfg *Config, env env.Environment) (bool, error) { if !cfg.Enable { return false, nil } @@ -28,6 +34,10 @@ func (s *Service) Init(cfg *Config) (bool, error) { s.cfg = cfg s.rpc = rpc.NewServer() + if env != nil { + env.SetEnv(ENV_KEY, cfg.Listen) + } + return true, nil } |