summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Endpoints/Common.go1
-rw-r--r--Endpoints/SongList.go48
-rw-r--r--static/index.html4
-rw-r--r--static/index.js11
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();
+ }));
+}