diff --git a/.gitignore b/.gitignore
index 62c8935..28aad3e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,4 @@
-.idea/
\ No newline at end of file
+.idea/
+.idea/*
+.idea/workspace.xml
+workspace.xml
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index d7c4838..98f8d27 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -19,9 +19,11 @@
-
+
+
+
@@ -71,7 +73,7 @@
-
+
@@ -173,7 +175,8 @@
-
+
+
true
@@ -195,10 +198,10 @@
-
+
-
+
@@ -207,54 +210,54 @@
-
+
-
-
-
+
+
+
-
-
-
+
+
+
-
-
-
+
+
+
-
-
-
-
+
+
+
+
-
-
-
-
+
+
+
+
-
-
-
-
+
+
+
+
-
-
-
-
+
+
+
+
-
-
+
+
-
+
-
+
@@ -273,15 +276,6 @@
-
-
-
- file://$PROJECT_DIR$/musicFacade.go
- 135
-
-
-
-
diff --git a/app.yaml b/app.yaml
index fd8baf2..66f0a2a 100644
--- a/app.yaml
+++ b/app.yaml
@@ -1,5 +1,5 @@
application: musicserver
-version: 2.0.1
+version: 2.0.2
runtime: go115
api_version: go1
diff --git a/doc/swagger.yaml b/doc/swagger.yaml
index 68929d1..64cd74e 100644
--- a/doc/swagger.yaml
+++ b/doc/swagger.yaml
@@ -1,8 +1,8 @@
openapi: 3.0.3
info:
- version: "2.0.0"
+ version: "2.0.2"
title: "Music Server"
- description: "Rebuilt the application in Go."
+ description: "Hopefully fixed the caching problem with random."
contact:
email: "zarnor91@gmail.com"
servers:
diff --git a/musicFacade.go b/musicFacade.go
index dec90eb..c6c0fc0 100644
--- a/musicFacade.go
+++ b/musicFacade.go
@@ -3,10 +3,12 @@ package main
import (
"encoding/json"
"fmt"
+ "io"
"io/ioutil"
"log"
"math/rand"
"net/http"
+ "os"
"strconv"
)
@@ -163,7 +165,7 @@ func getPreviousSong() string {
}
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 {
song := r.URL.Query().Get("song")
if song == "" {
@@ -173,20 +175,32 @@ func musicHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}
} 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 {
- 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 {
reset()
w.WriteHeader(http.StatusOK)
} 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 {
- 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 {
w.Header().Add("Content-Type", "application/json")
@@ -197,10 +211,14 @@ func musicHandler(w http.ResponseWriter, r *http.Request) {
_ = json.NewEncoder(w).Encode(getPlayedSongs())
} 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" {
- 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 {
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 {
song int
}
diff --git a/musicserver.go b/musicserver.go
index 3395a52..39140d9 100644
--- a/musicserver.go
+++ b/musicserver.go
@@ -100,15 +100,20 @@ type SongData struct {
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
- (w).Header().Set("Access-Control-Allow-Origin", "*")
+ setCorsAndNoCacheHeaders(&w, r)
+
if r.URL.Path == "/version" {
w.Header().Add("Content-Type", "application/json")
testf()
- data := VersionData{Version: "2.0.1",
- Changelog: "Fixed CORS",
+ data := VersionData{Version: "2.0.2",
+ Changelog: "Hopefully fixed the caching problem with random.",
History: []VersionData{
+ {
+ Version: "2.0.1",
+ Changelog: "Fixed CORS",
+ },
{
Version: "2.0.0",
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)
+ }
+ }
+}
diff --git a/syncFacade.go b/syncFacade.go
index 3450a74..58f949b 100644
--- a/syncFacade.go
+++ b/syncFacade.go
@@ -12,7 +12,7 @@ import (
)
func syncHandler(w http.ResponseWriter, r *http.Request) {
- (w).Header().Set("Access-Control-Allow-Origin", "*")
+ setCorsAndNoCacheHeaders(&w, r)
if r.URL.Path == "/sync" {
w.Header().Add("Content-Type", "application/json")
syncGames()