From 7f60a59d427973d0d35beae542dbd485f875436a Mon Sep 17 00:00:00 2001 From: Ryan D McGuire Date: Mon, 24 Mar 2025 16:39:11 -0400 Subject: [PATCH] implement grpc gateway and addl handler support --- pkg/app/run.go | 3 --- pkg/app/run_grpc.go | 2 +- pkg/config/types_grpc.go | 2 ++ pkg/srv/grpc/gateway.go | 2 +- pkg/srv/grpc/grpc.go | 2 +- pkg/srv/grpc/opts/config.go | 12 +++++++----- pkg/srv/grpc/services.go | 4 +++- 7 files changed, 15 insertions(+), 12 deletions(-) diff --git a/pkg/app/run.go b/pkg/app/run.go index 3f012bb..8e86e84 100644 --- a/pkg/app/run.go +++ b/pkg/app/run.go @@ -9,9 +9,6 @@ import ( "gitea.libretechconsulting.com/rmcguire/go-app/pkg/config" ) -// TODO: Make Configurable -const GRPC_GW_API_PATH = "/api" - func (a *App) Done() <-chan any { return a.appDone } diff --git a/pkg/app/run_grpc.go b/pkg/app/run_grpc.go index 7e73832..97444e3 100644 --- a/pkg/app/run_grpc.go +++ b/pkg/app/run_grpc.go @@ -25,7 +25,7 @@ func (a *App) runGRPC(ctx context.Context) { } a.HTTP.Handlers = append(a.HTTP.Handlers, opts.HTTPHandler{ - Prefix: GRPC_GW_API_PATH, + Prefix: a.cfg.GRPC.GRPCGatewayPath, StripPrefix: true, Handler: a.GRPC.GetGatewayMux(), }) diff --git a/pkg/config/types_grpc.go b/pkg/config/types_grpc.go index bf19310..86a4fbc 100644 --- a/pkg/config/types_grpc.go +++ b/pkg/config/types_grpc.go @@ -15,4 +15,6 @@ type GRPCConfig struct { LogRequests bool `yaml:"logRequests" env:"APP_GRPC_LOG_REQUESTS" json:"logRequests,omitempty"` EnableReflection bool `yaml:"enableReflection" env:"APP_GRPC_ENABLE_REFLECTION" json:"enableReflection,omitempty"` EnableInstrumentation bool `yaml:"enableInstrumentation" env:"APP_GRPC_ENABLE_INSTRUMENTATION" json:"enableInstrumentation,omitempty"` // requires OTEL + EnableGRPCGateway bool `yaml:"enableGRPCGateway" json:"enableGRPCGateway,omitempty" env:"APP_GRPC_ENABLE_GATEWAY" default:"true"` + GRPCGatewayPath string `yaml:"grpcGatewayPath" json:"grpcGatewayPath,omitempty" env:"APP_GRPC_GATEWAY_PATH" default:"/grpc-api"` } diff --git a/pkg/srv/grpc/gateway.go b/pkg/srv/grpc/gateway.go index 1c86156..c9280ea 100644 --- a/pkg/srv/grpc/gateway.go +++ b/pkg/srv/grpc/gateway.go @@ -5,7 +5,7 @@ import ( "fmt" "net" - "github.com/grpc-ecosystem/grpc-gateway/runtime" + "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/trace" diff --git a/pkg/srv/grpc/grpc.go b/pkg/srv/grpc/grpc.go index 09a7ff0..5d169c9 100644 --- a/pkg/srv/grpc/grpc.go +++ b/pkg/srv/grpc/grpc.go @@ -4,7 +4,7 @@ import ( "context" "strings" - "github.com/grpc-ecosystem/grpc-gateway/runtime" + "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "github.com/rs/zerolog" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" diff --git a/pkg/srv/grpc/opts/config.go b/pkg/srv/grpc/opts/config.go index efed352..1436fda 100644 --- a/pkg/srv/grpc/opts/config.go +++ b/pkg/srv/grpc/opts/config.go @@ -3,7 +3,7 @@ package opts import ( "context" - "github.com/grpc-ecosystem/grpc-gateway/runtime" + "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "google.golang.org/grpc" "gitea.libretechconsulting.com/rmcguire/go-app/pkg/config" @@ -25,12 +25,14 @@ type AppGRPC struct { } type GRPCService struct { - Name string // Descriptive name of service - Type *grpc.ServiceDesc // Type (from protoc generated code) - Service any // Implementation of GRPCService.Type (ptr) - GwRegistrationFuncs []func(context.Context, *runtime.ServeMux, *grpc.ClientConn) error // Gateway regustration handler funcs + Name string // Descriptive name of service + Type *grpc.ServiceDesc // Type (from protoc generated code) + Service any // Implementation of GRPCService.Type (ptr) + GwRegistrationFuncs []GwRegistrationFunc // Gateway regustration handler funcs } +type GwRegistrationFunc func(context.Context, *runtime.ServeMux, *grpc.ClientConn) error + func (a *AppGRPC) SetGatewayMux(mux *runtime.ServeMux) { a.gatewayMux = mux } diff --git a/pkg/srv/grpc/services.go b/pkg/srv/grpc/services.go index a8036b7..d1b9542 100644 --- a/pkg/srv/grpc/services.go +++ b/pkg/srv/grpc/services.go @@ -19,7 +19,9 @@ func (a *appGRPCServer) registerGRPCServices(ctx context.Context) { for _, service := range a.opts.Services { a.registerGRPCService(ctx, service) - a.registerServiceGatewayHandlers(ctx, service) + if a.opts.EnableGRPCGateway { + a.registerServiceGatewayHandlers(ctx, service) + } } span.SetStatus(codes.Ok, "")