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:
149
pkg/api/music.go
149
pkg/api/music.go
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user