1 Star 0 Fork 0

csingo / cRpc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Init.go 2.80 KB
一键复制 编辑 原始数据 按行查看 历史
cxy1620541673 提交于 2023-09-09 20:05 . update
package cRpc
import (
"gitee.com/csingo/cLog"
"reflect"
)
func Inject(instance interface{}) {
container.Save(instance)
}
func IsRpcService(instance interface{}) bool {
return reflect.TypeOf(instance).Implements(reflect.TypeOf((*RpcServiceInterface)(nil)).Elem())
}
func Load() {
cLog.WithContext(nil, map[string]interface{}{
"source": "cRpc.Load",
}).Trace("读取 RpcConf 配置")
loadRpcConf()
}
func loadRpcConf() {
clientContainer.SetSignature(selfRpcConf.Signature)
for _, client := range selfRpcConf.Clients {
clientContainer.Save(client)
}
}
func GetService(app, service string) interface{} {
return container.Get(app, service)
}
//func GetGinHandlerFunc() gin.HandlerFunc {
// return func(ctx *gin.Context) {
// rpcApp := ctx.Request.Header.Get("Rpc-App")
// rpcService := ctx.Request.Header.Get("Rpc-Service")
// rpcMethod := ctx.Request.Header.Get("Rpc-Method")
//
// instance := GetService(rpcApp, rpcService)
// if instance == nil {
// cLog.WithContext(ctx, map[string]interface{}{
// "source": "cRpc.GetGinHandlerFunc:GetService",
// "app": rpcApp,
// "service": rpcService,
// "method": rpcMethod,
// }).Error("服务 rpc service 异常")
// message := fmt.Sprintf("rpc service not found: %s.%s", rpcApp, rpcService)
// ctx.String(http.StatusNotAcceptable, message)
// ctx.Abort()
// return
// }
//
// caller := reflect.ValueOf(instance).MethodByName(rpcMethod)
// if !caller.IsValid() {
// cLog.WithContext(ctx, map[string]interface{}{
// "source": "cRpc.GetGinHandlerFunc:caller.IsValid",
// "app": rpcApp,
// "service": rpcService,
// "method": rpcMethod,
// }).Error("rpc service 方法不合法")
// message := fmt.Sprintf("rpc service method not found: %s.%s.%s", rpcApp, rpcService, rpcMethod)
// ctx.String(http.StatusNotAcceptable, message)
// ctx.Abort()
// return
// }
//
// paramType := caller.Type().In(1)
// param := reflect.New(paramType)
//
// jsonCaller := reflect.ValueOf(ctx).MethodByName("BindJSON")
// jsonRes := jsonCaller.Call([]reflect.Value{param})
// if !jsonRes[0].IsNil() {
// cLog.WithContext(ctx, map[string]interface{}{
// "source": "cRpc.GetGinHandlerFunc:jsonCaller.Call",
// "app": rpcApp,
// "service": rpcService,
// "method": rpcMethod,
// }).Error("rpc service 调用异常")
// ctx.String(http.StatusNotAcceptable, jsonRes[0].Interface().(error).Error())
// ctx.Abort()
// return
// }
//
// responseValues := caller.Call([]reflect.Value{reflect.ValueOf(ctx), param.Elem()})
// if !responseValues[1].IsNil() {
// err := responseValues[1].Interface().(error)
// ctx.String(http.StatusInternalServerError, err.Error())
// ctx.Abort()
// return
// }
// }
//}
func Decrypt(f DecryptFunc) {
container.SetDecrypt(f)
}
func Encrypt(f EncryptFunc) {
clientContainer.EncryptFunc = f
}
Go
1
https://gitee.com/csingo/cRpc.git
git@gitee.com:csingo/cRpc.git
csingo
cRpc
cRpc
v0.2.26

搜索帮助