another big refactor to make code more manageable

This commit is contained in:
Drew Weymouth
2022-12-25 16:59:42 -08:00
parent 8a84128bc8
commit f792b473e8
9 changed files with 227 additions and 115 deletions
+41
View File
@@ -0,0 +1,41 @@
package backend
import (
"net/http"
subsonic "github.com/dweymouth/go-subsonic"
"github.com/google/uuid"
)
type ServerManager struct {
ServerID uuid.UUID
Server *subsonic.Client
onServerConnected []func()
}
func NewServerManager() *ServerManager {
return &ServerManager{}
}
func (s *ServerManager) ConnectToServer(conf *ServerConfig, password string) error {
cli := &subsonic.Client{
Client: &http.Client{},
BaseUrl: conf.Hostname,
User: conf.Username,
ClientName: "supersonic",
}
if err := cli.Authenticate(password); err != nil {
return err
}
s.Server = cli
s.ServerID = conf.ID
for _, cb := range s.onServerConnected {
cb()
}
return nil
}
func (s *ServerManager) OnServerConnected(cb func()) {
s.onServerConnected = append(s.onServerConnected, cb)
}