Small refactor (#148)
* small refactor * extra line * fix room allocator test * selector fakes not used * keep decisions out of router * put nodeId logic back * fix room allocator test
This commit is contained in:
+89
-8
@@ -6,8 +6,17 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/livekit/livekit-server/pkg/config"
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/protocol/auth"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/utils"
|
||||
"github.com/livekit/protocol/webhook"
|
||||
"github.com/pkg/errors"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Injectors from wire.go:
|
||||
@@ -17,29 +26,28 @@ func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*Live
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
router := createRouter(client, currentNode)
|
||||
nodeSelector, err := CreateNodeSelector(conf)
|
||||
router := routing.CreateRouter(conf, client, currentNode)
|
||||
roomStore := createStore(client)
|
||||
roomAllocator, err := NewRoomAllocator(conf, router, roomStore)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
roomStore := createStore(client)
|
||||
roomAllocator := NewRoomAllocator(conf, router, nodeSelector, roomStore)
|
||||
roomService, err := NewRoomService(roomAllocator, roomStore, router)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
messageBus := createMessageBus(client)
|
||||
keyProvider, err := CreateKeyProvider(conf)
|
||||
keyProvider, err := createKeyProvider(conf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
notifier, err := CreateWebhookNotifier(conf, keyProvider)
|
||||
notifier, err := createWebhookNotifier(conf, keyProvider)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
recordingService := NewRecordingService(messageBus, notifier)
|
||||
rtcService := NewRTCService(conf, roomAllocator, router, currentNode)
|
||||
localRoomManager, err := NewLocalRoomManager(roomStore, router, currentNode, nodeSelector, notifier, conf)
|
||||
localRoomManager, err := NewLocalRoomManager(conf, roomStore, router, currentNode, notifier)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -60,6 +68,79 @@ func InitializeRouter(conf *config.Config, currentNode routing.LocalNode) (routi
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
router := createRouter(client, currentNode)
|
||||
router := routing.CreateRouter(conf, client, currentNode)
|
||||
return router, nil
|
||||
}
|
||||
|
||||
// wire.go:
|
||||
|
||||
func createKeyProvider(conf *config.Config) (auth.KeyProvider, error) {
|
||||
|
||||
if conf.KeyFile != "" {
|
||||
if st, err := os.Stat(conf.KeyFile); err != nil {
|
||||
return nil, err
|
||||
} else if st.Mode().Perm() != 0600 {
|
||||
return nil, fmt.Errorf("key file must have permission set to 600")
|
||||
}
|
||||
f, err := os.Open(conf.KeyFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
_ = f.Close()
|
||||
}()
|
||||
return auth.NewFileBasedKeyProviderFromReader(f)
|
||||
}
|
||||
|
||||
if len(conf.Keys) == 0 {
|
||||
return nil, errors.New("one of key-file or keys must be provided in order to support a secure installation")
|
||||
}
|
||||
|
||||
return auth.NewFileBasedKeyProviderFromMap(conf.Keys), nil
|
||||
}
|
||||
|
||||
func createWebhookNotifier(conf *config.Config, provider auth.KeyProvider) (webhook.Notifier, error) {
|
||||
wc := conf.WebHook
|
||||
if len(wc.URLs) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
secret := provider.GetSecret(wc.APIKey)
|
||||
if secret == "" {
|
||||
return nil, ErrWebHookMissingAPIKey
|
||||
}
|
||||
|
||||
return webhook.NewNotifier(wc.APIKey, secret, wc.URLs), nil
|
||||
}
|
||||
|
||||
func createRedisClient(conf *config.Config) (*redis.Client, error) {
|
||||
if !conf.HasRedis() {
|
||||
return nil, nil
|
||||
}
|
||||
logger.Infow("using multi-node routing via redis", "addr", conf.Redis.Address)
|
||||
rc := redis.NewClient(&redis.Options{
|
||||
Addr: conf.Redis.Address,
|
||||
Username: conf.Redis.Username,
|
||||
Password: conf.Redis.Password,
|
||||
DB: conf.Redis.DB,
|
||||
})
|
||||
if err := rc.Ping(context.Background()).Err(); err != nil {
|
||||
err = errors.Wrap(err, "unable to connect to redis")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rc, nil
|
||||
}
|
||||
|
||||
func createMessageBus(rc *redis.Client) utils.MessageBus {
|
||||
if rc == nil {
|
||||
return nil
|
||||
}
|
||||
return utils.NewRedisMessageBus(rc)
|
||||
}
|
||||
|
||||
func createStore(rc *redis.Client) RoomStore {
|
||||
if rc != nil {
|
||||
return NewRedisRoomStore(rc)
|
||||
}
|
||||
return NewLocalRoomStore()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user