Files
isp-gate-service/proxy/grpc/client.go
T

63 lines
1.7 KiB
Go

package grpc
import (
"github.com/integration-system/isp-lib/backend"
"github.com/integration-system/isp-lib/structure"
log "github.com/integration-system/isp-log"
"github.com/pkg/errors"
"github.com/valyala/fasthttp"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"isp-gate-service/conf"
"isp-gate-service/domain"
"isp-gate-service/log_code"
"isp-gate-service/proxy/grpc/handlers"
"isp-gate-service/utils"
)
type grpcProxy struct {
client *backend.RxGrpcClient
skipAuth bool
}
func NewProxy(skipAuth bool) *grpcProxy {
return &grpcProxy{
client: backend.NewRxGrpcClient(
backend.WithDialOptions(
grpc.WithInsecure(), grpc.WithBlock(),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(int(conf.DefaultMaxResponseBodySize))),
grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(int(conf.DefaultMaxResponseBodySize))),
),
backend.WithDialingErrorHandler(func(err error) {
log.Errorf(log_code.ErrorClientGrpc, "dialing err: %v", err)
})),
skipAuth: skipAuth,
}
}
func (p *grpcProxy) ProxyRequest(ctx *fasthttp.RequestCtx, path string) domain.ProxyResponse {
if p.client.InternalGrpcClient == nil {
msg := "client undefined"
log.Error(log_code.ErrorClientGrpc, msg)
utils.WriteError(ctx, msg, codes.Internal, nil)
return domain.Create().
SetRequestBody(ctx.Request.Body()).
SetResponseBody(ctx.Response.Body()).
SetError(errors.New(msg))
}
return handlers.Handler.Get(ctx).Complete(ctx, path, p.client)
}
func (p *grpcProxy) Consumer(addr []structure.AddressConfiguration) bool {
return p.client.ReceiveAddressList(addr)
}
func (p *grpcProxy) SkipAuth() bool {
return p.skipAuth
}
func (p *grpcProxy) Close() {
p.client.Close()
}