Added support for profiling. Removed the pkg module altogether. Everything except old sync is now using code generated by sqlc.
140 lines
3.5 KiB
Go
140 lines
3.5 KiB
Go
package server
|
|
|
|
import (
|
|
"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 {
|
|
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 {
|
|
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 {
|
|
backend.Reset()
|
|
return ctx.NoContent(http.StatusOK)
|
|
}
|
|
|
|
func (m *MusicHandler) GetRandomSong(ctx echo.Context) error {
|
|
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 {
|
|
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 {
|
|
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 {
|
|
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 {
|
|
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 {
|
|
gameList := backend.GetAllGames()
|
|
return ctx.JSON(http.StatusOK, gameList)
|
|
}
|
|
|
|
func (m *MusicHandler) GetAllGamesRandom(ctx echo.Context) error {
|
|
gameList := backend.GetAllGamesRandom()
|
|
return ctx.JSON(http.StatusOK, gameList)
|
|
}
|
|
|
|
type played struct {
|
|
Song int
|
|
}
|
|
|
|
func (m *MusicHandler) PutPlayed(ctx echo.Context) error {
|
|
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 {
|
|
backend.AddLatestToQue()
|
|
return ctx.NoContent(http.StatusOK)
|
|
}
|
|
|
|
func (m *MusicHandler) AddLatestPlayed(ctx echo.Context) error {
|
|
backend.AddLatestPlayed()
|
|
return ctx.NoContent(http.StatusOK)
|
|
}
|