Skip to content

Commit 4af7200

Browse files
committed
fix: fix crash when API server is disabled
Closes #159
1 parent 30c81e8 commit 4af7200

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

cmd/daemon/api_server.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ import (
2222

2323
const timeout = 10 * time.Second
2424

25-
type ApiServer struct {
25+
type ApiServer interface {
26+
Emit(ev *ApiEvent)
27+
Receive() <-chan ApiRequest
28+
}
29+
30+
type ConcreteApiServer struct {
2631
log librespot.Logger
2732

2833
allowOrigin string
@@ -270,8 +275,8 @@ type ApiEventDataShuffleContext struct {
270275
Value bool `json:"value"`
271276
}
272277

273-
func NewApiServer(log librespot.Logger, address string, port int, allowOrigin string, certFile string, keyFile string) (_ *ApiServer, err error) {
274-
s := &ApiServer{log: log, allowOrigin: allowOrigin, certFile: certFile, keyFile: keyFile}
278+
func NewApiServer(log librespot.Logger, address string, port int, allowOrigin string, certFile string, keyFile string) (_ ApiServer, err error) {
279+
s := &ConcreteApiServer{log: log, allowOrigin: allowOrigin, certFile: certFile, keyFile: keyFile}
275280
s.requests = make(chan ApiRequest)
276281

277282
s.listener, err = net.Listen("tcp", fmt.Sprintf("%s:%d", address, port))
@@ -285,13 +290,23 @@ func NewApiServer(log librespot.Logger, address string, port int, allowOrigin st
285290
return s, nil
286291
}
287292

288-
func NewStubApiServer() (*ApiServer, error) {
289-
s := &ApiServer{}
290-
s.requests = make(chan ApiRequest)
291-
return s, nil
293+
type StubApiServer struct {
294+
log librespot.Logger
295+
}
296+
297+
func NewStubApiServer(log librespot.Logger) (ApiServer, error) {
298+
return &StubApiServer{log: log}, nil
299+
}
300+
301+
func (s *StubApiServer) Emit(ev *ApiEvent) {
302+
s.log.Tracef("voiding websocket event: %s", ev.Type)
303+
}
304+
305+
func (s *StubApiServer) Receive() <-chan ApiRequest {
306+
return make(<-chan ApiRequest)
292307
}
293308

294-
func (s *ApiServer) handleRequest(req ApiRequest, w http.ResponseWriter) {
309+
func (s *ConcreteApiServer) handleRequest(req ApiRequest, w http.ResponseWriter) {
295310
req.resp = make(chan apiResponse, 1)
296311
s.requests <- req
297312
resp := <-req.resp
@@ -333,7 +348,7 @@ func (s *ApiServer) handleRequest(req ApiRequest, w http.ResponseWriter) {
333348
}
334349
}
335350

336-
func (s *ApiServer) serve() {
351+
func (s *ConcreteApiServer) serve() {
337352
m := http.NewServeMux()
338353
m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
339354
w.Header().Set("Content-Type", "application/json")
@@ -603,7 +618,7 @@ func (s *ApiServer) serve() {
603618
}
604619
}
605620

606-
func (s *ApiServer) Emit(ev *ApiEvent) {
621+
func (s *ConcreteApiServer) Emit(ev *ApiEvent) {
607622
s.clientsLock.RLock()
608623
defer s.clientsLock.RUnlock()
609624

@@ -620,11 +635,11 @@ func (s *ApiServer) Emit(ev *ApiEvent) {
620635
}
621636
}
622637

623-
func (s *ApiServer) Receive() <-chan ApiRequest {
638+
func (s *ConcreteApiServer) Receive() <-chan ApiRequest {
624639
return s.requests
625640
}
626641

627-
func (s *ApiServer) Close() {
642+
func (s *ConcreteApiServer) Close() {
628643
s.close = true
629644

630645
// close all websocket clients

cmd/daemon/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type App struct {
4949
state *AppState
5050
stateLock sync.Mutex
5151

52-
server *ApiServer
52+
server ApiServer
5353
logoutCh chan *AppPlayer
5454
}
5555

@@ -598,7 +598,7 @@ func main() {
598598
log.WithError(err).Fatal("failed creating api server")
599599
}
600600
} else {
601-
app.server, _ = NewStubApiServer()
601+
app.server, _ = NewStubApiServer(app.log)
602602
}
603603

604604
ctx := context.TODO()

0 commit comments

Comments
 (0)