diff --git a/Dockerfile b/Dockerfile index 05fcdfc..0b51d8f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM docker.io/golang:1.20 as builder +FROM docker.io/golang:1.22 as builder WORKDIR /app diff --git a/flake.lock b/flake.lock index b110849..c570239 100644 --- a/flake.lock +++ b/flake.lock @@ -28,11 +28,11 @@ ] }, "locked": { - "lastModified": 1710154385, - "narHash": "sha256-4c3zQ2YY4BZOufaBJB4v9VBBeN2dH7iVdoJw8SDNCfI=", + "lastModified": 1717050755, + "narHash": "sha256-C9IEHABulv2zEDFA+Bf0E1nmfN4y6MIUe5eM2RCrDC0=", "owner": "tweag", "repo": "gomod2nix", - "rev": "872b63ddd28f318489c929d25f1f0a3c6039c971", + "rev": "31b6d2e40b36456e792cd6cf50d5a8ddd2fa59a1", "type": "github" }, "original": { @@ -43,11 +43,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1713284584, - "narHash": "sha256-rRuPBJD9+yjz7tY3xC/BvFUwloutynR9piiVE6fhGqo=", + "lastModified": 1720955038, + "narHash": "sha256-GaliJqfFwyYxReFywxAa8orCO+EnDq2NK2F+5aSc8vo=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "2b6ee326ad047870526d9a3ae88dfd0197da898d", + "rev": "aa247c0c90ecf4ae7a032c54fdc21b91ca274062", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 1338416..bb94005 100644 --- a/flake.nix +++ b/flake.nix @@ -39,4 +39,3 @@ } ); } - diff --git a/go.mod b/go.mod index 52f168e..5a4ffed 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module gitlab.com/XenGi/sanic -go 1.21 +go 1.22 require ( github.com/fhs/gompd/v2 v2.3.0 diff --git a/mpd.go b/mpd.go index 8a0e416..5e4cf37 100644 --- a/mpd.go +++ b/mpd.go @@ -11,6 +11,7 @@ import ( // MPD API calls +// updateDb Updates the music database: find new files, remove deleted files, update modified files. func updateDb(c echo.Context) error { // Connect to MPD server conn, err := mpd.Dial("tcp", "localhost:6600") @@ -27,6 +28,7 @@ func updateDb(c echo.Context) error { return c.String(http.StatusOK, fmt.Sprintf("Database update started with job id %d", jobId)) } +// previousTrack Plays previous song in the queue. func previousTrack(c echo.Context) error { // Connect to MPD server conn, err := mpd.Dial("tcp", "localhost:6600") @@ -43,6 +45,7 @@ func previousTrack(c echo.Context) error { return c.String(http.StatusOK, "Playing previous track in queue") } +// nextTrack Plays next song in the queue. func nextTrack(c echo.Context) error { // Connect to MPD server conn, err := mpd.Dial("tcp", "localhost:6600") @@ -59,6 +62,7 @@ func nextTrack(c echo.Context) error { return c.String(http.StatusOK, "PLaying next track in queue") } +// stopPlayback Stops playing. func stopPlayback(c echo.Context) error { // Connect to MPD server conn, err := mpd.Dial("tcp", "localhost:6600") @@ -75,6 +79,7 @@ func stopPlayback(c echo.Context) error { return c.String(http.StatusOK, "Playback stopped") } +// resumePlayback Begins playing the playlist or if paused resumes playback. func resumePlayback(c echo.Context) error { // Connect to MPD server conn, err := mpd.Dial("tcp", "localhost:6600") @@ -102,6 +107,7 @@ func resumePlayback(c echo.Context) error { return c.String(http.StatusOK, "Playback resumed") } +// pausePlayback Pauses playback. func pausePlayback(c echo.Context) error { // Connect to MPD server conn, err := mpd.Dial("tcp", "localhost:6600") @@ -118,6 +124,7 @@ func pausePlayback(c echo.Context) error { return c.String(http.StatusOK, "Playback paused") } +// seek Seeks to the position defined by seconds within the current song. func seek(c echo.Context) error { // Connect to MPD server conn, err := mpd.Dial("tcp", "localhost:6600") @@ -144,6 +151,7 @@ func seek(c echo.Context) error { return c.String(http.StatusOK, fmt.Sprintf("Seeked current track to %d seconds", seconds)) } +// toggleRepeat Toggles repeat state between 1 or 0. func toggleRepeat(c echo.Context) error { // Connect to MPD server conn, err := mpd.Dial("tcp", "localhost:6600") @@ -171,6 +179,7 @@ func toggleRepeat(c echo.Context) error { return c.String(http.StatusOK, msg) } +// toggleRandom Toggles random state between 1 or 0. func toggleRandom(c echo.Context) error { // Connect to MPD server conn, err := mpd.Dial("tcp", "localhost:6600") @@ -198,6 +207,7 @@ func toggleRandom(c echo.Context) error { return c.String(http.StatusOK, msg) } +// setVolume Sets volume to level, the range of volume is 0-100. func setVolume(c echo.Context) error { // Connect to MPD server conn, err := mpd.Dial("tcp", "localhost:6600") @@ -225,6 +235,7 @@ func setVolume(c echo.Context) error { // Queue +// deleteTrackFromQueue removed track with song_id from queue func deleteTrackFromQueue(c echo.Context) error { // Connect to MPD server conn, err := mpd.Dial("tcp", "localhost:6600") @@ -247,6 +258,7 @@ func deleteTrackFromQueue(c echo.Context) error { return c.String(http.StatusOK, fmt.Sprintf("Removed song %d from queue", songId)) } +// moveTrackInQueue moves song with song_id to the new place position in the queue. func moveTrackInQueue(c echo.Context) error { // Connect to MPD server conn, err := mpd.Dial("tcp", "localhost:6600") @@ -274,6 +286,7 @@ func moveTrackInQueue(c echo.Context) error { return c.String(http.StatusOK, fmt.Sprintf("Moved song %d to position %d", songId, position)) } +// attachPlaylist adds the playlist with the name playlist_name to the queue. func attachPlaylist(c echo.Context) error { // Connect to MPD server conn, err := mpd.Dial("tcp", "localhost:6600") @@ -293,6 +306,7 @@ func attachPlaylist(c echo.Context) error { return c.JSON(http.StatusOK, "") } +// replaceQueue replaces the current queue with the playlist with the name playlist_name. func replaceQueue(c echo.Context) error { // Connect to MPD server conn, err := mpd.Dial("tcp", "localhost:6600") @@ -319,6 +333,7 @@ func replaceQueue(c echo.Context) error { // Playlists +// listPlaylists return a list of all stored playlists. func listPlaylists(c echo.Context) error { // Connect to MPD server conn, err := mpd.Dial("tcp", "localhost:6600") @@ -336,6 +351,7 @@ func listPlaylists(c echo.Context) error { return c.JSON(http.StatusOK, playlists) } +// listPlaylist returns the contents of the playlist defined by name. func listPlaylist(c echo.Context) error { // Connect to MPD server conn, err := mpd.Dial("tcp", "localhost:6600") @@ -355,6 +371,7 @@ func listPlaylist(c echo.Context) error { return c.JSON(http.StatusOK, playlist) } +// deletePlaylist deletes the playlist defined by name. func deletePlaylist(c echo.Context) error { // Connect to MPD server conn, err := mpd.Dial("tcp", "localhost:6600") @@ -374,6 +391,7 @@ func deletePlaylist(c echo.Context) error { return c.String(http.StatusNoContent, "") } +// savePlaylist saves the current queue to a playlist with the given name. func savePlaylist(c echo.Context) error { // Connect to MPD server conn, err := mpd.Dial("tcp", "localhost:6600") @@ -393,6 +411,7 @@ func savePlaylist(c echo.Context) error { return c.String(http.StatusCreated, "") } +// searchDatabase search the database path given by pattern and returns all entries that contain the pattern either in their artist, album or title. func searchDatabase(c echo.Context) error { // Connect to MPD server conn, err := mpd.Dial("tcp", "localhost:6600") diff --git a/server.go b/server.go index d145897..ae2e3c8 100644 --- a/server.go +++ b/server.go @@ -16,6 +16,7 @@ import ( "strings" ) +// Config holds the configuration for the mpd connection and for the web server. type Config struct { MPD struct { Hostname string `ini:"hostname"` @@ -112,6 +113,7 @@ func main() { } } +// wsServe handles websocket connections. func wsServe(c echo.Context) error { websocket.Handler(func(ws *websocket.Conn) { defer ws.Close() @@ -182,6 +184,7 @@ func wsServe(c echo.Context) error { return nil } +// downloadTrack tries to download a given URL and saves the song to the database. func downloadTrack(c echo.Context) error { // yt-dlp \ // --no-wait-for-video \ diff --git a/static/index.html b/static/index.html index 15584db..31b34ce 100644 --- a/static/index.html +++ b/static/index.html @@ -200,7 +200,8 @@
- + + diff --git a/static/index.js b/static/index.js index dcc6a1e..c2c7c37 100644 --- a/static/index.js +++ b/static/index.js @@ -33,6 +33,7 @@ const tab_browser = document.getElementById("tab-browser"); const tab_search = document.getElementById("tab-search"); const tab_playlists = document.getElementById("tab-playlists"); const control_playlist_list = document.getElementById("control-playlist-list"); +const control_refresh_playlists = document.getElementById("control-refresh-playlists"); const control_replace_playlist = document.getElementById("control-replace-playlist"); const control_attach_playlist = document.getElementById("control-attach-playlist"); const control_save_playlist = document.getElementById("control-save-playlist"); @@ -83,9 +84,7 @@ refreshPlaylists = () => { option.value = p["playlist"]; option.addEventListener("click", () => { fetch(`${API_URL}/playlists/${p["playlist"]}`).then(async r => { - if (r.status === 200) { - fillResultTable(await r.json()); - } + if (r.status === 200) fillResultTable(await r.json()); }) }); control_playlist_list.appendChild(option) @@ -172,6 +171,10 @@ control_search_submit.addEventListener("click", event => { }) }); +control_refresh_playlists.addEventListener("click", () => { + refreshPlaylists(); +}); + control_replace_playlist.addEventListener("click", () => { fetch(`${API_URL}/queue/replace/${control_playlist_list.value}`).then(async r => { if (r.status !== 200) {