添加配置调用
This commit is contained in:
parent
ffdfa9c94f
commit
7db81a88e4
@ -52,14 +52,18 @@ func ClientMetadataUnaryChain(ctx context.Context, req interface{}, info *grpc.U
|
||||
if strings.HasPrefix(k, "x-cfg-") && len(v) > 0 {
|
||||
c := strings.Join(v, "")
|
||||
if len(c) > 0 {
|
||||
adapter, err := gcfg.NewAdapterContent(gbase64.MustDecodeToString(v[0]))
|
||||
a, err := gcfg.NewAdapterContent(gbase64.MustDecodeToString(v[0]))
|
||||
if err != nil {
|
||||
g.Log("unary").Error(ctx, err)
|
||||
continue
|
||||
}
|
||||
g.Cfg(strings.TrimPrefix(k, "x-cfg-")).SetAdapter(adapter)
|
||||
g.Cfg(strings.TrimPrefix(k, "x-cfg-")).SetAdapter(a)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
} else {
|
||||
g.Log("unary").Error(ctx, "No Metadata from incoming cfg context")
|
||||
}
|
||||
return handler(ctx, req)
|
||||
}
|
||||
|
||||
29
config/config.go
Normal file
29
config/config.go
Normal file
@ -0,0 +1,29 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gogf/gf/v2/encoding/gbase64"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gcfg"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
func All(ctx context.Context) *gcfg.Config {
|
||||
return g.Cfg()
|
||||
}
|
||||
|
||||
func Api(ctx context.Context) *gcfg.Config {
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
c := md.Get("x-cfg-api")
|
||||
if ok && len(c) > 0 {
|
||||
b, _ := gbase64.DecodeToString(c[0])
|
||||
a, err := gcfg.NewAdapterContent(b)
|
||||
if err != nil {
|
||||
g.Log("config with api").Error(ctx, err)
|
||||
}
|
||||
return gcfg.NewWithAdapter(a)
|
||||
}
|
||||
|
||||
return g.Cfg("api")
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user