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.

This commit is contained in:
2020-12-06 17:20:11 +01:00
parent cda32daacf
commit 601e5cd14d
6 changed files with 140 additions and 195 deletions

98
.idea/workspace.xml generated
View File

@@ -20,6 +20,7 @@
</component>
<component name="ChangeListManager">
<list default="true" id="d639f8a9-4b6d-4957-9ba4-8361acac15ca" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/indexFacade.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/app.yaml" beforeDir="false" afterPath="$PROJECT_DIR$/app.yaml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/musicFacade.go" beforeDir="false" afterPath="$PROJECT_DIR$/musicFacade.go" afterDir="false" />
@@ -174,105 +175,12 @@
<MESSAGE value="Small fix in Dockerfile" />
<MESSAGE value="Fixed CORS" />
<MESSAGE value="Hopefully fixed the caching problem with random" />
<option name="LAST_COMMIT_MESSAGE" value="Hopefully fixed the caching problem with random" />
<MESSAGE value="Another small change that should fix the caching problem." />
<option name="LAST_COMMIT_MESSAGE" value="Another small change that should fix the caching problem." />
</component>
<component name="VgoProject">
<integration-enabled>true</integration-enabled>
</component>
<component name="WindowStateProjectService">
<state x="432" y="337" key="#GOROOT" timestamp="1606056684244">
<screen x="0" y="0" width="1280" height="800" />
</state>
<state x="432" y="337" key="#GOROOT/0.0.1280.800@0.0.1280.800" timestamp="1606056684244" />
<state x="440" y="240" key="#Go_Modules" timestamp="1602878032740">
<screen x="0" y="0" width="1280" height="800" />
</state>
<state x="440" y="240" key="#Go_Modules/0.0.1280.800@0.0.1280.800" timestamp="1602878032740" />
<state x="209" y="65" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1606500738678">
<screen x="0" y="0" width="1280" height="800" />
</state>
<state x="209" y="65" key="#com.intellij.execution.impl.EditConfigurationsDialog/0.0.1280.800@0.0.1280.800" timestamp="1606500738678" />
<state x="354" y="206" key="#com.intellij.fileTypes.FileTypeChooser" timestamp="1606327049349">
<screen x="0" y="0" width="1280" height="800" />
</state>
<state x="354" y="206" key="#com.intellij.fileTypes.FileTypeChooser/0.0.1280.800@0.0.1280.800" timestamp="1606327049349" />
<state x="63" y="25" width="1120" height="702" key="CommitChangelistDialog2" timestamp="1606935043680">
<screen x="0" y="0" width="1280" height="800" />
</state>
<state x="63" y="25" width="1120" height="702" key="CommitChangelistDialog2/0.0.1280.800@0.0.1280.800" timestamp="1606935043680" />
<state x="500" y="304" key="Github.ShareDialog" timestamp="1602604919305">
<screen x="0" y="0" width="1280" height="800" />
</state>
<state x="500" y="304" key="Github.ShareDialog/0.0.1280.800@0.0.1280.800" timestamp="1602604919305" />
<state x="454" y="112" key="Github.UntrackedFilesDialog" timestamp="1602604943862">
<screen x="0" y="0" width="1280" height="800" />
</state>
<state x="454" y="112" key="Github.UntrackedFilesDialog/0.0.1280.800@0.0.1280.800" timestamp="1602604943862" />
<state width="1238" height="212" key="GridCell.Tab.0.bottom" timestamp="1606935784051">
<screen x="0" y="0" width="1280" height="800" />
</state>
<state width="1238" height="212" key="GridCell.Tab.0.bottom/0.0.1280.800@0.0.1280.800" timestamp="1606935784051" />
<state width="1238" height="212" key="GridCell.Tab.0.bottom/0.25.1280.702@0.25.1280.702" timestamp="1606934333706" />
<state width="1238" height="212" key="GridCell.Tab.0.center" timestamp="1606935784048">
<screen x="0" y="0" width="1280" height="800" />
</state>
<state width="1238" height="212" key="GridCell.Tab.0.center/0.0.1280.800@0.0.1280.800" timestamp="1606935784048" />
<state width="1238" height="212" key="GridCell.Tab.0.center/0.25.1280.702@0.25.1280.702" timestamp="1606934333705" />
<state width="1238" height="212" key="GridCell.Tab.0.left" timestamp="1606935784047">
<screen x="0" y="0" width="1280" height="800" />
</state>
<state width="1238" height="212" key="GridCell.Tab.0.left/0.0.1280.800@0.0.1280.800" timestamp="1606935784047" />
<state width="1238" height="212" key="GridCell.Tab.0.left/0.25.1280.702@0.25.1280.702" timestamp="1606934333703" />
<state width="1238" height="212" key="GridCell.Tab.0.right" timestamp="1606935784049">
<screen x="0" y="0" width="1280" height="800" />
</state>
<state width="1238" height="212" key="GridCell.Tab.0.right/0.0.1280.800@0.0.1280.800" timestamp="1606935784049" />
<state width="1238" height="212" key="GridCell.Tab.0.right/0.25.1280.702@0.25.1280.702" timestamp="1606934333706" />
<state width="1238" height="264" key="GridCell.Tab.1.bottom" timestamp="1606934171754">
<screen x="0" y="25" width="1280" height="702" />
</state>
<state width="1238" height="264" key="GridCell.Tab.1.bottom/0.0.1280.800@0.0.1280.800" timestamp="1606852913985" />
<state width="1238" height="264" key="GridCell.Tab.1.bottom/0.25.1280.702@0.25.1280.702" timestamp="1606934171754" />
<state width="1238" height="264" key="GridCell.Tab.1.center" timestamp="1606934171752">
<screen x="0" y="25" width="1280" height="702" />
</state>
<state width="1238" height="264" key="GridCell.Tab.1.center/0.0.1280.800@0.0.1280.800" timestamp="1606852913984" />
<state width="1238" height="264" key="GridCell.Tab.1.center/0.25.1280.702@0.25.1280.702" timestamp="1606934171752" />
<state width="1238" height="264" key="GridCell.Tab.1.left" timestamp="1606934171752">
<screen x="0" y="25" width="1280" height="702" />
</state>
<state width="1238" height="264" key="GridCell.Tab.1.left/0.0.1280.800@0.0.1280.800" timestamp="1606852913984" />
<state width="1238" height="264" key="GridCell.Tab.1.left/0.25.1280.702@0.25.1280.702" timestamp="1606934171752" />
<state width="1238" height="264" key="GridCell.Tab.1.right" timestamp="1606934171753">
<screen x="0" y="25" width="1280" height="702" />
</state>
<state width="1238" height="264" key="GridCell.Tab.1.right/0.0.1280.800@0.0.1280.800" timestamp="1606852913985" />
<state width="1238" height="264" key="GridCell.Tab.1.right/0.25.1280.702@0.25.1280.702" timestamp="1606934171753" />
<state x="491" y="322" key="VCS.ChangelistChooser" timestamp="1604854207192">
<screen x="0" y="0" width="1280" height="800" />
</state>
<state x="491" y="322" key="VCS.ChangelistChooser/0.0.1280.800@0.0.1280.800" timestamp="1604854207192" />
<state x="240" y="137" key="Vcs.Push.Dialog.v2" timestamp="1606935046790">
<screen x="0" y="0" width="1280" height="800" />
</state>
<state x="240" y="137" key="Vcs.Push.Dialog.v2/0.0.1280.800@0.0.1280.800" timestamp="1606935046790" />
<state x="546" y="264" key="com.intellij.docker.deployment.ui.DockerPortBindingsField.v173.dialogSize" timestamp="1606060189431">
<screen x="0" y="0" width="1280" height="800" />
</state>
<state x="546" y="264" key="com.intellij.docker.deployment.ui.DockerPortBindingsField.v173.dialogSize/0.0.1280.800@0.0.1280.800" timestamp="1606060189431" />
<state x="361" y="264" width="706" height="259" key="com.intellij.docker.deployment.ui.DockerVolumeBindingsField.v173.dialogSize" timestamp="1606334614894">
<screen x="0" y="0" width="1280" height="800" />
</state>
<state x="361" y="264" width="706" height="259" key="com.intellij.docker.deployment.ui.DockerVolumeBindingsField.v173.dialogSize/0.0.1280.800@0.0.1280.800" timestamp="1606334614894" />
<state x="483" y="173" key="refactoring.ChangeSignatureDialog" timestamp="1603014849046">
<screen x="0" y="0" width="1280" height="800" />
</state>
<state x="483" y="173" key="refactoring.ChangeSignatureDialog/0.0.1280.800@0.0.1280.800" timestamp="1603014849046" />
<state x="305" y="124" width="670" height="676" key="search.everywhere.popup" timestamp="1602967169862">
<screen x="0" y="0" width="1280" height="800" />
</state>
<state x="305" y="124" width="670" height="676" key="search.everywhere.popup/0.0.1280.800@0.0.1280.800" timestamp="1602967169862" />
</component>
<component name="XDebuggerManager">
<watches-manager>
<configuration name="GoApplicationRunConfiguration">

View File

@@ -1,5 +1,5 @@
application: musicserver
version: 2.0.3
version: 2.1.0
runtime: go115
api_version: go1

View File

@@ -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:

91
indexFacade.go Normal file
View File

@@ -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
}
}

View File

@@ -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
}

View File

@@ -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",