/
notification.go
55 lines (44 loc) · 1.18 KB
/
notification.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package notifier
import (
"net/http"
log "github.com/sirupsen/logrus"
)
const (
defaultSubtitle = "%s start streaming %s"
actionURI = "/notification" // Use URI not handle notifications callback
serverListenAddr = "localhost"
streamQueryParameter = "id"
)
// Notifier service
type Notifier interface {
// Notify send a desktop notification
Notify(username, game, id string) error
// Close stops the current notifier service (closes the underlying web server)
Close() error
}
// service implements Notifier
type service struct {
title string // Notification application title
out chan<- string // send notifications click events
srv *http.Server // server which handle notification click callbacks
}
// New creates a new notifier service and output channel for notification callback events
func New(title string) (Notifier, <-chan string) {
out := make(chan string)
var s = &service{
title: title,
out: out,
srv: nil,
}
s.startServer()
return s, out
}
func (s *service) Close() error {
close(s.out)
// server does not exist
if s.srv == nil {
return nil
}
log.Debugln("notification service: closing web server")
return s.srv.Close()
}