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/rpc/config.go | |
parent | 3c3a7801100f29c99a5e446646c818bf16ccd5f0 (diff) |
dependency injection and lighter service Init methods.
Diffstat (limited to 'service/rpc/config.go')
-rw-r--r-- | service/rpc/config.go | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/service/rpc/config.go b/service/rpc/config.go index e3168945..0485fdf6 100644 --- a/service/rpc/config.go +++ b/service/rpc/config.go @@ -5,9 +5,11 @@ import ( "net" "strings" "syscall" + "github.com/spiral/roadrunner/service" ) -type config struct { +// Config defines RPC service config. +type Config struct { // Indicates if RPC connection is enabled. Enable bool @@ -15,9 +17,31 @@ type config struct { Listen string } -// listener creates new rpc socket listener. -func (cfg *config) listener() (net.Listener, error) { - dsn := strings.Split(cfg.Listen, "://") +// 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 returns nil if config is valid. +func (c *Config) Valid() error { + if dsn := strings.Split(c.Listen, "://"); len(dsn) != 2 { + return errors.New("invalid socket DSN (tcp://:6001, unix://rpc.sock)") + } + + if dsn := strings.Split(c.Listen, "://"); len(dsn) != 2 { + return errors.New("invalid socket DSN (tcp://:6001, unix://rpc.sock)") + } + + return nil +} + +// Listener creates new rpc socket Listener. +func (c *Config) Listener() (net.Listener, error) { + dsn := strings.Split(c.Listen, "://") if len(dsn) != 2 { return nil, errors.New("invalid socket DSN (tcp://:6001, unix://rpc.sock)") } @@ -29,9 +53,9 @@ func (cfg *config) listener() (net.Listener, error) { return net.Listen(dsn[0], dsn[1]) } -// dialer creates rpc socket dialer. -func (cfg *config) dialer() (net.Conn, error) { - dsn := strings.Split(cfg.Listen, "://") +// Dialer creates rpc socket Dialer. +func (c *Config) Dialer() (net.Conn, error) { + dsn := strings.Split(c.Listen, "://") if len(dsn) != 2 { return nil, errors.New("invalid socket DSN (tcp://:6001, unix://rpc.sock)") } |