Changed the structure of the whole application, should be no changes to functionality.

This commit is contained in:
2021-12-05 11:18:48 +01:00
parent 0a73134381
commit e1de6f0f76
28 changed files with 935 additions and 1105 deletions

32
pkg/api/index.go Normal file
View File

@@ -0,0 +1,32 @@
package api
import (
"encoding/json"
"fmt"
"music-server/pkg/helpers"
"music-server/pkg/server"
"net/http"
)
func IndexHandler(w http.ResponseWriter, r *http.Request) {
helpers.SetCorsAndNoCacheHeaders(&w, r)
if r.URL.Path == "/version" {
w.Header().Add("Content-Type", "application/json")
history := server.GetVersionHistory()
_ = json.NewEncoder(w).Encode(history)
} else if r.URL.Path == "/docs" {
http.ServeFile(w, r, "./docs/swagger.yaml")
} else if r.URL.Path == "/" {
_, err := fmt.Fprint(w, "Hello, World!!")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
} else {
http.NotFound(w, r)
return
}
}

79
pkg/api/music.go Normal file
View File

@@ -0,0 +1,79 @@
package api
import (
"encoding/json"
"fmt"
"music-server/pkg/helpers"
"music-server/pkg/models"
"music-server/pkg/server"
"net/http"
)
func MusicHandler(w http.ResponseWriter, r *http.Request) {
helpers.SetCorsAndNoCacheHeaders(&w, r)
if r.URL.Path == "/music" && r.Method == http.MethodGet {
song := r.URL.Query().Get("song")
if song == "" {
w.WriteHeader(http.StatusBadRequest)
_, err := fmt.Fprint(w, "song can't be empty")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
} else {
s := server.GetSong(song)
helpers.SendSong(w, s)
}
} else if r.URL.Path == "/music/first" && r.Method == http.MethodGet {
song := server.GetSoundCheckSong()
helpers.SendSong(w, song)
} else if r.URL.Path == "/music/reset" && r.Method == http.MethodGet {
server.Reset()
w.WriteHeader(http.StatusOK)
} else if r.URL.Path == "/music/rand" && r.Method == http.MethodGet {
song := server.GetRandomSong()
helpers.SendSong(w, song)
} else if r.URL.Path == "/music/rand/low" && r.Method == http.MethodGet {
chance := server.GetRandomSongLowChance()
helpers.SendSong(w, chance)
} else if r.URL.Path == "/music/info" && r.Method == http.MethodGet {
w.Header().Add("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(server.GetSongInfo())
} else if r.URL.Path == "/music/list" && r.Method == http.MethodGet {
w.Header().Add("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(server.GetPlayedSongs())
} else if r.URL.Path == "/music/next" {
song := server.GetNextSong()
helpers.SendSong(w, song)
} else if r.URL.Path == "/music/previous" {
song := server.GetPreviousSong()
helpers.SendSong(w, song)
} else if r.URL.Path == "/music/all" && r.Method == http.MethodGet {
w.Header().Add("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(server.GetAllGames())
} else if r.URL.Path == "/music/all/random" && r.Method == http.MethodGet {
w.Header().Add("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(server.GetAllGamesRandom())
} else if r.URL.Path == "/music/played" && r.Method == http.MethodPut {
var p models.Played
err := json.NewDecoder(r.Body).Decode(&p)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
server.SetPlayed(p.Song)
w.WriteHeader(http.StatusOK)
} else if r.URL.Path == "/music/addQue" && r.Method == http.MethodGet {
server.AddLatestToQue()
w.WriteHeader(http.StatusOK)
}
}

28
pkg/api/sync.go Normal file
View File

@@ -0,0 +1,28 @@
package api
import (
"fmt"
"music-server/pkg/helpers"
"music-server/pkg/server"
"net/http"
)
func SyncHandler(w http.ResponseWriter, r *http.Request) {
helpers.SetCorsAndNoCacheHeaders(&w, r)
if r.URL.Path == "/sync" {
w.Header().Add("Content-Type", "application/json")
server.SyncGames()
_, err := fmt.Fprint(w, "Games are synced")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
} else if r.URL.Path == "/sync/reset" {
w.Header().Add("Content-Type", "application/json")
server.ResetDB()
_, err := fmt.Fprint(w, "Games and songs are deleted from the database")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
}
}