diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 6c3236a..c37058b 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -20,6 +20,7 @@ + @@ -174,105 +175,12 @@ - true - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/app.yaml b/app.yaml index ffd6146..9f2da09 100644 --- a/app.yaml +++ b/app.yaml @@ -1,5 +1,5 @@ application: musicserver -version: 2.0.3 +version: 2.1.0 runtime: go115 api_version: go1 diff --git a/doc/swagger.yaml b/doc/swagger.yaml index ecb3fa0..007a14c 100644 --- a/doc/swagger.yaml +++ b/doc/swagger.yaml @@ -1,8 +1,8 @@ openapi: 3.0.3 info: - version: "2.0.3" + version: "2.1.0" title: "Music Server" - description: "Another small change that should fix the caching problem." + description: "Added /addQue to add the last received song to the songQue. Changed /rand and /rand/low to not add song to the que. Changed /next to not call /rand when the end of the que is reached, instead the last song in the que will be resent." contact: email: "zarnor91@gmail.com" servers: @@ -192,6 +192,20 @@ paths: description: "Bad Request" "500": description: "Something went wrong on the server" + /music/addQue: + get: + tags: + - "Music" + summary: "Adds last song to que" + description: "Adds the last featched song to the song que" + operationId: "addQue" + responses: + "200": + description: "OK" + "401": + description: "Bad Request" + "500": + description: "Something went wrong on the server" /music/reset: get: tags: diff --git a/indexFacade.go b/indexFacade.go new file mode 100644 index 0000000..303f095 --- /dev/null +++ b/indexFacade.go @@ -0,0 +1,91 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "os" +) + +func indexHandler(w http.ResponseWriter, r *http.Request) { + setCorsAndNoCacheHeaders(&w, r) + + if r.URL.Path == "/version" { + w.Header().Add("Content-Type", "application/json") + + testf() + + data := VersionData{Version: "2.1.0", + Changelog: "Added /addQue to add the last received song to the songQue. " + + "Changed /rand and /rand/low to not add song to the que. " + + "Changed /next to not call /rand when the end of the que is reached, instead the last song in the que will be resent.", + History: []VersionData{ + { + Version: "2.0.3", + Changelog: "Another small change that should fix the caching problem.", + }, + { + Version: "2.0.2", + Changelog: "Hopefully fixed the caching problem with random.", + }, + { + Version: "2.0.1", + Changelog: "Fixed CORS", + }, + { + Version: "2.0.0", + Changelog: "Rebuilt the application in Go.", + }, + { + Version: "1.2.0", + Changelog: "Made the /sync endpoint async. " + + "Fixed bug where the game list wasn't reloaded when using /reset. " + + "Fixed bug where the songNo showed in the list didn't match what should be sent.", + }, + { + Version: "1.1.0", + Changelog: "Added sync endpoint, don't really trust it to 100%, would say beta. " + + "Fixed bug with /next after /previous. Added /reset endpoint. " + + "Added some info more to /info and /list.", + }, + { + Version: "1.0.0", + Changelog: "Added swagger documentation. Created version 1.0.", + }, + { + Version: "0.5.5", + Changelog: "Added increase played endpoint.", + }, + }, + } + _ = json.NewEncoder(w).Encode(data) + + } else if r.URL.Path == "/doc" { + http.ServeFile(w, r, "./doc/swagger.yaml") + + } else if r.URL.Path == "/" { + rows, dbErr := dbPool.Query(context.Background(), "select game_name from game") + if dbErr != nil { + _, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", dbErr) + os.Exit(1) + } + for rows.Next() { + var gameName string + dbErr = rows.Scan(&gameName) + if dbErr != nil { + _, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", dbErr) + } + _, _ = fmt.Fprintf(w, "%v\n", gameName) + } + + _, err := fmt.Fprint(w, "Hello, World!!") + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + } + } else { + http.NotFound(w, r) + return + } + +} diff --git a/musicFacade.go b/musicFacade.go index 8846251..8665602 100644 --- a/musicFacade.go +++ b/musicFacade.go @@ -15,6 +15,7 @@ import ( var currentSong = -1 var games []GameData var songQue []SongData +var lastFetched SongData func getSoundCheckSong() string { reset() @@ -33,6 +34,12 @@ func reset() { games = findAllGames() } +func addLatestToQue() { + if lastFetched.path != "" { + songQue = append(songQue, lastFetched) + } +} + func getRandomSong() string { if games == nil || len(games) == 0 { games = findAllGames() @@ -45,7 +52,7 @@ func getRandomSong() string { song := songs[rand.Intn(len(songs))] currentSong = len(songQue) - songQue = append(songQue, song) + lastFetched = song return song.path } @@ -74,12 +81,15 @@ func getRandomSongLowChance() string { song := songs[rand.Intn(len(songs))] currentSong = len(songQue) - songQue = append(songQue, song) + lastFetched = song return song.path } func getSongInfo() SongInfo { + if songQue == nil { + return SongInfo{} + } var currentSongData = songQue[currentSong] currentGameData := getCurrentGame(currentSongData) @@ -144,8 +154,12 @@ func setPlayed(songNumber int) { } func getNextSong() string { + if songQue == nil { + return "" + } if currentSong == len(songQue)-1 || currentSong == -1 { - return getRandomSong() + var songData = songQue[currentSong] + return songData.path } else { currentSong = currentSong + 1 var songData = songQue[currentSong] @@ -154,6 +168,9 @@ func getNextSong() string { } func getPreviousSong() string { + if songQue == nil { + return "" + } if currentSong == -1 || currentSong == 0 { var songData = songQue[0] return songData.path @@ -177,12 +194,10 @@ func musicHandler(w http.ResponseWriter, r *http.Request) { } else { s := getSong(song) sendFile(w, r, s) - //http.ServeFile(w, r, s) } } else if r.URL.Path == "/music/first" && r.Method == http.MethodGet { song := getSoundCheckSong() sendFile(w, r, song) - //http.ServeFile(w, r, getSoundCheckSong()) } else if r.URL.Path == "/music/reset" && r.Method == http.MethodGet { reset() @@ -190,16 +205,10 @@ func musicHandler(w http.ResponseWriter, r *http.Request) { } else if r.URL.Path == "/music/rand" && r.Method == http.MethodGet { song := getRandomSong() - sendFile(w, r, song) - /*file, _ := os.Open(song) - seeker := io.ReadSeeker(file) - http.ServeContent(w, r, song, time.Time{}, seeker)*/ - } else if r.URL.Path == "/music/rand/low" && r.Method == http.MethodGet { chance := getRandomSongLowChance() - //http.ServeFile(w, r, chance) sendFile(w, r, chance) } else if r.URL.Path == "/music/info" && r.Method == http.MethodGet { @@ -212,12 +221,10 @@ func musicHandler(w http.ResponseWriter, r *http.Request) { } else if r.URL.Path == "/music/next" { song := getNextSong() - //http.ServeFile(w, r, song) sendFile(w, r, song) } else if r.URL.Path == "/music/previous" { song := getPreviousSong() - //http.ServeFile(w, r, song) sendFile(w, r, song) } else if r.URL.Path == "/music/all" && r.Method == http.MethodGet { @@ -233,6 +240,9 @@ func musicHandler(w http.ResponseWriter, r *http.Request) { } setPlayed(p.song) w.WriteHeader(http.StatusOK) + } else if r.URL.Path == "/music/addQue" && r.Method == http.MethodGet { + addLatestToQue() + w.WriteHeader(http.StatusOK) } } @@ -240,13 +250,13 @@ func sendFile(writer http.ResponseWriter, request *http.Request, Filename string fmt.Println("Client requests: " + Filename) //Check if file exists and open - Openfile, err := os.Open(Filename) - defer Openfile.Close() //Close after function return + openFile, err := os.Open(Filename) if err != nil { //File not found, send 404 http.Error(writer, "File not found.", 404) return } + defer openFile.Close() //Close after function return //File is found, create and send the correct headers @@ -254,12 +264,12 @@ func sendFile(writer http.ResponseWriter, request *http.Request, Filename string //Create a buffer to store the header of the file in FileHeader := make([]byte, 512) //Copy the headers into the FileHeader buffer - _, _ = Openfile.Read(FileHeader) + _, _ = openFile.Read(FileHeader) //Get content type of file //FileContentType := http.DetectContentType(FileHeader) //Get the file size - FileStat, _ := Openfile.Stat() //Get info from file + FileStat, _ := openFile.Stat() //Get info from file FileSize := strconv.FormatInt(FileStat.Size(), 10) //Get file size as a string //Send the headers @@ -269,8 +279,8 @@ func sendFile(writer http.ResponseWriter, request *http.Request, Filename string //Send the file //We read 512 bytes from the file already, so we reset the offset back to 0 - _, _ = Openfile.Seek(0, 0) - _, _ = io.Copy(writer, Openfile) //'Copy' the file to the client + _, _ = openFile.Seek(0, 0) + _, _ = io.Copy(writer, openFile) //'Copy' the file to the client return } diff --git a/musicserver.go b/musicserver.go index f7c71fd..b406e39 100644 --- a/musicserver.go +++ b/musicserver.go @@ -1,8 +1,6 @@ package main import ( - "context" - "encoding/json" "fmt" "github.com/gorilla/mux" "log" @@ -99,82 +97,6 @@ type SongData struct { timesPlayed int } -func indexHandler(w http.ResponseWriter, r *http.Request) { - setCorsAndNoCacheHeaders(&w, r) - - if r.URL.Path == "/version" { - w.Header().Add("Content-Type", "application/json") - - testf() - - data := VersionData{Version: "2.0.3", - Changelog: "Another small change that should fix the caching problem.", - History: []VersionData{ - { - Version: "2.0.2", - Changelog: "Hopefully fixed the caching problem with random.", - }, - { - Version: "2.0.1", - Changelog: "Fixed CORS", - }, - { - Version: "2.0.0", - Changelog: "Rebuilt the application in Go.", - }, - { - Version: "1.2.0", - Changelog: "Made the /sync endpoint async. " + - "Fixed bug where the game list wasn't reloaded when using /reset." + - "Fixed bug where the songNo showed in the list didn't match what should be sent.", - }, - { - Version: "1.1.0", - Changelog: "Added sync endpoint, don't really trust it to 100%, would say beta. " + - "Fixed bug with /next after /previous. Added /reset endpoint. " + - "Added some info more to /info and /list.", - }, - { - Version: "1.0.0", - Changelog: "Added swagger documentation. Created version 1.0.", - }, - { - Version: "0.5.5", - Changelog: "Added increase played endpoint.", - }, - }, - } - _ = json.NewEncoder(w).Encode(data) - - } else if r.URL.Path == "/doc" { - http.ServeFile(w, r, "./doc/swagger.yaml") - - } else if r.URL.Path == "/" { - rows, dbErr := dbPool.Query(context.Background(), "select game_name from game") - if dbErr != nil { - _, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", dbErr) - os.Exit(1) - } - for rows.Next() { - var gameName string - dbErr = rows.Scan(&gameName) - if dbErr != nil { - _, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", dbErr) - } - _, _ = fmt.Fprintf(w, "%v\n", gameName) - } - - _, err := fmt.Fprint(w, "Hello, World!!") - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - } - } else { - http.NotFound(w, r) - return - } - -} - func setCorsAndNoCacheHeaders(w *http.ResponseWriter, r *http.Request) { var etagHeaders = []string{ "ETag",