refactor: add concept of a pre-init function that gets called before init

This commit is contained in:
Derrick Hammer 2024-02-17 05:16:24 -05:00
parent 0ac4d318b7
commit 997e362d90
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 10 additions and 5 deletions

View File

@ -12,8 +12,9 @@ import (
func RegisterProtocols() { func RegisterProtocols() {
registry.Register(registry.ProtocolEntry{ registry.Register(registry.ProtocolEntry{
Key: "s5", Key: "s5",
Module: s5.ProtocolModule, Module: s5.ProtocolModule,
PreInitFunc: s5.PreInit,
}) })
} }
@ -23,6 +24,9 @@ func BuildProtocols(config *viper.Viper) fx.Option {
for _, entry := range registry.GetRegistry() { for _, entry := range registry.GetRegistry() {
if lo.Contains(enabledProtocols, entry.Key) { if lo.Contains(enabledProtocols, entry.Key) {
options = append(options, entry.Module) options = append(options, entry.Module)
if entry.PreInitFunc != nil {
options = append(options, fx.Invoke(entry.PreInitFunc))
}
} }
} }

View File

@ -10,14 +10,15 @@ const GroupName = "protocols"
type Protocol interface { type Protocol interface {
Name() string Name() string
Init(...any) error Init() error
Start(ctx context.Context) error Start(ctx context.Context) error
Stop(ctx context.Context) error Stop(ctx context.Context) error
} }
type ProtocolEntry struct { type ProtocolEntry struct {
Key string Key string
Module fx.Option Module fx.Option
PreInitFunc interface{}
} }
var protocolEntry []ProtocolEntry var protocolEntry []ProtocolEntry