resolved reqpreprocessing review comments

1. added logs for contextKey parsing and unmarshaling yml file.
2. resolved router test case issue.
2. resolved logger test case issues.
This commit is contained in:
MohitKatare-protean
2025-04-06 17:09:59 +05:30
parent 278e217c64
commit ec62a3242b
7 changed files with 176 additions and 70 deletions

View File

@@ -15,7 +15,8 @@ import (
// Config represents the configuration for the request preprocessor middleware.
type Config struct {
Role string
Role string
ContextKeys []string
}
const contextKey = "context"
@@ -57,7 +58,12 @@ func NewPreProcessor(cfg *Config) (func(http.Handler) http.Handler, error) {
log.Debugf(ctx, "adding subscriberId to request:%s, %v", model.ContextKeySubscriberID, subID)
ctx = context.WithValue(ctx, model.ContextKeySubscriberID, subID)
}
for _, key := range cfg.ContextKeys {
ctxKey, _ := model.ParseContextKey(key)
if v, ok := reqContext[key]; ok {
ctx = context.WithValue(ctx, ctxKey, v)
}
}
r.Body = io.NopCloser(bytes.NewBuffer(body))
r.ContentLength = int64(len(body))
r = r.WithContext(ctx)
@@ -74,5 +80,11 @@ func validateConfig(cfg *Config) error {
if cfg.Role != "bap" && cfg.Role != "bpp" {
return errors.New("role must be either 'bap' or 'bpp'")
}
for _, key := range cfg.ContextKeys {
if _, err := model.ParseContextKey(key); err != nil {
return err
}
}
return nil
}