Update on the Review Comments
1. Resolved review comments. 2. Resolved Go linting issues. 3. Increase coverage from 93 to 96 percentage for module.
This commit is contained in:
@@ -25,10 +25,10 @@ type Config struct {
|
|||||||
Log log.Config `yaml:"log"`
|
Log log.Config `yaml:"log"`
|
||||||
PluginManager *plugin.ManagerConfig `yaml:"pluginManager"`
|
PluginManager *plugin.ManagerConfig `yaml:"pluginManager"`
|
||||||
Modules []module.Config `yaml:"modules"`
|
Modules []module.Config `yaml:"modules"`
|
||||||
HTTP httpConfig `yaml:"http"` // Nest http config
|
HTTP timeouts `yaml:"http"` // Nest http config
|
||||||
}
|
}
|
||||||
|
|
||||||
type httpConfig struct {
|
type timeouts struct {
|
||||||
Port string `yaml:"port"`
|
Port string `yaml:"port"`
|
||||||
Timeout timeoutConfig `yaml:"timeout"`
|
Timeout timeoutConfig `yaml:"timeout"`
|
||||||
}
|
}
|
||||||
@@ -44,7 +44,7 @@ var runFunc = run
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Define and parse command-line flags.
|
// Define and parse command-line flags.
|
||||||
flag.StringVar(&configPath, "config", "../../config/clientSideHandler-config.yaml", "Path to the configuration file")
|
flag.StringVar(&configPath, "config", "../../config/onix/adapter.yaml", "Path to the configuration file")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// Use custom log for initial setup messages.
|
// Use custom log for initial setup messages.
|
||||||
|
|||||||
@@ -73,22 +73,6 @@ func (m *MockPluginManager) SchemaValidator(ctx context.Context, cfg *plugin.Con
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// testConfigContent defines a mock configuration used in tests.
|
|
||||||
const testConfigContent = `
|
|
||||||
appName: "TestApp"
|
|
||||||
http:
|
|
||||||
port: "8080"
|
|
||||||
timeout:
|
|
||||||
read: 5
|
|
||||||
write: 5
|
|
||||||
idle: 10
|
|
||||||
`
|
|
||||||
|
|
||||||
// initLogger is a mock function to initialize the logger.
|
|
||||||
var initLogger = func(cfg *Config) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// mockRun is a mock implementation of the `run` function, simulating a successful run.
|
// mockRun is a mock implementation of the `run` function, simulating a successful run.
|
||||||
func mockRun(ctx context.Context, configPath string) error {
|
func mockRun(ctx context.Context, configPath string) error {
|
||||||
return nil // Simulate a successful run
|
return nil // Simulate a successful run
|
||||||
@@ -110,7 +94,9 @@ func TestMainFunction(t *testing.T) {
|
|||||||
fs := flag.NewFlagSet("test", flag.ExitOnError)
|
fs := flag.NewFlagSet("test", flag.ExitOnError)
|
||||||
fs.StringVar(&configPath, "config", "../../config/clientSideHandler-config.yaml", "Path to the configuration file")
|
fs.StringVar(&configPath, "config", "../../config/clientSideHandler-config.yaml", "Path to the configuration file")
|
||||||
|
|
||||||
fs.Parse(os.Args[1:])
|
if err := fs.Parse(os.Args[1:]); err != nil {
|
||||||
|
t.Fatalf("Failed to parse flags: %v", err)
|
||||||
|
}
|
||||||
main()
|
main()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -378,7 +364,7 @@ func TestNewServerSuccess(t *testing.T) {
|
|||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
cfg := &Config{
|
cfg := &Config{
|
||||||
Modules: tt.modules,
|
Modules: tt.modules,
|
||||||
HTTP: httpConfig{
|
HTTP: timeouts{
|
||||||
Port: "8080",
|
Port: "8080",
|
||||||
Timeout: timeoutConfig{
|
Timeout: timeoutConfig{
|
||||||
Read: 5,
|
Read: 5,
|
||||||
@@ -423,7 +409,7 @@ func TestNewServerFailure(t *testing.T) {
|
|||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
cfg := &Config{
|
cfg := &Config{
|
||||||
Modules: tt.modules,
|
Modules: tt.modules,
|
||||||
HTTP: httpConfig{
|
HTTP: timeouts{
|
||||||
Port: "8080",
|
Port: "8080",
|
||||||
Timeout: timeoutConfig{
|
Timeout: timeoutConfig{
|
||||||
Read: 5,
|
Read: 5,
|
||||||
@@ -455,7 +441,7 @@ func TestValidateConfigSuccess(t *testing.T) {
|
|||||||
name: "Valid Config",
|
name: "Valid Config",
|
||||||
cfg: Config{
|
cfg: Config{
|
||||||
AppName: "TestApp",
|
AppName: "TestApp",
|
||||||
HTTP: httpConfig{
|
HTTP: timeouts{
|
||||||
Port: "8080",
|
Port: "8080",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -483,7 +469,7 @@ func TestValidateConfigFailure(t *testing.T) {
|
|||||||
name: "Missing AppName",
|
name: "Missing AppName",
|
||||||
cfg: Config{
|
cfg: Config{
|
||||||
AppName: "",
|
AppName: "",
|
||||||
HTTP: httpConfig{
|
HTTP: timeouts{
|
||||||
Port: "8080",
|
Port: "8080",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -493,7 +479,7 @@ func TestValidateConfigFailure(t *testing.T) {
|
|||||||
name: "Missing Port",
|
name: "Missing Port",
|
||||||
cfg: Config{
|
cfg: Config{
|
||||||
AppName: "TestApp",
|
AppName: "TestApp",
|
||||||
HTTP: httpConfig{
|
HTTP: timeouts{
|
||||||
Port: "",
|
Port: "",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
69
config/bap.yaml
Normal file
69
config/bap.yaml
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
appName: "bapAdapter"
|
||||||
|
log:
|
||||||
|
level: debug
|
||||||
|
destinations:
|
||||||
|
- type: stdout
|
||||||
|
context_keys:
|
||||||
|
- transaction_id
|
||||||
|
- message_id
|
||||||
|
http:
|
||||||
|
port: 8080
|
||||||
|
timeout:
|
||||||
|
read: 30
|
||||||
|
write: 30
|
||||||
|
idle: 30
|
||||||
|
plugin:
|
||||||
|
root: /app/plugins
|
||||||
|
pluginZipPath: /mnt/gcs/plugins/plugins_bundle.zip
|
||||||
|
plugins:
|
||||||
|
- publisher Src version raw comp zip
|
||||||
|
- nopschemavalidator
|
||||||
|
- router
|
||||||
|
- nopsigner
|
||||||
|
- nopsignvalidator
|
||||||
|
- reqpreprocessor
|
||||||
|
- gcpAuthMdw
|
||||||
|
modules:
|
||||||
|
- name: reciever
|
||||||
|
type: transaction
|
||||||
|
path: /reciever
|
||||||
|
targetType: msgQ
|
||||||
|
plugin:
|
||||||
|
schemaValidator:
|
||||||
|
id: nopschemavalidator
|
||||||
|
signValidator:
|
||||||
|
id: nopsignvalidator
|
||||||
|
publisher:
|
||||||
|
id: publisher
|
||||||
|
config:
|
||||||
|
project: ondc-seller-dev
|
||||||
|
topic: bapNetworkReciever
|
||||||
|
router:
|
||||||
|
id: router
|
||||||
|
config:
|
||||||
|
routingConfigPath: /mnt/gcs/configs/bapRecieverRouting-config.yaml
|
||||||
|
preProcessors:
|
||||||
|
- id: reqpreprocessor
|
||||||
|
steps:
|
||||||
|
steps:
|
||||||
|
- addRoute
|
||||||
|
signValidate
|
||||||
|
-addRout
|
||||||
|
customValidate
|
||||||
|
- name: transactionCaller
|
||||||
|
path: /caller
|
||||||
|
targetType: "http"
|
||||||
|
plugin:
|
||||||
|
signer:
|
||||||
|
id: nopsigner
|
||||||
|
router:
|
||||||
|
id: router
|
||||||
|
config:
|
||||||
|
routingConfigPath: /mnt/gcs/configs/bapCallerRouting-config.yaml
|
||||||
|
preProcessors:
|
||||||
|
- id: reqpreprocessor
|
||||||
|
# postProcessors:
|
||||||
|
# - id: gcpAuthMdw
|
||||||
|
# config:
|
||||||
|
# audience: https://bpp-adapter-903496459467.asia-southeast1.run.app
|
||||||
|
# serviceAccount: 903496459467-compute@developer.gserviceaccount.com
|
||||||
3
config/bapCallerRouting-config.yaml
Normal file
3
config/bapCallerRouting-config.yaml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
routes:
|
||||||
|
- action: search
|
||||||
|
target: https://bpp-adapter-903496459467.asia-southeast1.run.app/reciever
|
||||||
63
config/bpp-local.yaml
Normal file
63
config/bpp-local.yaml
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
appName: "bppClientService"
|
||||||
|
log:
|
||||||
|
level: debug
|
||||||
|
destinations:
|
||||||
|
- type: stdout
|
||||||
|
context_keys:
|
||||||
|
- transaction_id
|
||||||
|
- message_id
|
||||||
|
http:
|
||||||
|
port: 8080
|
||||||
|
timeout:
|
||||||
|
read: 30
|
||||||
|
write: 30
|
||||||
|
idle: 30
|
||||||
|
plugin:
|
||||||
|
root: extracted/plugins
|
||||||
|
pluginZipPath: plugins_bundle.zip
|
||||||
|
plugins:
|
||||||
|
- publisher
|
||||||
|
- nopschemavalidator
|
||||||
|
- router
|
||||||
|
- nopsigner
|
||||||
|
- nopsignvalidator
|
||||||
|
- reqpreprocessor
|
||||||
|
- gcpAuthMdw
|
||||||
|
module:
|
||||||
|
modules:
|
||||||
|
- name: transactionReciever
|
||||||
|
path: /reciever
|
||||||
|
targetType: msgQ
|
||||||
|
plugin:
|
||||||
|
schemaValidator:
|
||||||
|
id: nopschemavalidator
|
||||||
|
signValidator:
|
||||||
|
id: nopsignValidator
|
||||||
|
publisher:
|
||||||
|
id: publisher
|
||||||
|
config:
|
||||||
|
project: ondc-seller-dev
|
||||||
|
topic: clientSideTopic
|
||||||
|
Router:
|
||||||
|
id: router
|
||||||
|
config:
|
||||||
|
routingConfigPath: configs/bppRecieverRouting-config.yaml
|
||||||
|
preProcessors:
|
||||||
|
- id: reqpreprocessor
|
||||||
|
- name: transactionCaller
|
||||||
|
path: /caller
|
||||||
|
targetType: "http"
|
||||||
|
plugin:
|
||||||
|
signer:
|
||||||
|
id: nopsigner
|
||||||
|
Router:
|
||||||
|
id: router
|
||||||
|
config:
|
||||||
|
routingConfigPath: configs/bppCallerRouting-config.yaml
|
||||||
|
preProcessors:
|
||||||
|
- id: reqpreprocessor
|
||||||
|
postProcessors:
|
||||||
|
- id: gcpAuthMdw
|
||||||
|
config:
|
||||||
|
audience: "target"
|
||||||
|
|
||||||
63
config/bpp.yaml
Normal file
63
config/bpp.yaml
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
appName: "bppClientService"
|
||||||
|
log:
|
||||||
|
level: debug
|
||||||
|
destinations:
|
||||||
|
- type: stdout
|
||||||
|
context_keys:
|
||||||
|
- transaction_id
|
||||||
|
- message_id
|
||||||
|
http:
|
||||||
|
port: 8080
|
||||||
|
timeout:
|
||||||
|
read: 30
|
||||||
|
write: 30
|
||||||
|
idle: 30
|
||||||
|
plugin:
|
||||||
|
root: /app/plugins
|
||||||
|
pluginZipPath: /mnt/gcs/plugins/plugins_bundle.zip
|
||||||
|
plugins:
|
||||||
|
- publisher
|
||||||
|
- nopschemavalidator
|
||||||
|
- router
|
||||||
|
- nopsigner
|
||||||
|
- nopsignvalidator
|
||||||
|
- reqpreprocessor
|
||||||
|
- gcpAuthMdw
|
||||||
|
module:
|
||||||
|
modules:
|
||||||
|
- name: transactionReciever
|
||||||
|
path: /reciever
|
||||||
|
targetType: msgQ
|
||||||
|
plugin:
|
||||||
|
schemaValidator:
|
||||||
|
id: nopschemavalidator
|
||||||
|
signValidator:
|
||||||
|
id: nopsignvalidator
|
||||||
|
publisher:
|
||||||
|
id: publisher
|
||||||
|
config:
|
||||||
|
project: ondc-seller-dev
|
||||||
|
topic: bppNetworkReciever
|
||||||
|
router:
|
||||||
|
id: router
|
||||||
|
config:
|
||||||
|
routingConfigPath: /mnt/gcs/configs/bppRecieverRouting-config.yaml
|
||||||
|
preProcessors:
|
||||||
|
- id: reqpreprocessor
|
||||||
|
- name: transactionCaller
|
||||||
|
path: /caller
|
||||||
|
targetType: "http"
|
||||||
|
plugin:
|
||||||
|
signer:
|
||||||
|
id: nopsigner
|
||||||
|
router:
|
||||||
|
id: router
|
||||||
|
config:
|
||||||
|
routingConfigPath: /mnt/gcs/configs/bppCallerRouting-config.yaml
|
||||||
|
preProcessors:
|
||||||
|
- id: reqpreprocessor
|
||||||
|
# postProcessors:
|
||||||
|
# - id: gcpAuthMdw
|
||||||
|
# config:
|
||||||
|
# audience: https://bap-adapter-903496459467.asia-southeast1.run.app
|
||||||
|
# serviceAccount: 903496459467-compute@developer.gserviceaccount.com
|
||||||
3
config/bppCallerRouting-config.yaml
Normal file
3
config/bppCallerRouting-config.yaml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
routes:
|
||||||
|
- action: on_search
|
||||||
|
target: targeturl
|
||||||
3
config/bppRecieverRouting-config.yaml
Normal file
3
config/bppRecieverRouting-config.yaml
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
routes:
|
||||||
|
- action: search
|
||||||
|
target: https://sellerapp-903496459467.asia-southeast1.run.app
|
||||||
2
config/byuerApp-config.yaml
Normal file
2
config/byuerApp-config.yaml
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
bap_url: "https://bap-csr-903496459467.asia-southeast1.run.app" # Replace with actual Beckn API endpoint
|
||||||
|
port: "8080" # The port on which the server will run
|
||||||
69
config/sellerData.yaml
Normal file
69
config/sellerData.yaml
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
- id: pooja-stores
|
||||||
|
descriptor:
|
||||||
|
name: Pooja Stores
|
||||||
|
locations:
|
||||||
|
- id: koramangala-4th-block-location
|
||||||
|
gps: "12.9349377,77.6055586"
|
||||||
|
categories:
|
||||||
|
- id: fresh_fruits
|
||||||
|
descriptor:
|
||||||
|
name: Fresh Fruits
|
||||||
|
- id: beverages
|
||||||
|
descriptor:
|
||||||
|
name: Beverages
|
||||||
|
items:
|
||||||
|
- id: item_2
|
||||||
|
descriptor:
|
||||||
|
name: Green Apples Organic
|
||||||
|
images:
|
||||||
|
- url: "https://mock_bpp.com/images/item_2.jpg"
|
||||||
|
category_id: fresh_fruits
|
||||||
|
location_id: koramangala-4th-block-location
|
||||||
|
price:
|
||||||
|
currency: INR
|
||||||
|
value: "170"
|
||||||
|
matched: true
|
||||||
|
- id: item_1
|
||||||
|
descriptor:
|
||||||
|
name: Red Apples
|
||||||
|
images:
|
||||||
|
- url: "https://mock_bpp.com/images/item_1.jpg"
|
||||||
|
category_id: fresh_fruits
|
||||||
|
location_id: koramangala-4th-block-location
|
||||||
|
price:
|
||||||
|
currency: INR
|
||||||
|
value: "90"
|
||||||
|
related: true
|
||||||
|
- id: item_7
|
||||||
|
descriptor:
|
||||||
|
name: Green Apple Juice
|
||||||
|
images:
|
||||||
|
- url: "https://mock_bpp.com/images/item_7.jpg"
|
||||||
|
category_id: beverages
|
||||||
|
location_id: koramangala-4th-block-location
|
||||||
|
price:
|
||||||
|
currency: INR
|
||||||
|
value: "70"
|
||||||
|
matched: true
|
||||||
|
- id: food-mall
|
||||||
|
descriptor:
|
||||||
|
name: Food Mall
|
||||||
|
locations:
|
||||||
|
- id: food-mall-location
|
||||||
|
gps: "12.9349377,77.6055586"
|
||||||
|
categories:
|
||||||
|
- id: fresh-food
|
||||||
|
descriptor:
|
||||||
|
name: Fresh food
|
||||||
|
items:
|
||||||
|
- id: item_1_1
|
||||||
|
descriptor:
|
||||||
|
name: Green Apple Salad
|
||||||
|
images:
|
||||||
|
- url: "https://mock_bpp.com/images/item_1_1.jpg"
|
||||||
|
category_id: fresh-food
|
||||||
|
location_id: food-mall-location
|
||||||
|
price:
|
||||||
|
currency: INR
|
||||||
|
value: "200"
|
||||||
|
matched: true
|
||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/beckn/beckn-onix/pkg/log"
|
|
||||||
"github.com/beckn/beckn-onix/pkg/model"
|
"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"
|
||||||
@@ -25,24 +24,12 @@ type PluginManager interface {
|
|||||||
SchemaValidator(ctx context.Context, cfg *plugin.Config) (definition.SchemaValidator, error)
|
SchemaValidator(ctx context.Context, cfg *plugin.Config) (definition.SchemaValidator, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Provider represents a function that initializes an HTTP handler using a PluginManager.
|
|
||||||
type Provider func(ctx context.Context, mgr PluginManager, cfg *Config) (http.Handler, error)
|
|
||||||
|
|
||||||
// Type defines different handler types for processing requests.
|
// Type defines different handler types for processing requests.
|
||||||
type Type string
|
type Type string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// HandlerTypeStd represents the standard handler type used for general request processing.
|
// HandlerTypeStd represents the standard handler type used for general request processing.
|
||||||
HandlerTypeStd Type = "std"
|
HandlerTypeStd Type = "std"
|
||||||
|
|
||||||
// HandlerTypeRegSub represents the registry subscriber handler type for handling registry subscription requests.
|
|
||||||
HandlerTypeRegSub Type = "regSub"
|
|
||||||
|
|
||||||
// HandlerTypeNPSub represents the network participant subscriber handler type for handling network participant subscription requests.
|
|
||||||
HandlerTypeNPSub Type = "npSub"
|
|
||||||
|
|
||||||
// HandlerTypeLookup represents the lookup handler type used for resolving service details.
|
|
||||||
HandlerTypeLookup Type = "lookUp"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// PluginCfg holds the configuration for various plugins.
|
// PluginCfg holds the configuration for various plugins.
|
||||||
@@ -108,13 +95,3 @@ func (s *Step) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|||||||
*s = step
|
*s = step
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DummyHandler is a basic HTTP handler that returns a fixed response.
|
|
||||||
func DummyHandler(ctx context.Context, mgr PluginManager, cfg *Config) (http.Handler, error) {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
if _, err := w.Write([]byte("Dummy Handler Response")); err != nil {
|
|
||||||
log.Error(context.Background(), err, "failed to write nack response")
|
|
||||||
}
|
|
||||||
}), nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -230,35 +230,6 @@ func (h *stdHandler) initSteps(ctx context.Context, mgr PluginManager, cfg *Conf
|
|||||||
steps[c.ID] = step
|
steps[c.ID] = step
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register processing steps
|
|
||||||
for _, step := range cfg.Steps {
|
|
||||||
var s definition.Step
|
|
||||||
var err error
|
|
||||||
|
|
||||||
switch step {
|
|
||||||
case "sign":
|
|
||||||
s, err = newSignStep(h.signer, h.km)
|
|
||||||
case "validateSign":
|
|
||||||
s, err = newValidateSignStep(h.signValidator, h.km)
|
|
||||||
case "validateSchema":
|
|
||||||
s, err = newValidateSchemaStep(h.schemaValidator)
|
|
||||||
case "addRoute":
|
|
||||||
s, err = newRouteStep(h.router)
|
|
||||||
case "broadcast":
|
|
||||||
s = &broadcastStep{}
|
|
||||||
default:
|
|
||||||
if customStep, exists := steps[step]; exists {
|
|
||||||
s = customStep
|
|
||||||
} else {
|
|
||||||
return fmt.Errorf("unrecognized step: %s", step)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
h.steps = append(h.steps, s)
|
|
||||||
}
|
|
||||||
log.Infof(ctx, "Processor steps initialized: %v", cfg.Steps)
|
log.Infof(ctx, "Processor steps initialized: %v", cfg.Steps)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -155,12 +155,3 @@ func (s *addRouteStep) Run(ctx *model.StepContext) error {
|
|||||||
log.Debugf(ctx, "ctx.Route to %#v", ctx.Route)
|
log.Debugf(ctx, "ctx.Route to %#v", ctx.Route)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// broadcastStep is a stub for broadcasting.
|
|
||||||
type broadcastStep struct{}
|
|
||||||
|
|
||||||
// Run is a placeholder for future implementation.
|
|
||||||
func (b *broadcastStep) Run(ctx *model.StepContext) error {
|
|
||||||
// TODO: Implement broadcast logic if needed
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -16,33 +16,22 @@ type Config struct {
|
|||||||
Handler handler.Config
|
Handler handler.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Provider represents a function that initializes an HTTP handler using a PluginManager.
|
||||||
|
type Provider func(ctx context.Context, mgr handler.PluginManager, cfg *handler.Config) (http.Handler, error)
|
||||||
|
|
||||||
// handlerProviders maintains a mapping of handler types to their respective providers.
|
// handlerProviders maintains a mapping of handler types to their respective providers.
|
||||||
var handlerProviders = map[handler.Type]handler.Provider{
|
var handlerProviders = map[handler.Type]Provider{
|
||||||
handler.HandlerTypeStd: handler.NewStdHandler,
|
handler.HandlerTypeStd: handler.NewStdHandler,
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDummyHandlerProviders returns a dummy handler provider mapping for testing purposes.
|
|
||||||
func GetDummyHandlerProviders() map[handler.Type]handler.Provider {
|
|
||||||
return map[handler.Type]handler.Provider{
|
|
||||||
handler.HandlerTypeStd: handler.DummyHandler,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// getHandlerProviders is a function to retrieve handler providers, which can be overridden for testing.
|
|
||||||
var getHandlerProviders = func() map[handler.Type]handler.Provider {
|
|
||||||
return handlerProviders
|
|
||||||
}
|
|
||||||
|
|
||||||
// Register initializes and registers handlers based on the provided configuration.
|
// Register initializes and registers handlers based on the provided configuration.
|
||||||
// It iterates over the module configurations, retrieves appropriate handler providers,
|
// It iterates over the module configurations, retrieves appropriate handler providers,
|
||||||
// and registers the handlers with the HTTP multiplexer.
|
// and registers the handlers with the HTTP multiplexer.
|
||||||
func Register(ctx context.Context, mCfgs []Config, mux *http.ServeMux, mgr handler.PluginManager) error {
|
func Register(ctx context.Context, mCfgs []Config, mux *http.ServeMux, mgr handler.PluginManager) error {
|
||||||
log.Debugf(ctx, "Registering modules with config: %#v", mCfgs)
|
log.Debugf(ctx, "Registering modules with config: %#v", mCfgs)
|
||||||
|
|
||||||
providers := getHandlerProviders()
|
|
||||||
// Iterate over the handlers in the configuration.
|
// Iterate over the handlers in the configuration.
|
||||||
for _, c := range mCfgs {
|
for _, c := range mCfgs {
|
||||||
rmp, ok := providers[c.Handler.Type]
|
rmp, ok := handlerProviders[c.Handler.Type]
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("invalid module : %s", c.Name)
|
return fmt.Errorf("invalid module : %s", c.Name)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user