Compare commits

...

3 commits

Author SHA1 Message Date
XenGi ab01f8f329
🔥 2024-07-15 20:00:18 +02:00
XenGi 5474934cea
add docstrings to go code 2024-07-15 10:51:53 +02:00
XenGi 1705de7391
add docstrings to go code 2024-07-15 10:49:26 +02:00
8 changed files with 38 additions and 13 deletions

View file

@ -1,4 +1,4 @@
FROM docker.io/golang:1.20 as builder FROM docker.io/golang:1.22 as builder
WORKDIR /app WORKDIR /app

View file

@ -28,11 +28,11 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1710154385, "lastModified": 1717050755,
"narHash": "sha256-4c3zQ2YY4BZOufaBJB4v9VBBeN2dH7iVdoJw8SDNCfI=", "narHash": "sha256-C9IEHABulv2zEDFA+Bf0E1nmfN4y6MIUe5eM2RCrDC0=",
"owner": "tweag", "owner": "tweag",
"repo": "gomod2nix", "repo": "gomod2nix",
"rev": "872b63ddd28f318489c929d25f1f0a3c6039c971", "rev": "31b6d2e40b36456e792cd6cf50d5a8ddd2fa59a1",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -43,11 +43,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1713284584, "lastModified": 1720955038,
"narHash": "sha256-rRuPBJD9+yjz7tY3xC/BvFUwloutynR9piiVE6fhGqo=", "narHash": "sha256-GaliJqfFwyYxReFywxAa8orCO+EnDq2NK2F+5aSc8vo=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "2b6ee326ad047870526d9a3ae88dfd0197da898d", "rev": "aa247c0c90ecf4ae7a032c54fdc21b91ca274062",
"type": "github" "type": "github"
}, },
"original": { "original": {

View file

@ -39,4 +39,3 @@
} }
); );
} }

2
go.mod
View file

