@@ -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.
98105func 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.
137146func 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.
148158func 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.
176188func 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.
209222func 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.
225239func Must [T any ](t T , err error ) T {
226240 if err != nil {
227241 panic (err )
0 commit comments