Skip to content

Commit

Permalink
fix(notification): Run server only if supported
Browse files Browse the repository at this point in the history
  • Loading branch information
SkYNewZ committed Dec 8, 2021
1 parent 0aa8855 commit d237b5d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 43 deletions.
44 changes: 1 addition & 43 deletions pkg/notifier/notification.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package notifier

import (
"errors"
"fmt"
"net/http"
"sync"

"github.com/phayes/freeport"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -44,16 +41,7 @@ func New(title string) (Notifier, <-chan string) {
srv: nil,
}

once.Do(func() {
port, err := freeport.GetFreePort()
if err != nil {
log.Errorf("notification service: cannot find free port, notifications click will not works: %s", err)
return
}

s.startServer(port)
})

s.startServer()
return s, out
}

Expand All @@ -72,33 +60,3 @@ func (s *service) Close() error {

return nil
}

func (s *service) startServer(port int) {
log.Tracef("notification service: using port %d", port)
http.HandleFunc(actionURI, s.handleNotificationClick)
s.srv = &http.Server{
Addr: fmt.Sprintf("%s:%d", serverListenAddr, port),
Handler: nil, // let use http.DefaultServeMux
}

// start server
go func() {
log.Debugf("notification service: starting web server for notification click callblack at %s", s.srv.Addr)
if err := s.srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Errorf("notification service: fail to start web server: %s", err)
}
}()
}

// handleNotificationClick receives notification click events and
// send the streamer name to the service channel output
func (s *service) handleNotificationClick(_ http.ResponseWriter, r *http.Request) {
stream := r.URL.Query().Get(streamQueryParameter)
if stream == "" {
log.Warningf("received notification callback without '%s' key", streamQueryParameter)
return
}

log.Tracef("notification service: received notification event [%s]", stream)
s.out <- stream
}
3 changes: 3 additions & 0 deletions pkg/notifier/notification_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ import (
func (s *service) Notify(username, game, _ string) error {
return beeep.Notify(s.title, fmt.Sprintf(defaultSubtitle, username, game), "")
}

// startServer notification callback handler is not supported on darwin as it runs a AppleScript
func (s *service) startServer() {}
3 changes: 3 additions & 0 deletions pkg/notifier/notification_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ var ErrUnsupported = errors.New("notification service: unsupported operation sys
func (s *service) Notify(username, game, id string) error {
return ErrUnsupported
}

// startServer notification callback handler is not supported
func (s *service) startServer() {}
40 changes: 40 additions & 0 deletions pkg/notifier/notification_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,43 @@ func (s *service) makeNotificationURL(streamer string) string {
u.RawQuery = q.Encode()
return u.String()
}

// startServer starts a web server to receive click callback events
func (s *service) startServer() {
var port int
once.Do(func() {
port, err = freeport.GetFreePort()
if err != nil {
log.Errorf("notification service: cannot find free port, notifications click will not works: %s", err)
return
}
})

log.Tracef("notification service: using port %d", port)
http.HandleFunc(actionURI, s.handleNotificationClick)
s.srv = &http.Server{
Addr: fmt.Sprintf("%s:%d", serverListenAddr, port),
Handler: nil, // let use http.DefaultServeMux
}

// start server
go func() {
log.Debugf("notification service: starting web server for notification click callblack at %s", s.srv.Addr)
if err := s.srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Errorf("notification service: fail to start web server: %s", err)
}
}()
}

// handleNotificationClick receives notification click events and
// send the streamer name to the service channel output
func (s *service) handleNotificationClick(_ http.ResponseWriter, r *http.Request) {
stream := r.URL.Query().Get(streamQueryParameter)
if stream == "" {
log.Warningf("received notification callback without '%s' key", streamQueryParameter)
return
}

log.Tracef("notification service: received notification event [%s]", stream)
s.out <- stream
}

0 comments on commit d237b5d

Please sign in to comment.