package grpc import ( "context" "errors" "gitlab.finogeeks.club/finclip-backend/mop-middleware-auth/config" "gitlab.finogeeks.club/finclip-backend/mop-middleware-auth/grpc/pb" "gitlab.finogeeks.club/finclip-backend/mop-middleware-auth/logger" ) func AuthCheck(ctx context.Context, userId, serviceName, relativePath string) (*pb.CheckAuthRsp, error) { if config.GetConfig().ReqType == "" || config.GetConfig().ReqType != "grpc" { return AuthCheckByHttp(ctx, userId, serviceName, relativePath) } req := &pb.CheckAuthReq{ UserId: userId, ServiceName: serviceName, RelativePath: relativePath, } rsp := &pb.CheckAuthRsp{} var err error for i := 0; i < 2; i++ { conn, err1 := getGrpcConnManager().GetConn(config.GetConfig().ConsulAddr, "mop-auth-checker", "tag="+config.GetConfig().ConsulTag) if err1 != nil { err = err1 continue } c := pb.NewMopAuthCheckClient(conn) func() { //span, ctx := apm.ApmClient().CreateGRpcExitSpan(ctx, "GrpcGetControlConfigInfo", "OpenAPIGrpcGetControlConfigInfo", GRPC_SERVER_NAME_CONTROL_MANAGER) //defer span.End() rsp, err = c.MopAuthCheck(ctx, req) }() if err != nil { logger.GetLogger().Errorln("err=%s", err.Error()) continue } else { err = nil break } } if err != nil { return nil, err } return rsp, err } type AuthCheckByHttpHttpRsp struct { Error string `json:"error"` Errcode string `json:"errcode"` Data pb.CheckAuthRsp `json:"data"` } func AuthCheckByHttp(ctx context.Context, userId, serviceName, relativePath string) (*pb.CheckAuthRsp, error) { req := pb.CheckAuthReq{ UserId: userId, ServiceName: serviceName, RelativePath: relativePath, } result := pb.CheckAuthRsp{} rsp := AuthCheckByHttpHttpRsp{} var headers = map[string]string{ "Accept": "application/json, text/plain, */*", "Content-Type": "application/json", } result.Result = &pb.CommonResult{} result.Result.Httpcode = 400 result.Result.Errcode = "" result.Result.Error = "校验失败" result.Data = &pb.CheckAuthRsp_DATA{} result.Data.HasAuthor = false url := "http://mop-auth-checker:8080/api/v1/mop/mop-auth-checker/auth/check" rsp2, err := RestyClient.R().SetContext(ctx).SetHeaders(headers).SetBody(req).SetResult(&rsp).Post(url) if err != nil { logger.GetLogger().Errorf("goResty fail, rsp:%s, err:%s", rsp2.String(), err.Error()) return &result, err } else if rsp2.StatusCode() != 200 { logger.GetLogger().Errorf("goResty fail, httpCode:%d", rsp2.StatusCode()) return &result, errors.New("status err") } result.Result.Httpcode = int32(rsp2.StatusCode()) result.Result.Errcode = rsp.Errcode result.Result.Error = rsp.Error result.Data.HasAuthor = rsp.Data.Data.HasAuthor return &result, err }