Changed routing framework from mux to Gin.

Swagger doc is now included in the application.
A fronted can now be hosted from the application.
This commit is contained in:
2022-01-29 17:52:33 +01:00
parent 512fcd0c4f
commit f9d6c24a97
43 changed files with 28760 additions and 210 deletions

View File

@@ -1,32 +1,29 @@
package api
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"music-server/pkg/helpers"
"music-server/pkg/server"
"net/http"
)
func IndexHandler(w http.ResponseWriter, r *http.Request) {
helpers.SetCorsAndNoCacheHeaders(&w, r)
type Index struct {
}
if r.URL.Path == "/version" {
w.Header().Add("Content-Type", "application/json")
func NewIndex() *Index {
return &Index{}
}
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)
func (i *Index) GetVersion(ctx *gin.Context) {
versionHistory := server.GetVersionHistory()
if versionHistory.Version == "" {
helpers.NewError(ctx, http.StatusNotFound, nil)
return
}
ctx.JSON(http.StatusOK, versionHistory)
}
func (i *Index) GetDBTest(ctx *gin.Context) {
server.TestDB()
ctx.JSON(http.StatusOK, "TestedDB")
}

View File

@@ -1,79 +1,90 @@
package api
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"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)
}
type Music struct {
}
func NewMusic() *Music {
return &Music{}
}
func (m *Music) GetSong(ctx *gin.Context) {
song := ctx.Query("song")
if song == "" {
ctx.String(http.StatusBadRequest, "song can't be empty")
}
s := server.GetSong(song)
helpers.SendSong(ctx, s)
}
func (m *Music) GetMusicFirst(ctx *gin.Context) {
song := server.GetSoundCheckSong()
helpers.SendSong(ctx, song)
}
func (m *Music) ResetMusic(ctx *gin.Context) {
server.Reset()
ctx.Status(http.StatusOK)
}
func (m *Music) GetRandomSong(ctx *gin.Context) {
song := server.GetRandomSong()
helpers.SendSong(ctx, song)
}
func (m *Music) GetRandomSongLowChance(ctx *gin.Context) {
song := server.GetRandomSongLowChance()
helpers.SendSong(ctx, song)
}
func (m *Music) GetSongInfo(ctx *gin.Context) {
song := server.GetSongInfo()
ctx.JSON(http.StatusOK, song)
}
func (m *Music) GetPlayedSongs(ctx *gin.Context) {
songList := server.GetPlayedSongs()
ctx.JSON(http.StatusOK, songList)
}
func (m *Music) GetNextSong(ctx *gin.Context) {
song := server.GetNextSong()
helpers.SendSong(ctx, song)
}
func (m *Music) GetPreviousSong(ctx *gin.Context) {
song := server.GetPreviousSong()
helpers.SendSong(ctx, song)
}
func (m *Music) GetAllGames(ctx *gin.Context) {
gameList := server.GetAllGames()
ctx.JSON(http.StatusOK, gameList)
}
func (m *Music) GetAllGamesRandom(ctx *gin.Context) {
gameList := server.GetAllGamesRandom()
ctx.JSON(http.StatusOK, gameList)
}
func (m *Music) PutPlayed(ctx *gin.Context) {
var played models.Played
if err := ctx.ShouldBindJSON(&played); err != nil {
helpers.NewError(ctx, http.StatusBadRequest, err)
return
}
server.SetPlayed(played.Song)
ctx.Status(http.StatusOK)
}
func (m *Music) AddLatestToQue(ctx *gin.Context) {
server.AddLatestToQue()
ctx.Status(http.StatusOK)
}

View File

@@ -1,28 +1,24 @@
package api
import (
"fmt"
"music-server/pkg/helpers"
"github.com/gin-gonic/gin"
"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)
}
}
type Sync struct {
}
func NewSync() *Sync {
return &Sync{}
}
func (s *Sync) SyncGames(ctx *gin.Context) {
server.SyncGames()
ctx.JSON(http.StatusOK, "Games are synced")
}
func (s *Sync) ResetGames(ctx *gin.Context) {
server.ResetDB()
ctx.JSON(http.StatusOK, "Games and songs are deleted from the database")
}