From 4f0c5fe6653b5f4e2b48f6c889dc9de86cbfa4b7 Mon Sep 17 00:00:00 2001 From: Ryan McGuire Date: Sun, 30 Mar 2025 11:27:41 -0400 Subject: [PATCH] fix broken startup on disabled servers --- pkg/app/run.go | 10 ++++------ pkg/app/run_grpc.go | 18 +++++++++++++++--- pkg/app/run_http.go | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/pkg/app/run.go b/pkg/app/run.go index 8e86e84..af77e6f 100644 --- a/pkg/app/run.go +++ b/pkg/app/run.go @@ -44,13 +44,11 @@ func (a *App) MustRun() { // Prepare GRPC first. The GRPC server may update its opts // with a prepared GRPC-Gateway runtime.ServeMux if any of its services // have added GRPC-Gateway handlers. If present, serve under api path - if a.cfg.GRPCEnabled() { - a.runGRPC(ctx) - } + a.runGRPC(ctx) - if a.cfg.HTTPEnabled() { - a.runHTTP(ctx) - } + // Second, prepare and run HTTP server, embedding grpc-gateway + // runtime.ServeMux if set by runGRPC() + a.runHTTP(ctx) // Monitor app lifecycle go a.monitor() diff --git a/pkg/app/run_grpc.go b/pkg/app/run_grpc.go index 97444e3..d9023d7 100644 --- a/pkg/app/run_grpc.go +++ b/pkg/app/run_grpc.go @@ -3,14 +3,26 @@ package app import ( "context" + "github.com/rs/zerolog" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/trace" - "gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/http/opts" + optsgrpc "gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/grpc/opts" + optshttp "gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/http/opts" ) func (a *App) runGRPC(ctx context.Context) { + if a.GRPC == nil { + a.GRPC = &optsgrpc.AppGRPC{ + GRPCDone: make(chan error), + } + } + if !a.cfg.GRPCEnabled() { + zerolog.Ctx(ctx).Info().Msg("skipping disabled GRPC server") + return + } + span := trace.SpanFromContext(ctx) if err := a.initGRPC(ctx); err != nil { span.RecordError(err) @@ -21,10 +33,10 @@ func (a *App) runGRPC(ctx context.Context) { if a.GRPC.GetGatewayMux() != nil { if a.HTTP.Handlers == nil { - a.HTTP.Handlers = make([]opts.HTTPHandler, 0, 1) + a.HTTP.Handlers = make([]optshttp.HTTPHandler, 0, 1) } - a.HTTP.Handlers = append(a.HTTP.Handlers, opts.HTTPHandler{ + a.HTTP.Handlers = append(a.HTTP.Handlers, optshttp.HTTPHandler{ Prefix: a.cfg.GRPC.GRPCGatewayPath, StripPrefix: true, Handler: a.GRPC.GetGatewayMux(), diff --git a/pkg/app/run_http.go b/pkg/app/run_http.go index 6c90b2c..5b5e823 100644 --- a/pkg/app/run_http.go +++ b/pkg/app/run_http.go @@ -3,12 +3,26 @@ package app import ( "context" + "github.com/rs/zerolog" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/trace" + + optshttp "gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/http/opts" ) func (a *App) runHTTP(ctx context.Context) { + if a.HTTP == nil { + a.HTTP = &optshttp.AppHTTP{ + HTTPDone: make(chan any), + } + } + + if !a.cfg.HTTPEnabled() { + zerolog.Ctx(ctx).Info().Msg("skipping disabled HTTP Server") + return + } + span := trace.SpanFromContext(ctx) if err := a.initHTTP(ctx); err != nil { span.RecordError(err)