All checks were successful
Build / build (push) Successful in 2m35s
#2 - Added request to download the newest version of the app #3 - Added request to check progress during sync #4 - Now blocking all request while sync is in progress #5 - Implemented ants for thread pooling #6 - Changed the sync request to now only start the sync
193 lines
5.1 KiB
Go
193 lines
5.1 KiB
Go
package server
|
|
|
|
import (
|
|
"log"
|
|
"music-server/internal/backend"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
type MusicHandler struct {
|
|
}
|
|
|
|
func NewMusicHandler() *MusicHandler {
|
|
return &MusicHandler{}
|
|
}
|
|
|
|
func (m *MusicHandler) GetSong(ctx echo.Context) error {
|
|
if backend.Syncing {
|
|
log.Println("Syncing is in progress")
|
|
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
|
}
|
|
song := ctx.QueryParam("song")
|
|
if song == "" {
|
|
return ctx.String(http.StatusBadRequest, "song can't be empty")
|
|
}
|
|
songPath := backend.GetSong(song)
|
|
file, err := os.Open(songPath)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, err)
|
|
}
|
|
defer file.Close()
|
|
return ctx.Stream(http.StatusOK, "audio/mpeg", file)
|
|
}
|
|
|
|
func (m *MusicHandler) GetSoundCheckSong(ctx echo.Context) error {
|
|
if backend.Syncing {
|
|
log.Println("Syncing is in progress")
|
|
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
|
}
|
|
songPath := backend.GetSoundCheckSong()
|
|
file, err := os.Open(songPath)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, err)
|
|
}
|
|
defer file.Close()
|
|
return ctx.Stream(http.StatusOK, "audio/mpeg", file)
|
|
}
|
|
|
|
func (m *MusicHandler) ResetMusic(ctx echo.Context) error {
|
|
if backend.Syncing {
|
|
log.Println("Syncing is in progress")
|
|
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
|
}
|
|
backend.Reset()
|
|
return ctx.NoContent(http.StatusOK)
|
|
}
|
|
|
|
func (m *MusicHandler) GetRandomSong(ctx echo.Context) error {
|
|
if backend.Syncing {
|
|
log.Println("Syncing is in progress")
|
|
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
|
}
|
|
songPath := backend.GetRandomSong()
|
|
file, err := os.Open(songPath)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, err)
|
|
}
|
|
defer file.Close()
|
|
return ctx.Stream(http.StatusOK, "audio/mpeg", file)
|
|
}
|
|
|
|
func (m *MusicHandler) GetRandomSongLowChance(ctx echo.Context) error {
|
|
if backend.Syncing {
|
|
log.Println("Syncing is in progress")
|
|
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
|
}
|
|
songPath := backend.GetRandomSongLowChance()
|
|
file, err := os.Open(songPath)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, err)
|
|
}
|
|
defer file.Close()
|
|
return ctx.Stream(http.StatusOK, "audio/mpeg", file)
|
|
}
|
|
|
|
func (m *MusicHandler) GetRandomSongClassic(ctx echo.Context) error {
|
|
if backend.Syncing {
|
|
log.Println("Syncing is in progress")
|
|
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
|
}
|
|
songPath := backend.GetRandomSongClassic()
|
|
file, err := os.Open(songPath)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, err)
|
|
}
|
|
defer file.Close()
|
|
return ctx.Stream(http.StatusOK, "audio/mpeg", file)
|
|
}
|
|
|
|
func (m *MusicHandler) GetSongInfo(ctx echo.Context) error {
|
|
song := backend.GetSongInfo()
|
|
return ctx.JSON(http.StatusOK, song)
|
|
}
|
|
|
|
func (m *MusicHandler) GetPlayedSongs(ctx echo.Context) error {
|
|
songList := backend.GetPlayedSongs()
|
|
return ctx.JSON(http.StatusOK, songList)
|
|
}
|
|
|
|
func (m *MusicHandler) GetNextSong(ctx echo.Context) error {
|
|
if backend.Syncing {
|
|
log.Println("Syncing is in progress")
|
|
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
|
}
|
|
songPath := backend.GetNextSong()
|
|
file, err := os.Open(songPath)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, err)
|
|
}
|
|
defer file.Close()
|
|
return ctx.Stream(http.StatusOK, "audio/mpeg", file)
|
|
}
|
|
|
|
func (m *MusicHandler) GetPreviousSong(ctx echo.Context) error {
|
|
if backend.Syncing {
|
|
log.Println("Syncing is in progress")
|
|
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
|
}
|
|
songPath := backend.GetPreviousSong()
|
|
file, err := os.Open(songPath)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, err)
|
|
}
|
|
defer file.Close()
|
|
return ctx.Stream(http.StatusOK, "audio/mpeg", file)
|
|
}
|
|
|
|
func (m *MusicHandler) GetAllGames(ctx echo.Context) error {
|
|
if backend.Syncing {
|
|
log.Println("Syncing is in progress")
|
|
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
|
}
|
|
gameList := backend.GetAllGames()
|
|
return ctx.JSON(http.StatusOK, gameList)
|
|
}
|
|
|
|
func (m *MusicHandler) GetAllGamesRandom(ctx echo.Context) error {
|
|
if backend.Syncing {
|
|
log.Println("Syncing is in progress")
|
|
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
|
}
|
|
gameList := backend.GetAllGamesRandom()
|
|
return ctx.JSON(http.StatusOK, gameList)
|
|
}
|
|
|
|
type played struct {
|
|
Song int
|
|
}
|
|
|
|
func (m *MusicHandler) PutPlayed(ctx echo.Context) error {
|
|
if backend.Syncing {
|
|
log.Println("Syncing is in progress")
|
|
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
|
}
|
|
var played played
|
|
err := ctx.Bind(&played)
|
|
if err != nil {
|
|
return ctx.JSON(http.StatusBadRequest, err)
|
|
}
|
|
backend.SetPlayed(played.Song)
|
|
return ctx.NoContent(http.StatusOK)
|
|
}
|
|
|
|
func (m *MusicHandler) AddLatestToQue(ctx echo.Context) error {
|
|
if backend.Syncing {
|
|
log.Println("Syncing is in progress")
|
|
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
|
}
|
|
backend.AddLatestToQue()
|
|
return ctx.NoContent(http.StatusOK)
|
|
}
|
|
|
|
func (m *MusicHandler) AddLatestPlayed(ctx echo.Context) error {
|
|
if backend.Syncing {
|
|
log.Println("Syncing is in progress")
|
|
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
|
}
|
|
backend.AddLatestPlayed()
|
|
return ctx.NoContent(http.StatusOK)
|
|
}
|