go-app/pkg/srv/grpc/gateway.go

44 lines
1.3 KiB
Go

package grpc
import (
"context"
"net/http"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/grpc/opts"
)
const API_PATH = "/api" // TODO: Make Configurable
// TODO: Add GetClientConn method to appGRPCServer that supports TLS, auth, etc..
// Will probably have to convert server opts to client 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()
}
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()
// TODO: move to GetClientConn method which doesn't exist yet
clientConn, _ := grpc.NewClient(a.opts.Listen)
for _, registerGW := range service.GwRegistrationFuncs {
registerGW(ctx, a.gatewayMux, clientConn)
}
}
// Add our gateway mux to an existing http.ServeMux
func (a *appGRPCServer) mergeGatewayMuxWithHTTPMux(mux *http.ServeMux) {
mux.Handle(API_PATH, http.StripPrefix(API_PATH, a.gatewayMux))
}