4 Commits

Author SHA1 Message Date
f8279d9653 fix handler strip path 2025-03-26 08:30:51 -04:00
b44b6a331c support runtime.ServeMux opts for grpc-gateway 2025-03-25 10:49:58 -04:00
adfedc9239 assert trailing slash for handler prefixes 2025-03-25 10:49:47 -04:00
89f016ed9d update TODO 2025-03-24 16:46:45 -04:00
5 changed files with 25 additions and 7 deletions

View File

@ -1,5 +1,6 @@
# TODO # TODO
- [ ] Test and troubleshoot GRPC Gateway supprot
- [ ] Finish implementing GRPC service support - [ ] Finish implementing GRPC service support
- [ ] Expand config test case to cover GRPC config - [ ] Expand config test case to cover GRPC config
- [ ] Expand tracing - [ ] Expand tracing

View File

@ -2,6 +2,7 @@ package grpc
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"net" "net"
@ -18,7 +19,7 @@ func (a *appGRPCServer) registerServiceGatewayHandlers(ctx context.Context, serv
if len(service.GwRegistrationFuncs) < 1 { if len(service.GwRegistrationFuncs) < 1 {
return return
} else if a.gatewayMux == nil { } else if a.gatewayMux == nil {
a.gatewayMux = runtime.NewServeMux() a.gatewayMux = runtime.NewServeMux(a.opts.GRPCGatewayOpts...)
} }
ctx, span := a.tracer.Start(ctx, "appgrpc.init.prepare.service.gwHandlers", trace.WithAttributes( ctx, span := a.tracer.Start(ctx, "appgrpc.init.prepare.service.gwHandlers", trace.WithAttributes(
@ -29,8 +30,15 @@ func (a *appGRPCServer) registerServiceGatewayHandlers(ctx context.Context, serv
clientConn := a.GetClientConn(ctx) clientConn := a.GetClientConn(ctx)
var errs error
for _, registerGW := range service.GwRegistrationFuncs { for _, registerGW := range service.GwRegistrationFuncs {
registerGW(ctx, a.gatewayMux, clientConn) 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)
} }
} }
@ -50,6 +58,7 @@ func (a *appGRPCServer) GetClientConn(ctx context.Context) *grpc.ClientConn {
if err != nil { if err != nil {
span.RecordError(err) span.RecordError(err)
span.SetStatus(codes.Error, err.Error()) span.SetStatus(codes.Error, err.Error())
panic(err)
} }
return clientConn return clientConn

View File

@ -10,8 +10,8 @@ import (
) )
type GRPCOpts struct { type GRPCOpts struct {
*config.GRPCConfig *config.GRPCConfig // Settings configurable by env or yaml
*AppGRPC *AppGRPC // Settings provided in code
} }
type AppGRPC struct { type AppGRPC struct {
@ -19,7 +19,8 @@ type AppGRPC struct {
UnaryInterceptors []grpc.UnaryServerInterceptor UnaryInterceptors []grpc.UnaryServerInterceptor
StreamInterceptors []grpc.StreamServerInterceptor StreamInterceptors []grpc.StreamServerInterceptor
GRPCOpts []grpc.ServerOption GRPCOpts []grpc.ServerOption
GRPCDialOpts []grpc.DialOption // Map ServerOptions to DialOpts for GRPC Gateway support GRPCDialOpts []grpc.DialOption // Map ServerOptions to DialOpts for GRPC Gateway support
GRPCGatewayOpts []runtime.ServeMuxOption // Configure grpc-gateway ServeMux
GRPCDone <-chan error GRPCDone <-chan error
gatewayMux *runtime.ServeMux gatewayMux *runtime.ServeMux
} }

View File

@ -8,7 +8,6 @@ import (
semconv "go.opentelemetry.io/otel/semconv/v1.27.0" semconv "go.opentelemetry.io/otel/semconv/v1.27.0"
) )
// TODO: Implement
func (a *appGRPCServer) runGRPCServer(spanCtx context.Context) error { func (a *appGRPCServer) runGRPCServer(spanCtx context.Context) error {
_, span := a.tracer.Start(spanCtx, "appgrpc.init.start") _, span := a.tracer.Start(spanCtx, "appgrpc.init.start")
defer span.End() defer span.End()

View File

@ -7,6 +7,7 @@ import (
"net" "net"
"net/http" "net/http"
"os" "os"
"strings"
"time" "time"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
@ -60,8 +61,15 @@ func prepHTTPServer(opts *opts.AppHTTP) *http.Server {
// Inject extra handlers if given // Inject extra handlers if given
// Used for grpc-gateway runtime.ServeMux handlers // Used for grpc-gateway runtime.ServeMux handlers
for _, h := range opts.Handlers { for _, h := range opts.Handlers {
// prefix must end in / to route sub-paths
if !strings.HasSuffix(h.Prefix, "/") {
h.Prefix = h.Prefix + "/"
}
// if enabled, the path prefix is stripped before
// requests are sent to the handler
if h.StripPrefix { if h.StripPrefix {
h.Handler = http.StripPrefix(h.Prefix, h.Handler) h.Handler = http.StripPrefix(h.Prefix[:len(h.Prefix)-1], h.Handler)
} }
mux.Handle(h.Prefix, h.Handler) mux.Handle(h.Prefix, h.Handler)
} }