@ -1,6 +1,6 @@
module gitlab.com/XenGi/sanic module gitlab.com/XenGi/sanic
go 1.21 go 1.22
require ( require (
github.com/fhs/gompd/v2 v2.3.0 github.com/fhs/gompd/v2 v2.3.0

19
mpd.go
View file

@ -11,6 +11,7 @@ import (
// MPD API calls // MPD API calls
// updateDb Updates the music database: find new files, remove deleted files, update modified files.
func updateDb(c echo.Context) error { func updateDb(c echo.Context) error {
// Connect to MPD server // Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:6600") 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)) 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 { func previousTrack(c echo.Context) error {
// Connect to MPD server // Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:6600") 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") return c.String(http.StatusOK, "Playing previous track in queue")
} }
// nextTrack Plays next song in the queue.
func nextTrack(c echo.Context) error { func nextTrack(c echo.Context) error {
// Connect to MPD server // Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:6600") 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") return c.String(http.StatusOK, "PLaying next track in queue")
} }
// stopPlayback Stops playing.
func stopPlayback(c echo.Context) error { func stopPlayback(c echo.Context) error {
// Connect to MPD server // Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:6600") conn, err := mpd.Dial("tcp", "localhost:6600")
@ -75,6 +79,7 @@ func stopPlayback(c echo.Context) error {
return c.String(http.StatusOK, "Playback stopped") return c.String(http.StatusOK, "Playback stopped")
} }
// resumePlayback Begins playing the playlist or if paused resumes playback.
func resumePlayback(c echo.Context) error { func resumePlayback(c echo.Context) error {
// Connect to MPD server // Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:6600") conn, err := mpd.Dial("tcp", "localhost:6600")
@ -102,6 +107,7 @@ func resumePlayback(c echo.Context) error {
return c.String(http.StatusOK, "Playback resumed") return c.String(http.StatusOK, "Playback resumed")
} }
// pausePlayback Pauses playback.
func pausePlayback(c echo.Context) error { func pausePlayback(c echo.Context) error {
// Connect to MPD server // Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:6600") conn, err := mpd.Dial("tcp", "localhost:6600")
@ -118,6 +124,7 @@ func pausePlayback(c echo.Context) error {
return c.String(http.StatusOK, "Playback paused") 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 { func seek(c echo.Context) error {
// Connect to MPD server // Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:6600") 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)) 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 { func toggleRepeat(c echo.Context) error {
// Connect to MPD server // Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:6600") conn, err := mpd.Dial("tcp", "localhost:6600")
@ -171,6 +179,7 @@ func toggleRepeat(c echo.Context) error {
return c.String(http.StatusOK, msg) return c.String(http.StatusOK, msg)
} }
// toggleRandom Toggles random state between 1 or 0.
func toggleRandom(c echo.Context) error { func toggleRandom(c echo.Context) error {
// Connect to MPD server // Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:6600") conn, err := mpd.Dial("tcp", "localhost:6600")
@ -198,6 +207,7 @@ func toggleRandom(c echo.Context) error {
return c.String(http.StatusOK, msg) return c.String(http.StatusOK, msg)
} }
// setVolume Sets volume to level, the range of volume is 0-100.
func setVolume(c echo.Context) error { func setVolume(c echo.Context) error {
// Connect to MPD server // Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:6600") conn, err := mpd.Dial("tcp", "localhost:6600")
@ -225,6 +235,7 @@ func setVolume(c echo.Context) error {
// Queue // Queue
// deleteTrackFromQueue removed track with song_id from queue
func deleteTrackFromQueue(c echo.Context) error { func deleteTrackFromQueue(c echo.Context) error {
// Connect to MPD server // Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:6600") 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)) 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 { func moveTrackInQueue(c echo.Context) error {
// Connect to MPD server // Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:6600") 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)) 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 { func attachPlaylist(c echo.Context) error {
// Connect to MPD server // Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:6600") conn, err := mpd.Dial("tcp", "localhost:6600")
@ -293,6 +306,7 @@ func attachPlaylist(c echo.Context) error {
return c.JSON(http.StatusOK, "") return c.JSON(http.StatusOK, "")
} }
// replaceQueue replaces the current queue with the playlist with the name playlist_name.
func replaceQueue(c echo.Context) error { func replaceQueue(c echo.Context) error {
// Connect to MPD server // Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:6600") conn, err := mpd.Dial("tcp", "localhost:6600")
@ -319,6 +333,7 @@ func replaceQueue(c echo.Context) error {
// Playlists // Playlists
// listPlaylists return a list of all stored playlists.
func listPlaylists(c echo.Context) error { func listPlaylists(c echo.Context) error {
// Connect to MPD server // Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:6600") conn, err := mpd.Dial("tcp", "localhost:6600")
@ -336,6 +351,7 @@ func listPlaylists(c echo.Context) error {
return c.JSON(http.StatusOK, playlists) return c.JSON(http.StatusOK, playlists)
} }
// listPlaylist returns the contents of the playlist defined by name.
func listPlaylist(c echo.Context) error { func listPlaylist(c echo.Context) error {
// Connect to MPD server // Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:6600") conn, err := mpd.Dial("tcp", "localhost:6600")
@ -355,6 +371,7 @@ func listPlaylist(c echo.Context) error {
return c.JSON(http.StatusOK, playlist) return c.JSON(http.StatusOK, playlist)
} }
// deletePlaylist deletes the playlist defined by name.
func deletePlaylist(c echo.Context) error { func deletePlaylist(c echo.Context) error {
// Connect to MPD server // Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:6600") conn, err := mpd.Dial("tcp", "localhost:6600")
@ -374,6 +391,7 @@ func deletePlaylist(c echo.Context) error {
return c.String(http.StatusNoContent, "") return c.String(http.StatusNoContent, "")
} }
// savePlaylist saves the current queue to a playlist with the given name.
func savePlaylist(c echo.Context) error { func savePlaylist(c echo.Context) error {
// Connect to MPD server // Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:6600") conn, err := mpd.Dial("tcp", "localhost:6600")
@ -393,6 +411,7 @@ func savePlaylist(c echo.Context) error {
return c.String(http.StatusCreated, "") 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 { func searchDatabase(c echo.Context) error {
// Connect to MPD server // Connect to MPD server
conn, err := mpd.Dial("tcp", "localhost:6600") conn, err := mpd.Dial("tcp", "localhost:6600")

View file

@ -16,6 +16,7 @@ import (
"strings" "strings"
) )
// Config holds the configuration for the mpd connection and for the web server.
type Config struct { type Config struct {
MPD struct { MPD struct {
Hostname string `ini:"hostname"` Hostname string `ini:"hostname"`
@ -112,6 +113,7 @@ func main() {
} }
} }
// wsServe handles websocket connections.
func wsServe(c echo.Context) error { func wsServe(c echo.Context) error {
websocket.Handler(func(ws *websocket.Conn) { websocket.Handler(func(ws *websocket.Conn) {
defer ws.Close() defer ws.Close()
@ -182,6 +184,7 @@ func wsServe(c echo.Context) error {
return nil return nil
} }
// downloadTrack tries to download a given URL and saves the song to the database.
func downloadTrack(c echo.Context) error { func downloadTrack(c echo.Context) error {
// yt-dlp \ // yt-dlp \
// --no-wait-for-video \ // --no-wait-for-video \

View file

@ -200,7 +200,8 @@
<option value="1">basedrive</option><!-- TODO: Remove this line --> <option value="1">basedrive</option><!-- TODO: Remove this line -->
</select><!--/#control-playlist-list--> </select><!--/#control-playlist-list-->
<div> <div>
<button id="control-replace-playlist">&#x1F504; Replace</button><!-- 🔄 Counterclockwise Arrows Button --> <button id="control-refresh-playlists">&#x1F504; Refresh</button><!-- 🔄 Counterclockwise Arrows Button -->
<button id="control-replace-playlist">&#x2934;&#xFE0F; Replace</button><!-- ⤴️ Arrow Pointing Rightwards Then Curving Upwards -->
<button id="control-attach-playlist">&#x2B06; Attach</button><!-- ⬆️ Up Arrow --> <button id="control-attach-playlist">&#x2B06; Attach</button><!-- ⬆️ Up Arrow -->
<button id="control-save-playlist">&#x1F4BE; Save</button><!-- 💾 Floppy Disk --> <button id="control-save-playlist">&#x1F4BE; Save</button><!-- 💾 Floppy Disk -->
<button id="control-delete-playlist">&#x1F5D1;&#xFE0F; Delete</button><!-- 🗑️ Wastebasket --> <button id="control-delete-playlist">&#x1F5D1;&#xFE0F; Delete</button><!-- 🗑️ Wastebasket -->

View file

@ -33,6 +33,7 @@ const tab_browser = document.getElementById("tab-browser");
const tab_search = document.getElementById("tab-search"); const tab_search = document.getElementById("tab-search");
const tab_playlists = document.getElementById("tab-playlists"); const tab_playlists = document.getElementById("tab-playlists");
const control_playlist_list = document.getElementById("control-playlist-list"); 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_replace_playlist = document.getElementById("control-replace-playlist");
const control_attach_playlist = document.getElementById("control-attach-playlist"); const control_attach_playlist = document.getElementById("control-attach-playlist");
const control_save_playlist = document.getElementById("control-save-playlist"); const control_save_playlist = document.getElementById("control-save-playlist");
@ -83,9 +84,7 @@ refreshPlaylists = () => {
option.value = p["playlist"]; option.value = p["playlist"];
option.addEventListener("click", () => { option.addEventListener("click", () => {
fetch(`${API_URL}/playlists/${p["playlist"]}`).then(async r => { fetch(`${API_URL}/playlists/${p["playlist"]}`).then(async r => {
if (r.status === 200) { if (r.status === 200) fillResultTable(await r.json());
fillResultTable(await r.json());
}
}) })
}); });
control_playlist_list.appendChild(option) 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", () => { control_replace_playlist.addEventListener("click", () => {
fetch(`${API_URL}/queue/replace/${control_playlist_list.value}`).then(async r => { fetch(`${API_URL}/queue/replace/${control_playlist_list.value}`).then(async r => {
if (r.status !== 200) { if (r.status !== 200) {