- add predictable / vanity ids

- add / route
This commit is contained in:
Sören Oesterwind 2022-10-13 12:47:44 +02:00
parent f3e07d3f4d
commit 1af34dfcf3
2 changed files with 22 additions and 1 deletions

View File

@ -90,6 +90,7 @@ func main() {
http.HandleFunc("/v1/stopAll", handleStopAll)
http.HandleFunc("/v1/current", handleCurrent)
http.HandleFunc("/v1/list", handleListing)
http.HandleFunc("/", handleRoot)
fmt.Println("Listening on port " + fmt.Sprint(configuration.Port))
log.Fatal(http.ListenAndServe(":"+fmt.Sprint(configuration.Port), nil))

View File

@ -34,6 +34,8 @@ func handlePlay(w http.ResponseWriter, r *http.Request) {
loopBool = true
}
wantedId := r.URL.Query().Get("id") // Retrieve the id value from the query string, it's optional
t, err := os.Stat("./sounds/" + string(bytArr[:])) // Check if the file exists
if errors.Is(err, os.ErrNotExist) {
w.WriteHeader(400)
@ -61,6 +63,20 @@ func handlePlay(w http.ResponseWriter, r *http.Request) {
}
var currIndex = len(playbacks) // Create a new index for the playback
if len(wantedId) > 0 { // If the id is set, check if it is already in use
id, err := strconv.Atoi(wantedId)
if err != nil {
fmt.Fprintf(w, "{\"status\":\"fail\", \"reason\":\"id is not a number\"}")
return
}
if _, ok := playbacks[id]; ok {
fmt.Fprintf(w, "{\"status\":\"fail\", \"reason\":\"id is already in use\"}")
return
}
currIndex = id
}
fmt.Fprintf(w, "{\"status\":\"ok\", \"id\":%d}", currIndex) // Return a JSON object to the user
go PlaySound(string(bytArr[:]), currIndex, loopBool) // Play the sound
@ -267,3 +283,7 @@ func handleListing(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, string(j))
}
}
func handleRoot(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Soundr is running.")
}