Merge pull request 'minor tweaks for config changes' (#121) from fixup into v3

Reviewed-on: #121
This commit is contained in:
Василий Толстов 2023-03-15 01:20:06 +03:00
commit 185d23ce8a
8 changed files with 93 additions and 1077 deletions

View File

@ -1,3 +1,3 @@
package service package service
//go:generate protoc -I./proto -I. --go-grpc_out=paths=source_relative:./proto --go_out=paths=source_relative:./proto --go-micro_out=components=micro|rpc,standalone=true,debug=true,paths=source_relative:./micro proto/service.proto //go:generate protoc -I./proto -I. --go-grpc_out=paths=source_relative:./proto --go_out=paths=source_relative:./proto --go-micro_out=components=micro|grpc,standalone=true,debug=true,paths=source_relative:./micro proto/service.proto

13
go.mod
View File

@ -1,10 +1,19 @@
module go.unistack.org/micro-config-service/v3 module go.unistack.org/micro-config-service/v3
go 1.16 go 1.18
require ( require (
github.com/imdario/mergo v0.3.13 github.com/imdario/mergo v0.3.13
go.unistack.org/micro/v3 v3.10.11 go.unistack.org/micro-server-http/v3 v3.10.13
go.unistack.org/micro/v3 v3.10.16
google.golang.org/grpc v1.52.3 google.golang.org/grpc v1.52.3
google.golang.org/protobuf v1.28.1 google.golang.org/protobuf v1.28.1
) )
require (
github.com/golang/protobuf v1.5.2 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
google.golang.org/genproto v0.0.0-20221118155620-16455021b5e6 // indirect
)

1047
go.sum

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,7 @@
// Code generated by protoc-gen-go-micro. DO NOT EDIT. // Code generated by protoc-gen-go-micro. DO NOT EDIT.
// protoc-gen-go-micro version: v3.5.3 // versions:
// - protoc-gen-go-micro v3.10.2
// - protoc v3.21.12
// source: service.proto // source: service.proto
package servicepb package servicepb
@ -7,19 +9,16 @@ package servicepb
import ( import (
context "context" context "context"
proto "go.unistack.org/micro-config-service/v3/proto" proto "go.unistack.org/micro-config-service/v3/proto"
api "go.unistack.org/micro/v3/api" v3 "go.unistack.org/micro-server-http/v3"
client "go.unistack.org/micro/v3/client" client "go.unistack.org/micro/v3/client"
) )
var ( var (
ConfigName = "Config" ConfigName = "Config"
ConfigEndpoints = []api.Endpoint{}
) )
var (
func NewConfigEndpoints() []api.Endpoint { ConfigServerEndpoints = []v3.EndpointMetadata{}
return ConfigEndpoints )
}
type ConfigClient interface { type ConfigClient interface {
Load(ctx context.Context, req *proto.LoadRequest, opts ...client.CallOption) (*proto.LoadResponse, error) Load(ctx context.Context, req *proto.LoadRequest, opts ...client.CallOption) (*proto.LoadResponse, error)

View File

@ -1,5 +1,5 @@
// Code generated by protoc-gen-go-micro. DO NOT EDIT. // Code generated by protoc-gen-go-micro. DO NOT EDIT.
// protoc-gen-go-micro version: v3.5.3 // protoc-gen-go-micro version: v3.10.2
// source: service.proto // source: service.proto
package servicepb package servicepb
@ -7,7 +7,6 @@ package servicepb
import ( import (
context "context" context "context"
proto "go.unistack.org/micro-config-service/v3/proto" proto "go.unistack.org/micro-config-service/v3/proto"
api "go.unistack.org/micro/v3/api"
client "go.unistack.org/micro/v3/client" client "go.unistack.org/micro/v3/client"
server "go.unistack.org/micro/v3/server" server "go.unistack.org/micro/v3/server"
) )
@ -61,8 +60,5 @@ func RegisterConfigServer(s server.Server, sh ConfigServer, opts ...server.Handl
} }
h := &configServer{sh} h := &configServer{sh}
var nopts []server.HandlerOption var nopts []server.HandlerOption
for _, endpoint := range ConfigEndpoints {
nopts = append(nopts, api.WithEndpoint(&endpoint))
}
return s.Handle(s.NewHandler(&Config{h}, append(nopts, opts...)...)) return s.Handle(s.NewHandler(&Config{h}, append(nopts, opts...)...))
} }

