summaryrefslogtreecommitdiff
path: root/Endpoints
diff options
context:
space:
mode:
authorPhysick <96335032+DegustatorPonos@users.noreply.github.com>2025-12-06 12:05:43 +0500
committerPhysick <96335032+DegustatorPonos@users.noreply.github.com>2025-12-06 12:05:43 +0500
commit5fd81183f54662fdf08549c0d635e80166c62fe9 (patch)
tree0b730dd4f19c7446fabcc2ee7cc7313e94e67fe3 /Endpoints
initial endpoints
Diffstat (limited to 'Endpoints')
-rw-r--r--Endpoints/Audio.go46
-rw-r--r--Endpoints/Common.go8
-rw-r--r--Endpoints/SongList.go56
3 files changed, 110 insertions, 0 deletions
diff --git a/Endpoints/Audio.go b/Endpoints/Audio.go
new file mode 100644
index 0000000..f9dca20
--- /dev/null
+++ b/Endpoints/Audio.go
@@ -0,0 +1,46 @@
+package endpoints
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "log"
+ "net/http"
+ "os"
+
+ settings "physick.ru/culture_exam/Settings"
+)
+
+type getErr struct {
+ Error string
+}
+
+func (base getErr) 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 getSong(w http.ResponseWriter, r *http.Request) {
+ var songName = r.URL.Query().Get("name")
+ if len(songName) == 0 {
+ fmt.Fprint(w, getErr { Error: "No song name provided" })
+ w.WriteHeader(http.StatusBadRequest)
+ return
+ }
+ var fullPath = fmt.Sprintf("%s/%s", settings.Current.SongsLocation, songName)
+ var file, fopenErr = os.ReadFile(fullPath)
+ if fopenErr != nil {
+ fmt.Fprint(w, getErr { Error: fopenErr.Error()})
+ w.WriteHeader(http.StatusBadRequest)
+ return
+ }
+ w.Header().Add("Content-Type", "audio/mpeg")
+ var audioChunks = bytes.NewBuffer(file)
+
+ if _, err := audioChunks.WriteTo(w); err != nil {
+ log.Println(err)
+ }
+}
diff --git a/Endpoints/Common.go b/Endpoints/Common.go
new file mode 100644
index 0000000..b30afd0
--- /dev/null
+++ b/Endpoints/Common.go
@@ -0,0 +1,8 @@
+package endpoints
+
+import "net/http"
+
+func RegisterEndpoints() {
+ http.HandleFunc("/api/getSongs", getSongs)
+ http.HandleFunc("/api/getSong", getSong)
+}
diff --git a/Endpoints/SongList.go b/Endpoints/SongList.go
new file mode 100644
index 0000000..9830c74
--- /dev/null
+++ b/Endpoints/SongList.go
@@ -0,0 +1,56 @@
+package endpoints
+
+import (
+ "encoding/json"
+ "fmt"
+ "log"
+ "net/http"
+ "os"
+
+ audio "physick.ru/culture_exam/Audio"
+ settings "physick.ru/culture_exam/Settings"
+)
+
+type Songs = struct {
+ Name string
+ Duration int
+}
+
+type SongsList struct {
+ Songs []Songs
+}
+
+func (base SongsList) 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 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())
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+ var outp = SongsList {
+ Songs: make([]Songs, 0, len(files)),
+ }
+ for _, v := range files {
+ var fullPath = fmt.Sprintf("%s/%s", settings.Current.SongsLocation, v.Name())
+ var duration, durErr = audio.GetSongDuration(fullPath)
+ if durErr != nil {
+ log.Printf("Failed to parse file metadata: File: %s, error: %s\n", fullPath, durErr.Error())
+ continue
+ }
+ var song = Songs {
+ Name: v.Name(),
+ Duration: int(duration.Seconds()),
+ }
+ outp.Songs = append(outp.Songs, song)
+ }
+ w.Header().Add("Content-Type", "application/json")
+ fmt.Fprint(w, outp)
+}