From aa9ab1511b158ea4c8cbb0a2e5f87268d04e4fef Mon Sep 17 00:00:00 2001 From: godcong Date: Tue, 24 Dec 2024 19:11:08 +0800 Subject: [PATCH] feat(runtime): add service configuration and discovery support - Add support for service-specific configuration in runtime.Config - Implement registry and service configuration binding - Update runtime.Runtime to use new service configuration methods - Refactor config.Scan to use the new cfg field --- config/config.go | 31 ++++++++++++++++++++++-- gen/go/config/v1/data.pb.validate.go | 2 +- runtime.go | 35 ++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/config/config.go b/config/config.go index b248f86..5286186 100644 --- a/config/config.go +++ b/config/config.go @@ -66,11 +66,14 @@ func (v *EnvVars) Get(key string) (string, bool) { } type Config struct { + cfg any envVars EnvVars source KConfig Path string EnvPrefixes []string Builder Builder + registry func(source any, serviceName string) (*configv1.Registry, error) + service func(source any, serviceName string) (*configv1.Service, error) } func (c *Config) LoadFromFile(path string, opts ...KOption) error { @@ -90,6 +93,7 @@ func (c *Config) LoadFromSource(cfg *configv1.SourceConfig, opts ...OptionSettin if c.source != nil { return nil } + config, err := c.Builder.NewConfig(cfg, opts...) if err != nil { return err @@ -98,8 +102,13 @@ func (c *Config) LoadFromSource(cfg *configv1.SourceConfig, opts ...OptionSettin return c.source.Load() } -func (c *Config) Scan(v any) error { - return c.source.Scan(v) +func (c *Config) Scan() error { + return c.source.Scan(c.cfg) +} + +func (c *Config) Bind(cfg any) error { + c.cfg = cfg + return c.Scan() } func (c *Config) Watch(key string, ob KObserver) error { @@ -121,6 +130,24 @@ func (c *Config) Setup(prefix string) error { return c.envVars.Setup(prefix) } +func (c *Config) BindRegistry(fn func(source any, serviceName string) (*configv1.Registry, error)) { + c.registry = fn +} + +func (c *Config) Registry(serviceName string) (*configv1.Registry, error) { + if c.registry != nil { + return c.registry(c.cfg, serviceName) + } + return nil, ErrNotFound +} + +func (c *Config) Service(serviceName string) (*configv1.Service, error) { + if c.service != nil { + return c.service(c.cfg, serviceName) + } + return nil, ErrNotFound +} + func NewBuilder() Builder { return &builder{ factories: make(map[string]Factory), diff --git a/gen/go/config/v1/data.pb.validate.go b/gen/go/config/v1/data.pb.validate.go index 75732fa..7d03f3a 100644 --- a/gen/go/config/v1/data.pb.validate.go +++ b/gen/go/config/v1/data.pb.validate.go @@ -255,7 +255,7 @@ func (m *Data_Database) validate(all bool) error { errors = append(errors, err) } - // no validation rules for Source + // no validation rules for Cfg // no validation rules for Migrate diff --git a/runtime.go b/runtime.go index 17ce71b..02720cc 100644 --- a/runtime.go +++ b/runtime.go @@ -43,6 +43,7 @@ var ErrNotFound = errors.String("not found") type Runtime struct { once sync.Once + builder *builder Debug bool EnvPrefix string WorkDir string @@ -95,8 +96,42 @@ func (r *Runtime) Build(rr registry.Registry, servers ...transport.Server) *krat kratos.Registrar(rr), ) } + +func (r *Runtime) CreateRegistrar(serviceName string, ss ...registry.OptionSetting) (registry.KRegistrar, error) { + cfg, err := r.Config.Registry(serviceName) + if err != nil { + return nil, err + } + return r.builder.NewRegistrar(cfg, ss...) +} + +func (r *Runtime) CreateDiscovery(serviceName string, ss ...registry.OptionSetting) (registry.KDiscovery, error) { + cfg, err := r.Config.Registry(serviceName) + if err != nil { + return nil, err + } + return r.builder.NewDiscovery(cfg, ss...) +} + +func (r *Runtime) CreateGRPCServer(serviceName string, ss ...service.OptionSetting) (*service.GRPCServer, error) { + cfg, err := r.Config.Service(serviceName) + if err != nil { + return nil, err + } + return r.builder.NewGRPCServer(cfg, ss...) +} + +func (r *Runtime) CreateHTTPServer(serviceName string, ss ...service.OptionSetting) (*service.HTTPServer, error) { + cfg, err := r.Config.Service(serviceName) + if err != nil { + return nil, err + } + return r.builder.NewHTTPServer(cfg, ss...) +} + func New() Runtime { return Runtime{ + builder: build, EnvPrefix: DefaultEnvPrefix, } }