Moved around more code. Implemented more sqlc. Added support to generate swagger.

Added support for profiling. Removed the pkg module altogether.
Everything except old sync is now using code generated by sqlc.
This commit is contained in:
2025-01-15 16:04:14 +01:00
parent db8214cb02
commit d653463f58
43 changed files with 947 additions and 1133 deletions

View File

@@ -2,11 +2,10 @@ package server
import (
"music-server/internal/backend"
"music-server/internal/helpers"
"music-server/pkg/models"
"net/http"
"os"
"github.com/labstack/echo"
"github.com/labstack/echo/v4"
)
type MusicHandler struct {
@@ -21,13 +20,23 @@ func (m *MusicHandler) GetSong(ctx echo.Context) error {
if song == "" {
return ctx.String(http.StatusBadRequest, "song can't be empty")
}
s := backend.GetSong(song)
return helpers.SendSong(ctx, s)
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 {
song := backend.GetSoundCheckSong()
return helpers.SendSong(ctx, song)
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 {
@@ -36,18 +45,33 @@ func (m *MusicHandler) ResetMusic(ctx echo.Context) error {
}
func (m *MusicHandler) GetRandomSong(ctx echo.Context) error {
song := backend.GetRandomSong()
return helpers.SendSong(ctx, song)
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 {
song := backend.GetRandomSongLowChance()
return helpers.SendSong(ctx, song)
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 {
song := backend.GetRandomSongClassic()
return helpers.SendSong(ctx, song)
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 {
@@ -61,13 +85,23 @@ func (m *MusicHandler) GetPlayedSongs(ctx echo.Context) error {
}
func (m *MusicHandler) GetNextSong(ctx echo.Context) error {
song := backend.GetNextSong()
return helpers.SendSong(ctx, song)
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 {
song := backend.GetPreviousSong()
return helpers.SendSong(ctx, song)
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 {
@@ -80,11 +114,14 @@ func (m *MusicHandler) GetAllGamesRandom(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, gameList)
}
type played struct {
Song int
}
func (m *MusicHandler) PutPlayed(ctx echo.Context) error {
var played models.Played
var played played
err := ctx.Bind(&played)
if err != nil {
//helpers.NewError(ctx, http.StatusBadRequest, err)
return ctx.JSON(http.StatusBadRequest, err)
}
backend.SetPlayed(played.Song)