From 997e362d90116ee19f783fb90b2378b3f84eecd8 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 17 Feb 2024 05:16:24 -0500 Subject: [PATCH] refactor: add concept of a pre-init function that gets called before init --- protocols/protocols.go | 8 ++++++-- protocols/registry/registry.go | 7 ++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/protocols/protocols.go b/protocols/protocols.go index 2ef5dc2..1ab1099 100644 --- a/protocols/protocols.go +++ b/protocols/protocols.go @@ -12,8 +12,9 @@ import ( func RegisterProtocols() { registry.Register(registry.ProtocolEntry{ - Key: "s5", - Module: s5.ProtocolModule, + Key: "s5", + Module: s5.ProtocolModule, + PreInitFunc: s5.PreInit, }) } @@ -23,6 +24,9 @@ func BuildProtocols(config *viper.Viper) fx.Option { for _, entry := range registry.GetRegistry() { if lo.Contains(enabledProtocols, entry.Key) { options = append(options, entry.Module) + if entry.PreInitFunc != nil { + options = append(options, fx.Invoke(entry.PreInitFunc)) + } } } diff --git a/protocols/registry/registry.go b/protocols/registry/registry.go index 223af97..085dad1 100644 --- a/protocols/registry/registry.go +++ b/protocols/registry/registry.go @@ -10,14 +10,15 @@ const GroupName = "protocols" type Protocol interface { Name() string - Init(...any) error + Init() error Start(ctx context.Context) error Stop(ctx context.Context) error } type ProtocolEntry struct { - Key string - Module fx.Option + Key string + Module fx.Option + PreInitFunc interface{} } var protocolEntry []ProtocolEntry