Skip to content

Commit

Permalink
feat(runtime): add service configuration and discovery support
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
godcong committed Dec 24, 2024
1 parent 1851472 commit aa9ab15
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
31 changes: 29 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion gen/go/config/v1/data.pb.validate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var ErrNotFound = errors.String("not found")

type Runtime struct {
once sync.Once
builder *builder
Debug bool
EnvPrefix string
WorkDir string
Expand Down Expand Up @@ -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,
}
}

0 comments on commit aa9ab15

Please sign in to comment.