Skip to content

Commit d2fd15d

Browse files
committed
go comments
1 parent cbe69db commit d2fd15d

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

mpd-web-proxy/events.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ const (
1919
EventTypeMPD
2020
)
2121

22-
func startChannelListener(mpd net.Conn) chan string {
22+
// The MPD Idler uses the MPD idle command to receive
23+
// notifications when the state has changed on the MPD
24+
// server. It relays what has changed on the returned
25+
// channel.
26+
func startMPDIdler(mpd net.Conn) chan string {
2327
eventC := make(chan string)
2428
go func() {
25-
slog.Debug("start channel listener")
26-
defer slog.Debug("end channel listener")
29+
slog.Debug("start MPD Idler")
30+
defer slog.Debug("end MPD Idler")
2731
defer close(eventC)
2832
sc := bufio.NewScanner(mpd)
2933
// MPD Header
@@ -56,11 +60,19 @@ func startChannelListener(mpd net.Conn) chan string {
5660
return eventC
5761
}
5862

63+
// An Event is any asynchronous event
64+
// that should be sent to a listening client.
5965
type Event struct {
6066
Type EventType
6167
Data string
6268
}
6369

70+
// getEvents starts a goroutine which emits events
71+
// to send to the client. Events include:
72+
// - any string sent on the provided string channel is sent as an MPDEvent
73+
// - the emitter produces a "ping" event that clients can use as a heartbeat.
74+
// The emitter exits and closes the returned channel when the given context
75+
// is cancelled.
6476
func getEvents(ts chan string, ctx context.Context) chan Event {
6577
ret := make(chan Event)
6678
ticker := time.NewTicker(5 * time.Second)

mpd-web-proxy/main.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ func WriteBadRequest(rw http.ResponseWriter, msg string) {
9595
fmt.Fprintf(rw, "ACK: %s\n", msg)
9696
}
9797

98+
// Use server-sent events (SSE) to notify the client in
99+
// real time when the MPD server state has changed. Internally,
100+
// uses the mpd "idle" command and sends idle values as event
101+
// data to the client.
102+
//
103+
// Additionally, the endpoint will deliver "ping" events on
104+
// a set interval as a sort of heartbeat.
98105
func MpdEvents(rw http.ResponseWriter, req *http.Request) {
99106
slog.Info(fmt.Sprintf("%s %s", req.RemoteAddr, req.URL))
100107
defer slog.Info(fmt.Sprintf("%s %s CLIENT EXIT", req.RemoteAddr, req.URL))
@@ -113,7 +120,7 @@ func MpdEvents(rw http.ResponseWriter, req *http.Request) {
113120

114121
mpd := Must(net.Dial("tcp", MpdAuthority))
115122
defer mpd.Close()
116-
events := startChannelListener(mpd)
123+
events := startMPDIdler(mpd)
117124
for ev := range getEvents(events, req.Context()) {
118125
var eventPayload string
119126
switch ev.Type {
@@ -134,6 +141,8 @@ func MpdEvents(rw http.ResponseWriter, req *http.Request) {
134141
}
135142
}
136143

144+
// MpdCommand is an endpoint which sends the client query
145+
// to MPD and returns the MPD response in the HTTP body.
137146
func MpdCommand(rw http.ResponseWriter, req *http.Request) {
138147
qs, ok := req.URL.Query()["q"]
139148
if !ok {
@@ -145,6 +154,7 @@ func MpdCommand(rw http.ResponseWriter, req *http.Request) {
145154
rw.Write(data)
146155
}
147156

157+
// Returns the verson of the MPD server being used.
148158
func MpdVersion(rw http.ResponseWriter, req *http.Request) {
149159
conn := Must(net.Dial("tcp", MpdAuthority))
150160
defer conn.Close()
@@ -173,6 +183,8 @@ func chooseAFile(sc *bufio.Scanner) (string, error) {
173183
return "", sc.Err()
174184
}
175185

186+
// The AlbumArt endpoint searches in the album directory
187+
// or the ID3 tags in one of the track files for the album.
176188
func AlbumArt(rw http.ResponseWriter, req *http.Request) {
177189
conn := Must(net.Dial("tcp", MpdAuthority))
178190
defer conn.Close()
@@ -206,6 +218,7 @@ func AlbumArt(rw http.ResponseWriter, req *http.Request) {
206218
io.Copy(rw, bytes.NewReader(data))
207219
}
208220

221+
// Build the server.
209222
func httpServer(s *Server) {
210223
var nonEventMux http.ServeMux
211224
nonEventMux.HandleFunc("/go/version", func(w http.ResponseWriter, r *http.Request) {
@@ -222,6 +235,7 @@ func httpServer(s *Server) {
222235
http.ListenAndServe(BindAddr, nil)
223236
}
224237

238+
// Panic on any error.
225239
func Must[T any](t T, err error) T {
226240
if err != nil {
227241
panic(err)

mpd-web-proxy/mpd.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import (
1111
"strings"
1212
)
1313

14+
// MpdQuery connects and queries the MPD server
15+
// with the given command. Returns the response body
16+
// or any connection error.
1417
func MpdQuery(cmd string) ([]byte, error) {
1518
mpd, err := net.Dial("tcp", MpdAuthority)
1619
if err != nil {
@@ -20,6 +23,7 @@ func MpdQuery(cmd string) ([]byte, error) {
2023
return mpdQuery(cmd, mpd)
2124
}
2225

26+
// A cancellable MPD Query.
2327
func MpdQueryContext(cmd string, ctx context.Context) ([]byte, error) {
2428
mpd, err := net.Dial("tcp", MpdAuthority)
2529
if err != nil {
@@ -33,6 +37,8 @@ func MpdQueryContext(cmd string, ctx context.Context) ([]byte, error) {
3337
return mpdQuery(cmd, mpd)
3438
}
3539

40+
// mpdQuery reads and parses a response stream
41+
// to verify that the response is valid.
3642
func mpdQuery(cmd string, mpd io.ReadWriter) ([]byte, error) {
3743
scanner := bufio.NewScanner(mpd)
3844
if !scanner.Scan() {

0 commit comments

Comments
 (0)