From 7eca28d309768b1e8a777882d4da4d606001bdc7 Mon Sep 17 00:00:00 2001 From: Eric Wollesen Date: Thu, 2 Nov 2023 15:43:36 -0600 Subject: [PATCH] WIP: fixes from integration --- alerts/client.go | 7 +++- log/context.go | 2 +- log/null.go | 71 ++++++++++++++++++++++++++++++++++++++ platform/config.go | 2 +- service/middleware/auth.go | 2 ++ 5 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 log/null.go diff --git a/alerts/client.go b/alerts/client.go index 213c9df7a0..3c69d3b463 100644 --- a/alerts/client.go +++ b/alerts/client.go @@ -7,6 +7,8 @@ import ( "github.com/kelseyhightower/envconfig" "github.com/tidepool-org/platform/client" + "github.com/tidepool-org/platform/log" + "github.com/tidepool-org/platform/log/null" "github.com/tidepool-org/platform/platform" ) @@ -29,12 +31,15 @@ func NewClient(cfg *ClientConfig) (*Client, error) { // Upsert updates cfg in the service if it exists, or creates it if it doesn't. func (c *Client) Upsert(ctx context.Context, cfg *Config) error { + ctx = log.NewContextWithLogger(ctx, null.NewLogger()) url := c.client.ConstructURL("v1", "alerts", cfg.UserID, cfg.FollowedUserID) return c.client.RequestData(ctx, http.MethodPost, url, nil, cfg, nil) } // Delete the alerts config. func (c *Client) Delete(ctx context.Context, cfg *Config) error { + // Ideally the base client wouldn't require a logger, or if it found a nil, would use a null logger itself. + ctx = log.NewContextWithLogger(ctx, null.NewLogger()) url := c.client.ConstructURL("v1", "alerts", cfg.UserID, cfg.FollowedUserID) return c.client.RequestData(ctx, http.MethodDelete, url, nil, nil, nil) } @@ -42,7 +47,7 @@ func (c *Client) Delete(ctx context.Context, cfg *Config) error { // ClientConfig leverages platform.Config for the alerts client. type ClientConfig struct { *platform.Config - Address string `envconfig:"TIDEPOOL_DATA_CLIENT_ADDRESS"` + Address string `envconfig:"TIDEPOOL_DATA_CLIENT_ADDRESS" required:"true"` } // ConfigLoader abstracts the method by which config values are loaded. diff --git a/log/context.go b/log/context.go index 5d1a550391..d1ea5e253a 100644 --- a/log/context.go +++ b/log/context.go @@ -16,7 +16,7 @@ func LoggerFromContext(ctx context.Context) Logger { return logger } } - return nil + return NewNullLogger() } func ContextWithField(ctx context.Context, key string, value interface{}) context.Context { diff --git a/log/null.go b/log/null.go new file mode 100644 index 0000000000..fa52ddcfb1 --- /dev/null +++ b/log/null.go @@ -0,0 +1,71 @@ +package log + +func NewNullLogger() *nullLogger { + return &nullLogger{} +} + +type nullLogger struct{} + +func (l *nullLogger) Log(level Level, message string) { + return +} + +func (l *nullLogger) Debug(message string) { + return +} + +func (l *nullLogger) Info(message string) { + return +} + +func (l *nullLogger) Warn(message string) { + return +} + +func (l *nullLogger) Error(message string) { + return +} + +func (l *nullLogger) Debugf(message string, args ...interface{}) { + return +} + +func (l *nullLogger) Infof(message string, args ...interface{}) { + return +} + +func (l *nullLogger) Warnf(message string, args ...interface{}) { + return +} + +func (l *nullLogger) Errorf(message string, args ...interface{}) { + return +} + +func (l *nullLogger) WithError(err error) Logger { + return l +} + +func (l *nullLogger) WithField(key string, value interface{}) Logger { + return l +} + +func (l *nullLogger) WithFields(fields Fields) Logger { + return l +} + +func (l *nullLogger) WithLevelRank(level Level, rank Rank) Logger { + return l +} + +func (l *nullLogger) WithLevelRanks(levelRanks LevelRanks) Logger { + return l +} + +func (l *nullLogger) WithLevel(level Level) Logger { + return l +} + +func (l *nullLogger) Level() Level { + return DebugLevel +} diff --git a/platform/config.go b/platform/config.go index e045dbc384..e8b114c110 100644 --- a/platform/config.go +++ b/platform/config.go @@ -10,7 +10,7 @@ import ( // Config extends client.Config with additional properties. type Config struct { *client.Config - ServiceSecret string `envconfig:"TIDEPOOL_SERVICE_SECRET"` + ServiceSecret string `envconfig:"TIDEPOOL_SERVER_SECRET" required:"true"` } func NewConfig() *Config { diff --git a/service/middleware/auth.go b/service/middleware/auth.go index 76a629bf0e..c79de5d705 100644 --- a/service/middleware/auth.go +++ b/service/middleware/auth.go @@ -1,6 +1,7 @@ package middleware import ( + stdlog "log" "strings" "github.com/ant0ine/go-json-rest/rest" @@ -79,6 +80,7 @@ func (a *Authenticator) MiddlewareFunc(handlerFunc rest.HandlerFunc) rest.Handle } func (a *Authenticator) authenticate(req *rest.Request) (request.AuthDetails, error) { + stdlog.Printf("!!! req: %+v", req) details, err := a.authenticateServiceSecret(req) if err != nil || details != nil { return details, err