diff options
| author | Physick <96335032+DegustatorPonos@users.noreply.github.com> | 2025-12-06 12:53:43 +0500 |
|---|---|---|
| committer | Physick <96335032+DegustatorPonos@users.noreply.github.com> | 2025-12-06 12:53:43 +0500 |
| commit | 4b8ef84059e772ef7636c451356680ec5b0983c4 (patch) | |
| tree | 8cabc9b8c275128fd6582cbd74d36212e49776d6 | |
| parent | 5fd81183f54662fdf08549c0d635e80166c62fe9 (diff) | |
Random song endpoint
| -rw-r--r-- | Endpoints/Common.go | 1 | ||||
| -rw-r--r-- | Endpoints/SongList.go | 48 | ||||
| -rw-r--r-- | static/index.html | 4 | ||||
| -rw-r--r-- | 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 @@ <body> <h1> xdx </h1> <button onclick="GetSongs()"> Get songs </button> + <button onclick="SetRandomSong()"> Set random song </button> + <audio id="mainAudio"> + <source type="audio/mpeg" src=""/> + </audio> </body> </html> diff --git a/static/index.js b/static/index.js index 9f5070b..b7056b1 100644 --- a/static/index.js +++ b/static/index.js @@ -1,5 +1,16 @@ + function GetSongs() { fetch("/api/getSongs").then(r => r.json().then(body => { console.log(body); })); } + +function SetRandomSong() { + fetch("/api/randomSong").then(r => r.json().then(body => { + const player = document.getElementById("mainAudio"); + console.log(player); + console.log(body); + player.src = "/api/getSong?name=" + encodeURI(body.Name); + player.play(); + })); +} |
