mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
initial commit
This commit is contained in:
43
router.go
Normal file
43
router.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package showbridge
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Router struct {
|
||||
Context context.Context
|
||||
ProtocolInstances []Protocol
|
||||
}
|
||||
|
||||
func NewRouter(ctx context.Context, config Config) (*Router, error) {
|
||||
|
||||
router := Router{
|
||||
Context: ctx,
|
||||
ProtocolInstances: []Protocol{},
|
||||
}
|
||||
|
||||
for _, protocolDecl := range config.Protocols {
|
||||
|
||||
protocolInfo, ok := protocolRegistry[protocolDecl.Type]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("problem loading protocol registration for protocol type: %s", protocolDecl.Type)
|
||||
}
|
||||
|
||||
protocolInstance, err := protocolInfo.New(protocolDecl.Params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
router.ProtocolInstances = append(router.ProtocolInstances, protocolInstance)
|
||||
|
||||
}
|
||||
|
||||
return &router, nil
|
||||
}
|
||||
|
||||
func (r *Router) Run() {
|
||||
for _, protocolInstance := range r.ProtocolInstances {
|
||||
protocolInstance.Run(r.Context)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user