Hopefully fixed the caching problem with random

This commit is contained in:
2020-12-02 19:50:44 +01:00
parent b24c9dca3c
commit c21b66e0d9
7 changed files with 141 additions and 59 deletions

5
.gitignore vendored
View File

@@ -1 +1,4 @@
.idea/ .idea/
.idea/*
.idea/workspace.xml
workspace.xml

82
.idea/workspace.xml generated
View File

@@ -19,9 +19,11 @@
<select /> <select />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="d639f8a9-4b6d-4957-9ba4-8361acac15ca" name="Default Changelist" comment=""> <list default="true" id="d639f8a9-4b6d-4957-9ba4-8361acac15ca" name="Default Changelist" comment="Hopefully fixed the caching problem with random">
<change beforePath="$PROJECT_DIR$/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" 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$/app.yaml" beforeDir="false" afterPath="$PROJECT_DIR$/app.yaml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/doc/swagger.yaml" beforeDir="false" afterPath="$PROJECT_DIR$/doc/swagger.yaml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/musicFacade.go" beforeDir="false" afterPath="$PROJECT_DIR$/musicFacade.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/musicFacade.go" beforeDir="false" afterPath="$PROJECT_DIR$/musicFacade.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/musicserver.go" beforeDir="false" afterPath="$PROJECT_DIR$/musicserver.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/musicserver.go" beforeDir="false" afterPath="$PROJECT_DIR$/musicserver.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/syncFacade.go" beforeDir="false" afterPath="$PROJECT_DIR$/syncFacade.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/syncFacade.go" beforeDir="false" afterPath="$PROJECT_DIR$/syncFacade.go" afterDir="false" />
@@ -71,7 +73,7 @@
<recent name="$PROJECT_DIR$/songs" /> <recent name="$PROJECT_DIR$/songs" />
</key> </key>
</component> </component>
<component name="RunManager" selected="Docker.Dockerfile"> <component name="RunManager" selected="Go Build.App Engine">
<configuration name="App Engine" type="GoApplicationRunConfiguration" factoryName="Go Application"> <configuration name="App Engine" type="GoApplicationRunConfiguration" factoryName="Go Application">
<module name="MusicServer" /> <module name="MusicServer" />
<working_directory value="$PROJECT_DIR$" /> <working_directory value="$PROJECT_DIR$" />
@@ -173,7 +175,8 @@
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<MESSAGE value="1.0 of Go version" /> <MESSAGE value="1.0 of Go version" />
<MESSAGE value="Small fix in Dockerfile" /> <MESSAGE value="Small fix in Dockerfile" />
<option name="LAST_COMMIT_MESSAGE" value="Small fix in Dockerfile" /> <MESSAGE value="Fixed CORS" />
<option name="LAST_COMMIT_MESSAGE" value="Fixed CORS" />
</component> </component>
<component name="VgoProject"> <component name="VgoProject">
<integration-enabled>true</integration-enabled> <integration-enabled>true</integration-enabled>
@@ -195,10 +198,10 @@
<screen x="0" y="0" width="1280" height="800" /> <screen x="0" y="0" width="1280" height="800" />
</state> </state>
<state x="354" y="206" key="#com.intellij.fileTypes.FileTypeChooser/0.0.1280.800@0.0.1280.800" timestamp="1606327049349" /> <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="1606502062415"> <state x="63" y="25" width="1120" height="702" key="CommitChangelistDialog2" timestamp="1606934817114">
<screen x="0" y="0" width="1280" height="800" /> <screen x="0" y="0" width="1280" height="800" />
</state> </state>
<state x="63" y="25" width="1120" height="702" key="CommitChangelistDialog2/0.0.1280.800@0.0.1280.800" timestamp="1606502062415" /> <state x="63" y="25" width="1120" height="702" key="CommitChangelistDialog2/0.0.1280.800@0.0.1280.800" timestamp="1606934817114" />
<state x="500" y="304" key="Github.ShareDialog" timestamp="1602604919305"> <state x="500" y="304" key="Github.ShareDialog" timestamp="1602604919305">
<screen x="0" y="0" width="1280" height="800" /> <screen x="0" y="0" width="1280" height="800" />
</state> </state>
@@ -207,54 +210,54 @@
<screen x="0" y="0" width="1280" height="800" /> <screen x="0" y="0" width="1280" height="800" />
</state> </state>
<state x="454" y="112" key="Github.UntrackedFilesDialog/0.0.1280.800@0.0.1280.800" timestamp="1602604943862" /> <state x="454" y="112" key="Github.UntrackedFilesDialog/0.0.1280.800@0.0.1280.800" timestamp="1602604943862" />
<state width="929" height="234" key="GridCell.Tab.0.bottom" timestamp="1606501547537"> <state width="1238" height="212" key="GridCell.Tab.0.bottom" timestamp="1606934825189">
<screen x="0" y="0" width="1280" height="800" /> <screen x="0" y="0" width="1280" height="800" />
</state> </state>
<state width="929" height="234" key="GridCell.Tab.0.bottom/0.0.1280.800@0.0.1280.800" timestamp="1606501547537" /> <state width="1238" height="212" key="GridCell.Tab.0.bottom/0.0.1280.800@0.0.1280.800" timestamp="1606934825189" />
<state width="846" height="212" key="GridCell.Tab.0.bottom/0.25.1280.702@0.25.1280.702" timestamp="1606330812762" /> <state width="1238" height="212" key="GridCell.Tab.0.bottom/0.25.1280.702@0.25.1280.702" timestamp="1606934333706" />
<state width="929" height="234" key="GridCell.Tab.0.center" timestamp="1606501547533"> <state width="1238" height="212" key="GridCell.Tab.0.center" timestamp="1606934825188">
<screen x="0" y="0" width="1280" height="800" /> <screen x="0" y="0" width="1280" height="800" />
</state> </state>
<state width="929" height="234" key="GridCell.Tab.0.center/0.0.1280.800@0.0.1280.800" timestamp="1606501547533" /> <state width="1238" height="212" key="GridCell.Tab.0.center/0.0.1280.800@0.0.1280.800" timestamp="1606934825188" />
<state width="846" height="212" key="GridCell.Tab.0.center/0.25.1280.702@0.25.1280.702" timestamp="1606330812757" /> <state width="1238" height="212" key="GridCell.Tab.0.center/0.25.1280.702@0.25.1280.702" timestamp="1606934333705" />
<state width="929" height="234" key="GridCell.Tab.0.left" timestamp="1606501547529"> <state width="1238" height="212" key="GridCell.Tab.0.left" timestamp="1606934825187">
<screen x="0" y="0" width="1280" height="800" /> <screen x="0" y="0" width="1280" height="800" />
</state> </state>
<state width="929" height="234" key="GridCell.Tab.0.left/0.0.1280.800@0.0.1280.800" timestamp="1606501547529" /> <state width="1238" height="212" key="GridCell.Tab.0.left/0.0.1280.800@0.0.1280.800" timestamp="1606934825187" />
<state width="846" height="212" key="GridCell.Tab.0.left/0.25.1280.702@0.25.1280.702" timestamp="1606330812754" /> <state width="1238" height="212" key="GridCell.Tab.0.left/0.25.1280.702@0.25.1280.702" timestamp="1606934333703" />
<state width="929" height="234" key="GridCell.Tab.0.right" timestamp="1606501547536"> <state width="1238" height="212" key="GridCell.Tab.0.right" timestamp="1606934825188">
<screen x="0" y="0" width="1280" height="800" /> <screen x="0" y="0" width="1280" height="800" />
</state> </state>
<state width="929" height="234" key="GridCell.Tab.0.right/0.0.1280.800@0.0.1280.800" timestamp="1606501547536" /> <state width="1238" height="212" key="GridCell.Tab.0.right/0.0.1280.800@0.0.1280.800" timestamp="1606934825188" />
<state width="846" height="212" key="GridCell.Tab.0.right/0.25.1280.702@0.25.1280.702" timestamp="1606330812760" /> <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="1606334681952"> <state width="1238" height="264" key="GridCell.Tab.1.bottom" timestamp="1606934171754">
<screen x="0" y="0" width="1280" height="800" /> <screen x="0" y="25" width="1280" height="702" />
</state> </state>
<state width="1238" height="264" key="GridCell.Tab.1.bottom/0.0.1280.800@0.0.1280.800" timestamp="1606334681952" /> <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="1606330741478" /> <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="1606334681950"> <state width="1238" height="264" key="GridCell.Tab.1.center" timestamp="1606934171752">
<screen x="0" y="0" width="1280" height="800" /> <screen x="0" y="25" width="1280" height="702" />
</state> </state>
<state width="1238" height="264" key="GridCell.Tab.1.center/0.0.1280.800@0.0.1280.800" timestamp="1606334681950" /> <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="1606330741474" /> <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="1606334681950"> <state width="1238" height="264" key="GridCell.Tab.1.left" timestamp="1606934171752">
<screen x="0" y="0" width="1280" height="800" /> <screen x="0" y="25" width="1280" height="702" />
</state> </state>
<state width="1238" height="264" key="GridCell.Tab.1.left/0.0.1280.800@0.0.1280.800" timestamp="1606334681950" /> <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="1606330741473" /> <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="1606334681951"> <state width="1238" height="264" key="GridCell.Tab.1.right" timestamp="1606934171753">
<screen x="0" y="0" width="1280" height="800" /> <screen x="0" y="25" width="1280" height="702" />
</state> </state>
<state width="1238" height="264" key="GridCell.Tab.1.right/0.0.1280.800@0.0.1280.800" timestamp="1606334681951" /> <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="1606330741474" /> <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"> <state x="491" y="322" key="VCS.ChangelistChooser" timestamp="1604854207192">
<screen x="0" y="0" width="1280" height="800" /> <screen x="0" y="0" width="1280" height="800" />
</state> </state>
<state x="491" y="322" key="VCS.ChangelistChooser/0.0.1280.800@0.0.1280.800" timestamp="1604854207192" /> <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="1606502064491"> <state x="240" y="137" key="Vcs.Push.Dialog.v2" timestamp="1606767774291">
<screen x="0" y="0" width="1280" height="800" /> <screen x="0" y="0" width="1280" height="800" />
</state> </state>
<state x="240" y="137" key="Vcs.Push.Dialog.v2/0.0.1280.800@0.0.1280.800" timestamp="1606502064491" /> <state x="240" y="137" key="Vcs.Push.Dialog.v2/0.0.1280.800@0.0.1280.800" timestamp="1606767774291" />
<state x="546" y="264" key="com.intellij.docker.deployment.ui.DockerPortBindingsField.v173.dialogSize" timestamp="1606060189431"> <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" /> <screen x="0" y="0" width="1280" height="800" />
</state> </state>
@@ -273,15 +276,6 @@
<state x="305" y="124" width="670" height="676" key="search.everywhere.popup/0.0.1280.800@0.0.1280.800" timestamp="1602967169862" /> <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>
<component name="XDebuggerManager"> <component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" type="DlvLineBreakpoint">
<url>file://$PROJECT_DIR$/musicFacade.go</url>
<line>135</line>
<option name="timeStamp" value="4" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
<watches-manager> <watches-manager>
<configuration name="GoApplicationRunConfiguration"> <configuration name="GoApplicationRunConfiguration">
<watch expression="games" language="go" /> <watch expression="games" language="go" />

View File

@@ -1,5 +1,5 @@
application: musicserver application: musicserver
version: 2.0.1 version: 2.0.2
runtime: go115 runtime: go115
api_version: go1 api_version: go1

View File

@@ -1,8 +1,8 @@
openapi: 3.0.3 openapi: 3.0.3
info: info:
version: "2.0.0" version: "2.0.2"
title: "Music Server" title: "Music Server"
description: "Rebuilt the application in Go." description: "Hopefully fixed the caching problem with random."
contact: contact:
email: "zarnor91@gmail.com" email: "zarnor91@gmail.com"
servers: servers:

View File

@@ -3,10 +3,12 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"log" "log"
"math/rand" "math/rand"
"net/http" "net/http"
"os"
"strconv" "strconv"
) )
@@ -163,7 +165,7 @@ func getPreviousSong() string {
} }
func musicHandler(w http.ResponseWriter, r *http.Request) { func musicHandler(w http.ResponseWriter, r *http.Request) {
(w).Header().Set("Access-Control-Allow-Origin", "*") setCorsAndNoCacheHeaders(&w, r)
if r.URL.Path == "/music" && r.Method == http.MethodGet { if r.URL.Path == "/music" && r.Method == http.MethodGet {
song := r.URL.Query().Get("song") song := r.URL.Query().Get("song")
if song == "" { if song == "" {
@@ -173,20 +175,32 @@ func musicHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
} }
} else { } else {
http.ServeFile(w, r, getSong(song)) s := getSong(song)
sendFile(w, r, s)
//http.ServeFile(w, r, s)
} }
} else if r.URL.Path == "/music/first" && r.Method == http.MethodGet { } else if r.URL.Path == "/music/first" && r.Method == http.MethodGet {
http.ServeFile(w, r, getSoundCheckSong()) song := getSoundCheckSong()
sendFile(w, r, song)
//http.ServeFile(w, r, getSoundCheckSong())
} else if r.URL.Path == "/music/reset" && r.Method == http.MethodGet { } else if r.URL.Path == "/music/reset" && r.Method == http.MethodGet {
reset() reset()
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} else if r.URL.Path == "/music/rand" && r.Method == http.MethodGet { } else if r.URL.Path == "/music/rand" && r.Method == http.MethodGet {
http.ServeFile(w, r, getRandomSong()) 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 { } else if r.URL.Path == "/music/rand/low" && r.Method == http.MethodGet {
http.ServeFile(w, r, getRandomSongLowChance()) chance := getRandomSongLowChance()
//http.ServeFile(w, r, chance)
sendFile(w, r, chance)
} else if r.URL.Path == "/music/info" && r.Method == http.MethodGet { } else if r.URL.Path == "/music/info" && r.Method == http.MethodGet {
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
@@ -197,10 +211,14 @@ func musicHandler(w http.ResponseWriter, r *http.Request) {
_ = json.NewEncoder(w).Encode(getPlayedSongs()) _ = json.NewEncoder(w).Encode(getPlayedSongs())
} else if r.URL.Path == "/music/next" { } else if r.URL.Path == "/music/next" {
http.ServeFile(w, r, getNextSong()) song := getNextSong()
//http.ServeFile(w, r, song)
sendFile(w, r, song)
} else if r.URL.Path == "/music/previous" { } else if r.URL.Path == "/music/previous" {
http.ServeFile(w, r, getPreviousSong()) song := getPreviousSong()
//http.ServeFile(w, r, song)
sendFile(w, r, song)
} else if r.URL.Path == "/music/all" && r.Method == http.MethodGet { } else if r.URL.Path == "/music/all" && r.Method == http.MethodGet {
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
@@ -218,6 +236,44 @@ func musicHandler(w http.ResponseWriter, r *http.Request) {
} }
} }
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
if err != nil {
//File not found, send 404
http.Error(writer, "File not found.", 404)
return
}
//File is found, create and send the correct headers
//Get the Content-Type of the file
//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)
//Get content type of file
//FileContentType := http.DetectContentType(FileHeader)
//Get the file size
FileStat, _ := Openfile.Stat() //Get info from file
FileSize := strconv.FormatInt(FileStat.Size(), 10) //Get file size as a string
//Send the headers
writer.Header().Set("Content-Disposition", "attachment; filename="+Filename)
writer.Header().Set("Content-Type", "audio/mpeg")
writer.Header().Set("Content-Length", FileSize)
//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
return
}
type Played struct { type Played struct {
song int song int
} }

View File

@@ -100,15 +100,20 @@ type SongData struct {
} }
func indexHandler(w http.ResponseWriter, r *http.Request) { func indexHandler(w http.ResponseWriter, r *http.Request) {
(w).Header().Set("Access-Control-Allow-Origin", "*") setCorsAndNoCacheHeaders(&w, r)
if r.URL.Path == "/version" { if r.URL.Path == "/version" {
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
testf() testf()
data := VersionData{Version: "2.0.1", data := VersionData{Version: "2.0.2",
Changelog: "Fixed CORS", Changelog: "Hopefully fixed the caching problem with random.",
History: []VersionData{ History: []VersionData{
{
Version: "2.0.1",
Changelog: "Fixed CORS",
},
{ {
Version: "2.0.0", Version: "2.0.0",
Changelog: "Rebuilt the application in Go.", Changelog: "Rebuilt the application in Go.",
@@ -165,3 +170,27 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
} }
} }
func setCorsAndNoCacheHeaders(w *http.ResponseWriter, r *http.Request) {
var etagHeaders = []string{
"ETag",
"If-Modified-Since",
"If-Match",
"If-None-Match",
"If-Range",
"If-Unmodified-Since",
}
(*w).Header().Set("Expires", "Tue, 03 Jul 2001 06:00:00 GMT")
(*w).Header().Set("Last-Modified", time.Now().String()+" GMT")
(*w).Header().Set("Cache-Control", "no-cache, no-store, private, max-age=0")
(*w).Header().Set("Pragma", "no-cache")
(*w).Header().Set("X-Accel-Expires", "0")
(*w).Header().Set("Access-Control-Allow-Origin", "*")
for _, v := range etagHeaders {
if r.Header.Get(v) != "" {
r.Header.Del(v)
}
}
}

View File

@@ -12,7 +12,7 @@ import (
) )
func syncHandler(w http.ResponseWriter, r *http.Request) { func syncHandler(w http.ResponseWriter, r *http.Request) {
(w).Header().Set("Access-Control-Allow-Origin", "*") setCorsAndNoCacheHeaders(&w, r)
if r.URL.Path == "/sync" { if r.URL.Path == "/sync" {
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
syncGames() syncGames()