-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkri.go
112 lines (91 loc) · 2.13 KB
/
kri.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package krigoapp
import (
"net/http"
"sync"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
)
// Event is the update information sent over websocket
type Event struct {
Name string `json:"name"`
Data string `json:"content"`
}
// Server ...
type Server struct {
mu sync.Mutex
upgrader websocket.Upgrader
close chan int
// Server underlying http server
Server *http.Server
// WindowTitle current title of the selected window
WindowTitle string
// Thumbnail is a URL to the thumbnail of the currently playing song
ThumbnailURL string
// VideoURL is the URL of the current song
VideoURL string
CurrentTime float64
Duration float64
// Directory the server will serve files from
Servedir string
}
// NewServer creates a new server with the default settings
func NewServer(servedir string, addr string) *Server {
s := &Server{
Servedir: servedir,
}
r := mux.NewRouter()
r.HandleFunc("/ws/", s.wsHandler)
r.HandleFunc("/update", s.UpdateHandler)
r.PathPrefix("/").Handler(http.FileServer(http.Dir(servedir)))
s.Server = &http.Server{
Addr: addr,
Handler: r,
}
return s
}
// SetWindowTitle sets the currently tracked window title
func (s *Server) SetWindowTitle(title string) {
s.mu.Lock()
s.WindowTitle = title
s.mu.Unlock()
}
// SetThumbnailURL sets the thumbnail URL of the server.
func (s *Server) SetThumbnailURL(URL string) {
s.mu.Lock()
s.ThumbnailURL = URL
s.mu.Unlock()
}
// SetVideoURL sets the video URL
func (s *Server) SetVideoURL(URL string) {
s.mu.Lock()
s.VideoURL = URL
s.mu.Unlock()
}
// SetCurrentTime sets the current time
func (s *Server) SetCurrentTime(t float64) {
s.mu.Lock()
s.CurrentTime = t
s.mu.Unlock()
}
// SetDuration sets the duration
func (s *Server) SetDuration(t float64) {
s.mu.Lock()
s.Duration = t
s.mu.Unlock()
}
// Start begins listening for connections and hotkeys
func (s *Server) Start() error {
s.mu.Lock()
s.close = make(chan int)
s.mu.Unlock()
return s.Server.ListenAndServe()
}
// Close closes the running server
func (s *Server) Close() error {
s.mu.Lock()
if s.close != nil {
close(s.close)
}
s.mu.Unlock()
return s.Server.Close()
}