66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
package grpc
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
|
|
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/codes"
|
|
"go.opentelemetry.io/otel/trace"
|
|
"google.golang.org/grpc"
|
|
|
|
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/grpc/opts"
|
|
)
|
|
|
|
func (a *appGRPCServer) registerServiceGatewayHandlers(ctx context.Context, service *opts.GRPCService) {
|
|
if len(service.GwRegistrationFuncs) < 1 {
|
|
return
|
|
} else if a.gatewayMux == nil {
|
|
a.gatewayMux = runtime.NewServeMux(a.opts.GRPCGatewayOpts...)
|
|
}
|
|
|
|
ctx, span := a.tracer.Start(ctx, "appgrpc.init.prepare.service.gwHandlers", trace.WithAttributes(
|
|
attribute.String("service", service.Name),
|
|
attribute.Int("gwHandlers", len(service.GwRegistrationFuncs)),
|
|
))
|
|
defer span.End()
|
|
|
|
clientConn := a.GetClientConn(ctx)
|
|
|
|
var errs error
|
|
for _, registerGW := range service.GwRegistrationFuncs {
|
|
errs = errors.Join(errs, registerGW(ctx, a.gatewayMux, clientConn))
|
|
a.logger.Debug().Any("fwo", a.gatewayMux.GetForwardResponseOptions()).
|
|
Msg("calling gateway registration func")
|
|
}
|
|
|
|
if errs != nil {
|
|
panic(errs)
|
|
}
|
|
}
|
|
|
|
func (a *appGRPCServer) GetClientConn(ctx context.Context) *grpc.ClientConn {
|
|
span := trace.SpanFromContext(ctx)
|
|
|
|
host, port, err := net.SplitHostPort(a.opts.Listen)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if host == "" || host == "0.0.0.0" {
|
|
host = "localhost"
|
|
}
|
|
|
|
clientConn, err := grpc.NewClient(fmt.Sprintf("%s:%s", host, port), a.opts.GRPCDialOpts...)
|
|
if err != nil {
|
|
span.RecordError(err)
|
|
span.SetStatus(codes.Error, err.Error())
|
|
panic(err)
|
|
}
|
|
|
|
return clientConn
|
|
}
|