minor tweaks for config changes #121
@ -1,3 +1,3 @@
|
||||
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
13
go.mod
@ -1,10 +1,19 @@
|
||||
module go.unistack.org/micro-config-service/v3
|
||||
|
||||
go 1.16
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
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/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
|
||||
)
|
||||
|
@ -1,5 +1,7 @@
|
||||
// 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
|
||||
|
||||
package servicepb
|
||||
@ -7,19 +9,16 @@ package servicepb
|
||||
import (
|
||||
context "context"
|
||||
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"
|
||||
)
|
||||
|
||||
var (
|
||||
ConfigName = "Config"
|
||||
|
||||
ConfigEndpoints = []api.Endpoint{}
|
||||
)
|
||||
|
||||
func NewConfigEndpoints() []api.Endpoint {
|
||||
return ConfigEndpoints
|
||||
}
|
||||
var (
|
||||
ConfigServerEndpoints = []v3.EndpointMetadata{}
|
||||
)
|
||||
|
||||
type ConfigClient interface {
|
||||
Load(ctx context.Context, req *proto.LoadRequest, opts ...client.CallOption) (*proto.LoadResponse, error)
|
||||
|
@ -1,5 +1,5 @@
|
||||
// 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
|
||||
|
||||
package servicepb
|
||||
@ -7,7 +7,6 @@ package servicepb
|
||||
import (
|
||||
context "context"
|
||||
proto "go.unistack.org/micro-config-service/v3/proto"
|
||||
api "go.unistack.org/micro/v3/api"
|
||||
client "go.unistack.org/micro/v3/client"
|
||||
server "go.unistack.org/micro/v3/server"
|
||||
)
|
||||
@ -61,8 +60,5 @@ func RegisterConfigServer(s server.Server, sh ConfigServer, opts ...server.Handl
|
||||
}
|
||||
h := &configServer{sh}
|
||||
var nopts []server.HandlerOption
|
||||
for _, endpoint := range ConfigEndpoints {
|
||||
nopts = append(nopts, api.WithEndpoint(&endpoint))
|
||||
}
|
||||
return s.Handle(s.NewHandler(&Config{h}, append(nopts, opts...)...))
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.26.0
|
||||
// protoc v3.17.3
|
||||
// protoc v3.21.12
|
||||
// source: service.proto
|
||||
|
||||
package servicepb
|
||||
|
@ -1,4 +1,8 @@
|
||||
// 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
|
||||
|
||||
@ -14,6 +18,11 @@ import (
|
||||
// Requires gRPC-Go v1.32.0 or later.
|
||||
const _ = grpc.SupportPackageIsVersion7
|
||||
|
||||
const (
|
||||
Config_Load_FullMethodName = "/service.Config/Load"
|
||||
Config_Save_FullMethodName = "/service.Config/Save"
|
||||
)
|
||||
|
||||
// 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.
|
||||
@ -32,7 +41,7 @@ func NewConfigClient(cc grpc.ClientConnInterface) ConfigClient {
|
||||
|
||||
func (c *configClient) Load(ctx context.Context, in *LoadRequest, opts ...grpc.CallOption) (*LoadResponse, error) {
|
||||
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 {
|
||||
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) {
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
@ -90,7 +99,7 @@ func _Config_Load_Handler(srv interface{}, ctx context.Context, dec func(interfa
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/service.Config/Load",
|
||||
FullMethod: Config_Load_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
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{
|
||||
Server: srv,
|
||||
FullMethod: "/service.Config/Save",
|
||||
FullMethod: Config_Save_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(ConfigServer).Save(ctx, req.(*SaveRequest))
|
||||
|
64
service.go
64
service.go
@ -27,6 +27,10 @@ func (c *serviceConfig) Options() config.Options {
|
||||
}
|
||||
|
||||
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 {
|
||||
o(&c.opts)
|
||||
}
|
||||
@ -43,39 +47,55 @@ func (c *serviceConfig) Init(opts ...config.Option) error {
|
||||
|
||||
if cli == nil {
|
||||
err := fmt.Errorf("missing client option")
|
||||
c.opts.Logger.Error(c.opts.Context, err)
|
||||
if !c.opts.AllowFail {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
if c.service == "" {
|
||||
err := fmt.Errorf("missing Service option")
|
||||
c.opts.Logger.Error(c.opts.Context, err)
|
||||
if !c.opts.AllowFail {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
c.client = pbmicro.NewConfigClient(c.service, cli)
|
||||
|
||||
if err := config.DefaultAfterInit(c.opts.Context, c); err != nil && !c.opts.AllowFail {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
rsp, err := c.client.Load(ctx, &pb.LoadRequest{Service: c.service})
|
||||
if err != nil {
|
||||
c.opts.Logger.Errorf(ctx, "service load error: %v", err)
|
||||
if !c.opts.AllowFail {
|
||||
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...)
|
||||
@ -87,22 +107,24 @@ func (c *serviceConfig) Load(ctx context.Context, opts ...config.LoadOption) err
|
||||
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 {
|
||||
err = c.opts.Codec.Unmarshal(rsp.Config, src)
|
||||
if err == nil {
|
||||
err = mergo.Merge(c.opts.Struct, src, mopts...)
|
||||
err = mergo.Merge(dst, src, mopts...)
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
c.opts.Logger.Errorf(ctx, "service load error: %v", err)
|
||||
if !c.opts.AllowFail {
|
||||
if err != nil && !c.opts.AllowFail {
|
||||
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
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
if err := config.DefaultBeforeSave(ctx, c); err != nil {
|
||||
if err := config.DefaultBeforeSave(ctx, c); err != nil && !c.opts.AllowFail {
|
||||
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 {
|
||||
_, err = c.client.Save(ctx, &pb.SaveRequest{Service: c.service, Config: buf})
|
||||
}
|
||||
if err != nil {
|
||||
c.opts.Logger.Errorf(ctx, "service save error: %v", err)
|
||||
if !c.opts.AllowFail {
|
||||
if err != nil && !c.opts.AllowFail {
|
||||
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
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user