Merge pull request #450 from MayurWitsLab/fix/module_name

ft: module_name wrapper middleware
This commit is contained in:
Mayur
2025-04-07 13:11:31 +05:30
committed by GitHub
7 changed files with 39 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ log:
- transaction_id - transaction_id
- message_id - message_id
- subscriber_id - subscriber_id
- module_id
http: http:
port: 8080 port: 8080
timeout: timeout:

View File

@@ -7,6 +7,7 @@ log:
- transaction_id - transaction_id
- message_id - message_id
- subscriber_id - subscriber_id
- module_id
http: http:
port: 8080 port: 8080
timeout: timeout:

View File

@@ -7,6 +7,7 @@ log:
- transaction_id - transaction_id
- message_id - message_id
- subscriber_id - subscriber_id
- module_id
http: http:
port: 8080 port: 8080
timeout: timeout:

View File

@@ -7,6 +7,7 @@ import (
"github.com/beckn/beckn-onix/core/module/handler" "github.com/beckn/beckn-onix/core/module/handler"
"github.com/beckn/beckn-onix/pkg/log" "github.com/beckn/beckn-onix/pkg/log"
"github.com/beckn/beckn-onix/pkg/model"
) )
// Config represents the configuration for a module. // Config represents the configuration for a module.
@@ -44,6 +45,7 @@ func Register(ctx context.Context, mCfgs []Config, mux *http.ServeMux, mgr handl
return fmt.Errorf("failed to add middleware: %w", err) return fmt.Errorf("failed to add middleware: %w", err)
} }
h = moduleCtxMiddleware(c.Name, h)
log.Debugf(ctx, "Registering handler %s, of type %s @ %s", c.Name, c.Handler.Type, c.Path) log.Debugf(ctx, "Registering handler %s, of type %s @ %s", c.Name, c.Handler.Type, c.Path)
mux.Handle(c.Path, h) mux.Handle(c.Path, h)
} }
@@ -71,3 +73,10 @@ func addMiddleware(ctx context.Context, mgr handler.PluginManager, handler http.
log.Debugf(ctx, "Middleware chain setup completed") log.Debugf(ctx, "Middleware chain setup completed")
return handler, nil return handler, nil
} }
func moduleCtxMiddleware(moduleName string, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), model.ContextKeyModuleId, moduleName)
next.ServeHTTP(w, r.WithContext(ctx))
})
}

View File

@@ -4,9 +4,11 @@ import (
"context" "context"
"errors" "errors"
"net/http" "net/http"
"net/http/httptest"
"testing" "testing"
"github.com/beckn/beckn-onix/core/module/handler" "github.com/beckn/beckn-onix/core/module/handler"
"github.com/beckn/beckn-onix/pkg/model"
"github.com/beckn/beckn-onix/pkg/plugin" "github.com/beckn/beckn-onix/pkg/plugin"
"github.com/beckn/beckn-onix/pkg/plugin/definition" "github.com/beckn/beckn-onix/pkg/plugin/definition"
) )
@@ -97,6 +99,26 @@ func TestRegisterSuccess(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
// Create a request and a response recorder
req := httptest.NewRequest(http.MethodGet, "/test", nil)
rec := httptest.NewRecorder()
// Create a handler that extracts context
var capturedModuleName any
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
capturedModuleName = r.Context().Value(model.ContextKeyModuleId)
w.WriteHeader(http.StatusOK)
})
wrappedHandler := moduleCtxMiddleware("test-module", testHandler)
wrappedHandler.ServeHTTP(rec, req)
// Now verify if module name exists in context
if capturedModuleName != "test-module" {
t.Errorf("expected module_id in context to be 'test-module', got %v", capturedModuleName)
}
} }
// TestRegisterFailure tests scenarios where the handler registration should fail. // TestRegisterFailure tests scenarios where the handler registration should fail.

View File

@@ -3,8 +3,8 @@ package model
import ( import (
"errors" "errors"
"fmt" "fmt"
"testing"
"net/http" "net/http"
"testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"

View File

@@ -16,6 +16,10 @@ type Subscriber struct {
Domain string `json:"domain"` Domain string `json:"domain"`
} }
type ContextKey string
const ContextKeyModuleId ContextKey = "module_id"
// Subscription represents subscription details of a network participant. // Subscription represents subscription details of a network participant.
type Subscription struct { type Subscription struct {
Subscriber `json:",inline"` Subscriber `json:",inline"`