updates go dependencies, refactors config loading precedence, adds gRPC gateway path stripping option, and improves HTTP handler setup.
This commit is contained in:
@@ -38,7 +38,7 @@ func (a *App) runGRPC(ctx context.Context) {
|
||||
|
||||
a.HTTP.Handlers = append(a.HTTP.Handlers, optshttp.HTTPHandler{
|
||||
Prefix: a.cfg.GRPC.GRPCGatewayPath,
|
||||
StripPrefix: true,
|
||||
StripPrefix: a.cfg.GRPC.GRPCGatewayPathStrip,
|
||||
Handler: a.GRPC.GetGatewayMux(),
|
||||
})
|
||||
|
||||
|
||||
@@ -50,6 +50,10 @@ func LoadConfig(ctx context.Context) (context.Context, error) {
|
||||
func loadConfig(configPath string) (*AppConfig, error) {
|
||||
cfg := *DefaultConfig
|
||||
|
||||
if err := env.ParseWithOptions(&cfg, env.Options{DefaultValueTagName: "default"}); err != nil {
|
||||
return nil, fmt.Errorf("could not parse environment variables: %w", err)
|
||||
}
|
||||
|
||||
if configPath != "" {
|
||||
file, err := os.Open(configPath)
|
||||
if err != nil {
|
||||
@@ -63,10 +67,6 @@ func loadConfig(configPath string) (*AppConfig, error) {
|
||||
}
|
||||
}
|
||||
|
||||
if err := env.ParseWithOptions(&cfg, env.Options{DefaultValueTagName: "default"}); err != nil {
|
||||
return nil, fmt.Errorf("could not parse environment variables: %w", err)
|
||||
}
|
||||
|
||||
// Perform updates / enrichments
|
||||
err := prepareConfig(&cfg)
|
||||
if err != nil {
|
||||
|
||||
@@ -18,4 +18,5 @@ type GRPCConfig struct {
|
||||
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"`
|
||||
GRPCGatewayPathStrip bool `yaml:"grpcGatewayPathStrip" json:"grpcGatewayPathStrip,omitempty" env:"APP_GRPC_GATEWAY_PATH_STRIP" default:"false"`
|
||||
}
|
||||
|
||||
@@ -64,6 +64,9 @@ func prepHTTPServer(opts *opts.AppHTTP) *http.Server {
|
||||
if h.Pattern != "" && h.Prefix != "" {
|
||||
l.Fatal().Str("ExactPath", h.Pattern).Str("Prefix", h.Prefix).
|
||||
Msg("Can't have both pattern and prefix match for http handler")
|
||||
} else if h.Pattern == "" && h.Prefix == "" {
|
||||
l.Fatal().Str("ExactPath", h.Pattern).Str("Prefix", h.Prefix).
|
||||
Msg("Must have prefix or pattern for http handler")
|
||||
}
|
||||
|
||||
if h.Prefix != "" {
|
||||
@@ -82,7 +85,7 @@ func prepHTTPServer(opts *opts.AppHTTP) *http.Server {
|
||||
servePath = h.Pattern
|
||||
}
|
||||
|
||||
mux.Handle(h.Prefix, otelhttp.WithRouteTag(servePath, h.Handler))
|
||||
mux.Handle(servePath, otelhttp.NewHandler(h.Handler, servePath))
|
||||
}
|
||||
|
||||
// Add OTEL instrumentation, filter noise, set span names
|
||||
|
||||
Reference in New Issue
Block a user