support runtime.ServeMux opts for grpc-gateway
All checks were successful
Build and Publish / release (push) Has been skipped
Build and Publish / check-chart (push) Successful in 10s
Build and Publish / helm-release (push) Has been skipped

This commit is contained in:
2025-03-25 17:05:59 -04:00
parent 1e481627e3
commit f201ac1fca
10 changed files with 78 additions and 6 deletions

View File

@ -318,6 +318,18 @@ func (aw *AmbientWeather) enrichStation(update *weather.WeatherUpdate) {
}
}
func (aw *AmbientWeather) GetLogger() *zerolog.Logger {
aw.RLock()
defer aw.RUnlock()
return aw.l
}
func (aw *AmbientWeather) GetCtx() context.Context {
aw.RLock()
defer aw.RUnlock()
return aw.appCtx
}
func (aw *AmbientWeather) GetRecorder() *recorder.WeatherRecorder {
aw.RLock()
defer aw.RUnlock()

33
pkg/ambient/app.go Normal file
View File

@ -0,0 +1,33 @@
package ambient
import (
"context"
"net/http"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
)
func (aw *AmbientWeather) GetGatewayOpts() []runtime.ServeMuxOption {
return []runtime.ServeMuxOption{
runtime.WithMiddlewares(func(hf runtime.HandlerFunc) runtime.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
aw.GetLogger().Trace().
Any("pathParams", pathParams).
Msg("inbound request called")
}
}),
runtime.WithRoutingErrorHandler(
func(
ctx context.Context, mux *runtime.ServeMux, mshl runtime.Marshaler,
w http.ResponseWriter, r *http.Request, code int,
) {
aw.GetLogger().Trace().
Int("code", code).
Str("pattern", r.Pattern).
Msg("requesting grpc-gateway route")
// Pass back to default
runtime.DefaultRoutingErrorHandler(ctx, mux, mshl, w, r, code)
}),
}
}

View File

@ -29,6 +29,10 @@ type Opts struct {
KeepLast int
}
func (r *WeatherRecorder) Ping(ctx context.Context) error {
return r.recorder.Ping(ctx)
}
func MustNewWeatherRecorder(opts *Opts) *WeatherRecorder {
if opts.KeepLast < 1 {
opts.KeepLast = 1

View File

@ -25,6 +25,11 @@ type MemoryRecorder struct {
*sync.RWMutex
}
// No meaningful health check to do here
func (r *MemoryRecorder) Ping(ctx context.Context) error {
return nil
}
func (r *MemoryRecorder) Init(ctx context.Context, opts *recorders.RecorderOpts) {
if opts.RetainLast < 1 {
opts.RetainLast = DEF_RETAIN_LAST

View File

@ -20,4 +20,6 @@ func (n *NoopRecorder) Get(context.Context, *pb.GetWeatherRequest) ([]*weather.W
func (n *NoopRecorder) Count(context.Context) int { return 0 }
func (n *NoopRecorder) Ping(context.Context) error { return nil }
func (r *NoopRecorder) Name() string { return "no-op recorder" }

View File

@ -19,5 +19,6 @@ type Recorder interface {
Set(context.Context, *weather.WeatherUpdate) error
Get(context.Context, *pb.GetWeatherRequest) ([]*weather.WeatherUpdate, error)
Count(context.Context) int // Best Effort
Ping(context.Context) error
Name() string
}

View File

@ -36,6 +36,11 @@ type RedisRecorder struct {
*sync.RWMutex
}
func (r *RedisRecorder) Ping(ctx context.Context) error {
_, err := r.redis.Ping(ctx).Result()
return err
}
func (r *RedisRecorder) Init(ctx context.Context, opts *recorders.RecorderOpts) {
if opts.AppConfig.RecorderConfig.RedisConfig == nil {
panic("refusing to init redis recorder with no redisConfig")