Update on the review comments
This commit is contained in:
@@ -13,9 +13,10 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Config holds the configuration settings for the application.
|
||||
type Config struct {
|
||||
ContextKeys []string
|
||||
Role string
|
||||
ContextKeys []string // ContextKeys is a list of context keys used for request processing.
|
||||
Role string // Role specifies the role of the entity (e.g., subscriber, gateway).
|
||||
}
|
||||
|
||||
type becknRequest struct {
|
||||
@@ -25,6 +26,8 @@ type becknRequest struct {
|
||||
const contextKey = "context"
|
||||
const subscriberIDKey = "subscriber_id"
|
||||
|
||||
// NewPreProcessor creates a middleware that processes incoming HTTP requests by extracting
|
||||
// and modifying the request context based on the provided configuration.
|
||||
func NewPreProcessor(cfg *Config) (func(http.Handler) http.Handler, error) {
|
||||
if err := validateConfig(cfg); err != nil {
|
||||
return nil, err
|
||||
@@ -51,6 +54,7 @@ func NewPreProcessor(cfg *Config) (func(http.Handler) http.Handler, error) {
|
||||
}
|
||||
if subID != nil {
|
||||
log.Debugf(ctx, "adding subscriberId to request:%s, %v", subscriberIDKey, subID)
|
||||
// TODO: Add a ContextKey type in model and use it here instead of raw context key.
|
||||
ctx = context.WithValue(ctx, subscriberIDKey, subID)
|
||||
}
|
||||
for _, key := range cfg.ContextKeys {
|
||||
|
||||
@@ -201,11 +201,6 @@ func validateRules(rules []routingRule) error {
|
||||
|
||||
// Route determines the routing destination based on the request context.
|
||||
func (r *Router) Route(ctx context.Context, url *url.URL, body []byte) (*model.Route, error) {
|
||||
if r == nil {
|
||||
|
||||
log.Debug(ctx, "In Router :Router not set")
|
||||
}
|
||||
log.Debugf(ctx, "In Router: Routing request with url %v and body: %s", url, string(body))
|
||||
// Parse the body to extract domain and version
|
||||
var requestBody struct {
|
||||
Context struct {
|
||||
@@ -218,17 +213,11 @@ func (r *Router) Route(ctx context.Context, url *url.URL, body []byte) (*model.R
|
||||
if err := json.Unmarshal(body, &requestBody); err != nil {
|
||||
return nil, fmt.Errorf("error parsing request body: %w", err)
|
||||
}
|
||||
log.Debugf(ctx, "In Router: Routing request with %v and body: %#s", url, requestBody)
|
||||
log.Debugf(ctx, "In Router: Routing request with %v and body: %v", url, requestBody)
|
||||
|
||||
// Extract the endpoint from the URL
|
||||
endpoint := path.Base(url.Path)
|
||||
|
||||
if r.rules == nil {
|
||||
|
||||
log.Debug(ctx, "In Router :Routing rules not set")
|
||||
}
|
||||
log.Debugf(ctx, "In Router :Routing rules len :%d", len(r.rules))
|
||||
|
||||
// Lookup route in the optimized map
|
||||
domainRules, ok := r.rules[requestBody.Context.Domain]
|
||||
if !ok {
|
||||
|
||||
Reference in New Issue
Block a user