View File

@ -1,7 +1,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT. // Code generated by protoc-gen-go. DO NOT EDIT.
// versions: // versions:
// protoc-gen-go v1.26.0 // protoc-gen-go v1.26.0
// protoc v3.17.3 // protoc v3.21.12
// source: service.proto // source: service.proto
package servicepb package servicepb

View File

@ -1,4 +1,8 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT. // Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.3.0
// - protoc v3.21.12
// source: service.proto
package servicepb package servicepb
@ -14,6 +18,11 @@ import (
// Requires gRPC-Go v1.32.0 or later. // Requires gRPC-Go v1.32.0 or later.
const _ = grpc.SupportPackageIsVersion7 const _ = grpc.SupportPackageIsVersion7
const (
Config_Load_FullMethodName = "/service.Config/Load"
Config_Save_FullMethodName = "/service.Config/Save"
)
// ConfigClient is the client API for Config service. // ConfigClient is the client API for Config service.
// //
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
@ -32,7 +41,7 @@ func NewConfigClient(cc grpc.ClientConnInterface) ConfigClient {
func (c *configClient) Load(ctx context.Context, in *LoadRequest, opts ...grpc.CallOption) (*LoadResponse, error) { func (c *configClient) Load(ctx context.Context, in *LoadRequest, opts ...grpc.CallOption) (*LoadResponse, error) {
out := new(LoadResponse) out := new(LoadResponse)
err := c.cc.Invoke(ctx, "/service.Config/Load", in, out, opts...) err := c.cc.Invoke(ctx, Config_Load_FullMethodName, in, out, opts...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -41,7 +50,7 @@ func (c *configClient) Load(ctx context.Context, in *LoadRequest, opts ...grpc.C
func (c *configClient) Save(ctx context.Context, in *SaveRequest, opts ...grpc.CallOption) (*SaveResponse, error) { func (c *configClient) Save(ctx context.Context, in *SaveRequest, opts ...grpc.CallOption) (*SaveResponse, error) {
out := new(SaveResponse) out := new(SaveResponse)
err := c.cc.Invoke(ctx, "/service.Config/Save", in, out, opts...) err := c.cc.Invoke(ctx, Config_Save_FullMethodName, in, out, opts...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -90,7 +99,7 @@ func _Config_Load_Handler(srv interface{}, ctx context.Context, dec func(interfa
} }
info := &grpc.UnaryServerInfo{ info := &grpc.UnaryServerInfo{
Server: srv, Server: srv,
FullMethod: "/service.Config/Load", FullMethod: Config_Load_FullMethodName,
} }
handler := func(ctx context.Context, req interface{}) (interface{}, error) { handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(ConfigServer).Load(ctx, req.(*LoadRequest)) return srv.(ConfigServer).Load(ctx, req.(*LoadRequest))
@ -108,7 +117,7 @@ func _Config_Save_Handler(srv interface{}, ctx context.Context, dec func(interfa
} }
info := &grpc.UnaryServerInfo{ info := &grpc.UnaryServerInfo{
Server: srv, Server: srv,
FullMethod: "/service.Config/Save", FullMethod: Config_Save_FullMethodName,
} }
handler := func(ctx context.Context, req interface{}) (interface{}, error) { handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(ConfigServer).Save(ctx, req.(*SaveRequest)) return srv.(ConfigServer).Save(ctx, req.(*SaveRequest))

View File

@ -27,6 +27,10 @@ func (c *serviceConfig) Options() config.Options {
} }
func (c *serviceConfig) Init(opts ...config.Option) error { func (c *serviceConfig) Init(opts ...config.Option) error {
if err := config.DefaultBeforeInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
return err
}
for _, o := range opts { for _, o := range opts {
o(&c.opts) o(&c.opts)
} }
@ -43,39 +47,55 @@ func (c *serviceConfig) Init(opts ...config.Option) error {
if cli == nil { if cli == nil {
err := fmt.Errorf("missing client option") err := fmt.Errorf("missing client option")
c.opts.Logger.Error(c.opts.Context, err)
if !c.opts.AllowFail { if !c.opts.AllowFail {
return err return err
} }
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
return err
}
return nil return nil
} }
if c.service == "" { if c.service == "" {
err := fmt.Errorf("missing Service option") err := fmt.Errorf("missing Service option")
c.opts.Logger.Error(c.opts.Context, err)
if !c.opts.AllowFail { if !c.opts.AllowFail {
return err return err
} }
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
return err
}
return nil return nil
} }
c.client = pbmicro.NewConfigClient(c.service, cli) c.client = pbmicro.NewConfigClient(c.service, cli)
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
return err
}
return nil return nil
} }
func (c *serviceConfig) Load(ctx context.Context, opts ...config.LoadOption) error { func (c *serviceConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
if err := config.DefaultBeforeLoad(ctx, c); err != nil { if err := config.DefaultBeforeLoad(ctx, c); err != nil && !c.opts.AllowFail {
return err return err
} }
rsp, err := c.client.Load(ctx, &pb.LoadRequest{Service: c.service}) rsp, err := c.client.Load(ctx, &pb.LoadRequest{Service: c.service})
if err != nil { if err != nil {
c.opts.Logger.Errorf(ctx, "service load error: %v", err)
if !c.opts.AllowFail { if !c.opts.AllowFail {
return fmt.Errorf("failed to load config: %w", err) return fmt.Errorf("failed to load config: %w", err)
} }
return config.DefaultAfterLoad(ctx, c)
if err = config.DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
return err
}
return nil
} }
options := config.NewLoadOptions(opts...) options := config.NewLoadOptions(opts...)
@ -87,22 +107,24 @@ func (c *serviceConfig) Load(ctx context.Context, opts ...config.LoadOption) err
mopts = append(mopts, mergo.WithAppendSlice) mopts = append(mopts, mergo.WithAppendSlice)
} }
src, err := rutil.Zero(c.opts.Struct) dst := c.opts.Struct
if options.Struct != nil {
dst = options.Struct
}
src, err := rutil.Zero(dst)
if err == nil { if err == nil {
err = c.opts.Codec.Unmarshal(rsp.Config, src) err = c.opts.Codec.Unmarshal(rsp.Config, src)
if err == nil { if err == nil {
err = mergo.Merge(c.opts.Struct, src, mopts...) err = mergo.Merge(dst, src, mopts...)
} }
} }
if err != nil { if err != nil && !c.opts.AllowFail {
c.opts.Logger.Errorf(ctx, "service load error: %v", err)
if !c.opts.AllowFail {
return fmt.Errorf("failed to load config: %w", err) return fmt.Errorf("failed to load config: %w", err)
} }
}
if err := config.DefaultAfterLoad(ctx, c); err != nil { if err := config.DefaultAfterLoad(ctx, c); err != nil && !c.opts.AllowFail {
return err return err
} }
@ -110,22 +132,26 @@ func (c *serviceConfig) Load(ctx context.Context, opts ...config.LoadOption) err
} }
func (c *serviceConfig) Save(ctx context.Context, opts ...config.SaveOption) error { func (c *serviceConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
if err := config.DefaultBeforeSave(ctx, c); err != nil { if err := config.DefaultBeforeSave(ctx, c); err != nil && !c.opts.AllowFail {
return err return err
} }
buf, err := c.opts.Codec.Marshal(c.opts.Struct) options := config.NewSaveOptions(opts...)
dst := c.opts.Struct
if options.Struct != nil {
dst = options.Struct
}
buf, err := c.opts.Codec.Marshal(dst)
if err == nil { if err == nil {
_, err = c.client.Save(ctx, &pb.SaveRequest{Service: c.service, Config: buf}) _, err = c.client.Save(ctx, &pb.SaveRequest{Service: c.service, Config: buf})
} }
if err != nil { if err != nil && !c.opts.AllowFail {
c.opts.Logger.Errorf(ctx, "service save error: %v", err)
if !c.opts.AllowFail {
return fmt.Errorf("failed to save config: %w", err) return fmt.Errorf("failed to save config: %w", err)
} }
}
if err := config.DefaultAfterSave(ctx, c); err != nil { if err := config.DefaultAfterSave(ctx, c); err != nil && !c.opts.AllowFail {
return err return err
} }