diff options
author | Wolfy-J <[email protected]> | 2018-07-08 13:06:05 -0700 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-07-08 13:06:05 -0700 |
commit | 29c9bf94350e86ec96f5ce5eeb476dfcd57302cd (patch) | |
tree | 9f59af6446958d144b7de91b5005a3727dc90661 /service/http | |
parent | 3c3a7801100f29c99a5e446646c818bf16ccd5f0 (diff) |
dependency injection and lighter service Init methods.
Diffstat (limited to 'service/http')
-rw-r--r-- | service/http/config.go | 22 | ||||
-rw-r--r-- | service/http/service.go | 25 | ||||
-rw-r--r-- | service/http/service_test.go | 14 |
3 files changed, 28 insertions, 33 deletions
diff --git a/service/http/config.go b/service/http/config.go index 19a2e71d..e46b56cf 100644 --- a/service/http/config.go +++ b/service/http/config.go @@ -4,6 +4,7 @@ import ( "errors" "github.com/spiral/roadrunner" "strings" + "github.com/spiral/roadrunner/service" ) // Config configures RoadRunner HTTP server. @@ -24,25 +25,34 @@ type Config struct { Workers *roadrunner.ServerConfig } +// 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 { + if err := cfg.Unmarshal(c); err != nil { + return err + } + + return c.Valid() +} + // Valid validates the configuration. -func (cfg *Config) Valid() error { - if cfg.Uploads == nil { +func (c *Config) Valid() error { + if c.Uploads == nil { return errors.New("mailformed uploads config") } - if cfg.Workers == nil { + if c.Workers == nil { return errors.New("mailformed workers config") } - if cfg.Workers.Pool == nil { + if c.Workers.Pool == nil { return errors.New("mailformed workers config (pool config is missing)") } - if err := cfg.Workers.Pool.Valid(); err != nil { + if err := c.Workers.Pool.Valid(); err != nil { return err } - if !strings.Contains(cfg.Address, ":") { + if !strings.Contains(c.Address, ":") { return errors.New("mailformed server address") } diff --git a/service/http/service.go b/service/http/service.go index 7405bf37..30289e3c 100644 --- a/service/http/service.go +++ b/service/http/service.go @@ -3,7 +3,6 @@ package http import ( "context" "github.com/spiral/roadrunner" - "github.com/spiral/roadrunner/service" "github.com/spiral/roadrunner/service/rpc" "net/http" "sync" @@ -42,28 +41,14 @@ 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 service.Config, c service.Container) (bool, error) { - config := &Config{} - - if err := cfg.Unmarshal(config); err != nil { - return false, err - } - - if !config.Enable { +func (s *Service) Init(cfg *Config, r *rpc.Service) (bool, error) { + if !cfg.Enable { return false, nil } - if err := config.Valid(); err != nil { - return false, err - } - - s.cfg = config - - // registering http RPC interface - if r, ok := c.Get(rpc.ID); ok >= service.StatusConfigured { - if h, ok := r.(*rpc.Service); ok { - h.Register(ID, &rpcServer{s}) - } + s.cfg = cfg + if r != nil { + r.Register(ID, &rpcServer{s}) } return true, nil diff --git a/service/http/service_test.go b/service/http/service_test.go index 50836b4b..b442ae51 100644 --- a/service/http/service_test.go +++ b/service/http/service_test.go @@ -42,7 +42,7 @@ func Test_Service_NoConfig(t *testing.T) { c := service.NewContainer(logger) c.Register(ID, &Service{}) - assert.NoError(t, c.Init(&testCfg{httpCfg: `{}`})) + assert.Error(t, c.Init(&testCfg{httpCfg: `{}`})) s, st := c.Get(ID) assert.NotNil(t, s) @@ -108,7 +108,7 @@ func Test_Service_Configure_Enable(t *testing.T) { s, st := c.Get(ID) assert.NotNil(t, s) - assert.Equal(t, service.StatusConfigured, st) + assert.Equal(t, service.StatusOK, st) } func Test_Service_Echo(t *testing.T) { @@ -139,10 +139,10 @@ func Test_Service_Echo(t *testing.T) { s, st := c.Get(ID) assert.NotNil(t, s) - assert.Equal(t, service.StatusConfigured, st) + assert.Equal(t, service.StatusOK, st) // should do nothing - s.Stop() + s.(*Service).Stop() go func() { c.Serve() }() time.Sleep(time.Millisecond * 100) @@ -191,7 +191,7 @@ func Test_Service_ErrorEcho(t *testing.T) { s, st := c.Get(ID) assert.NotNil(t, s) - assert.Equal(t, service.StatusConfigured, st) + assert.Equal(t, service.StatusOK, st) goterr := make(chan interface{}) s.(*Service).AddListener(func(event int, ctx interface{}) { @@ -251,7 +251,7 @@ func Test_Service_Middleware(t *testing.T) { s, st := c.Get(ID) assert.NotNil(t, s) - assert.Equal(t, service.StatusConfigured, st) + assert.Equal(t, service.StatusOK, st) s.(*Service).AddMiddleware(func(f http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { @@ -325,7 +325,7 @@ func Test_Service_Listener(t *testing.T) { s, st := c.Get(ID) assert.NotNil(t, s) - assert.Equal(t, service.StatusConfigured, st) + assert.Equal(t, service.StatusOK, st) stop := make(chan interface{}) s.(*Service).AddListener(func(event int, ctx interface{}) { |