diff options
| author | Physick <96335032+DegustatorPonos@users.noreply.github.com> | 2025-12-06 12:05:43 +0500 |
|---|---|---|
| committer | Physick <96335032+DegustatorPonos@users.noreply.github.com> | 2025-12-06 12:05:43 +0500 |
| commit | 5fd81183f54662fdf08549c0d635e80166c62fe9 (patch) | |
| tree | 0b730dd4f19c7446fabcc2ee7cc7313e94e67fe3 /Endpoints/Audio.go | |
initial endpoints
Diffstat (limited to 'Endpoints/Audio.go')
| -rw-r--r-- | Endpoints/Audio.go | 46 |
1 files changed, 46 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) + } +} |
