From 4b8ef84059e772ef7636c451356680ec5b0983c4 Mon Sep 17 00:00:00 2001 From: Physick <96335032+DegustatorPonos@users.noreply.github.com> Date: Sat, 6 Dec 2025 12:53:43 +0500 Subject: Random song endpoint --- Endpoints/Common.go | 1 + Endpoints/SongList.go | 48 +++++++++++++++++++++++++++++++++++++++--------- static/index.html | 4 ++++ static/index.js | 11 +++++++++++ 4 files changed, 55 insertions(+), 9 deletions(-) diff --git a/Endpoints/Common.go b/Endpoints/Common.go index b30afd0..50ffa25 100644 --- a/Endpoints/Common.go +++ b/Endpoints/Common.go @@ -4,5 +4,6 @@ import "net/http" func RegisterEndpoints() { http.HandleFunc("/api/getSongs", getSongs) + http.HandleFunc("/api/randomSong", getRandomSong) http.HandleFunc("/api/getSong", getSong) } diff --git a/Endpoints/SongList.go b/Endpoints/SongList.go index 9830c74..fe9ad60 100644 --- a/Endpoints/SongList.go +++ b/Endpoints/SongList.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "log" + "math/rand" "net/http" "os" @@ -11,13 +12,22 @@ import ( settings "physick.ru/culture_exam/Settings" ) -type Songs = struct { +type Song struct { Name string Duration int + Start int } type SongsList struct { - Songs []Songs + Songs []Song +} + +func (base Song) String() string { + var outp, jsonErr = json.Marshal(base) + if jsonErr != nil { + return fmt.Sprintf("Failed to parse settings: %s", jsonErr.Error()) + } + return string(outp) } func (base SongsList) String() string { @@ -29,14 +39,35 @@ func (base SongsList) String() string { } func getSongs(w http.ResponseWriter, r *http.Request) { - var files, dirErr = os.ReadDir(settings.Current.SongsLocation) - if dirErr != nil { - log.Printf("Failed to look up songs list: %s", dirErr.Error()) + var songsList, err = getAvaliableSongs() + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + log.Printf("Failed to get files: %v\n", err.Error()) + return + } + w.Header().Add("Content-Type", "application/json") + fmt.Fprint(w, songsList) +} + +func getRandomSong(w http.ResponseWriter, r *http.Request) { + var songsList, err = getAvaliableSongs() + if err != nil { w.WriteHeader(http.StatusInternalServerError) + log.Printf("Failed to get files: %v\n", err.Error()) return } + var index = rand.Intn(len(songsList.Songs)) + w.Header().Add("Content-Type", "application/json") + fmt.Fprint(w, songsList.Songs[index]) +} + +func getAvaliableSongs() (SongsList, error) { + var files, dirErr = os.ReadDir(settings.Current.SongsLocation) + if dirErr != nil { + return SongsList{}, dirErr + } var outp = SongsList { - Songs: make([]Songs, 0, len(files)), + Songs: make([]Song, 0, len(files)), } for _, v := range files { var fullPath = fmt.Sprintf("%s/%s", settings.Current.SongsLocation, v.Name()) @@ -45,12 +76,11 @@ func getSongs(w http.ResponseWriter, r *http.Request) { log.Printf("Failed to parse file metadata: File: %s, error: %s\n", fullPath, durErr.Error()) continue } - var song = Songs { + var song = Song { Name: v.Name(), Duration: int(duration.Seconds()), } outp.Songs = append(outp.Songs, song) } - w.Header().Add("Content-Type", "application/json") - fmt.Fprint(w, outp) + return outp, nil } diff --git a/static/index.html b/static/index.html index 8f1caf6..f6127d7 100644 --- a/static/index.html +++ b/static/index.html @@ -5,5 +5,9 @@