Reorganized the code, moved more things to the new part
This commit is contained in:
28
internal/server/indexHandler.go
Normal file
28
internal/server/indexHandler.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"music-server/internal/backend"
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo"
|
||||
)
|
||||
|
||||
type IndexHandler struct {
|
||||
}
|
||||
|
||||
func NewIndexHandler() *IndexHandler {
|
||||
return &IndexHandler{}
|
||||
}
|
||||
|
||||
func (i *IndexHandler) GetVersion(ctx echo.Context) error {
|
||||
versionHistory := backend.GetVersionHistory()
|
||||
if versionHistory.Version == "" {
|
||||
return ctx.JSON(http.StatusNotFound, "version not found")
|
||||
}
|
||||
return ctx.JSON(http.StatusOK, versionHistory)
|
||||
}
|
||||
|
||||
func (i *IndexHandler) GetDBTest(ctx echo.Context) error {
|
||||
backend.TestDB()
|
||||
return ctx.JSON(http.StatusOK, "TestedDB")
|
||||
}
|
||||
102
internal/server/musicHandler.go
Normal file
102
internal/server/musicHandler.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"music-server/internal/backend"
|
||||
"music-server/internal/helpers"
|
||||
"music-server/pkg/models"
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo"
|
||||
)
|
||||
|
||||
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")
|
||||
}
|
||||
s := backend.GetSong(song)
|
||||
return helpers.SendSong(ctx, s)
|
||||
}
|
||||
|
||||
func (m *MusicHandler) GetSoundCheckSong(ctx echo.Context) error {
|
||||
song := backend.GetSoundCheckSong()
|
||||
return helpers.SendSong(ctx, song)
|
||||
}
|
||||
|
||||
func (m *MusicHandler) ResetMusic(ctx echo.Context) error {
|
||||
backend.Reset()
|
||||
return ctx.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
func (m *MusicHandler) GetRandomSong(ctx echo.Context) error {
|
||||
song := backend.GetRandomSong()
|
||||
return helpers.SendSong(ctx, song)
|
||||
}
|
||||
|
||||
func (m *MusicHandler) GetRandomSongLowChance(ctx echo.Context) error {
|
||||
song := backend.GetRandomSongLowChance()
|
||||
return helpers.SendSong(ctx, song)
|
||||
}
|
||||
|
||||
func (m *MusicHandler) GetRandomSongClassic(ctx echo.Context) error {
|
||||
song := backend.GetRandomSongClassic()
|
||||
return helpers.SendSong(ctx, song)
|
||||
}
|
||||
|
||||
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 {
|
||||
song := backend.GetNextSong()
|
||||
return helpers.SendSong(ctx, song)
|
||||
}
|
||||
|
||||
func (m *MusicHandler) GetPreviousSong(ctx echo.Context) error {
|
||||
song := backend.GetPreviousSong()
|
||||
return helpers.SendSong(ctx, song)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func (m *MusicHandler) PutPlayed(ctx echo.Context) error {
|
||||
var played models.Played
|
||||
err := ctx.Bind(&played)
|
||||
if err != nil {
|
||||
//helpers.NewError(ctx, http.StatusBadRequest, err)
|
||||
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)
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package server
|
||||
import (
|
||||
"fmt"
|
||||
"music-server/cmd/web"
|
||||
"music-server/pkg/api"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -37,11 +36,11 @@ func (s *Server) RegisterRoutes() http.Handler {
|
||||
swagger := http.FileServer(http.FS(web.Swagger))
|
||||
e.GET("/swagger/*", echo.WrapHandler(swagger))
|
||||
|
||||
index := api.NewIndex()
|
||||
index := NewIndexHandler()
|
||||
e.GET("/version", index.GetVersion)
|
||||
e.GET("/health", index.GetDBTest)
|
||||
|
||||
sync := api.NewSync()
|
||||
sync := NewSyncHandler()
|
||||
syncGroup := e.Group("/sync")
|
||||
syncGroup.GET("", sync.SyncGames)
|
||||
syncGroup.GET("/new", sync.SyncGamesNewOnlyChanges)
|
||||
@@ -49,7 +48,7 @@ func (s *Server) RegisterRoutes() http.Handler {
|
||||
syncGroup.GET("/quick", sync.SyncGamesQuick)
|
||||
syncGroup.GET("/reset", sync.ResetGames)
|
||||
|
||||
music := api.NewMusic()
|
||||
music := NewMusicHandler()
|
||||
musicGroup := e.Group("/music")
|
||||
musicGroup.GET("", music.GetSong)
|
||||
musicGroup.GET("/soundTest", music.GetSoundCheckSong)
|
||||
|
||||
@@ -2,7 +2,8 @@ package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"music-server/pkg/conf"
|
||||
"log"
|
||||
"music-server/pkg/db"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
@@ -13,6 +14,15 @@ type Server struct {
|
||||
port int
|
||||
}
|
||||
|
||||
var (
|
||||
host = os.Getenv("DB_HOST")
|
||||
dbPort = os.Getenv("DB_PORT")
|
||||
database = os.Getenv("DB_NAME")
|
||||
username = os.Getenv("DB_USERNAME")
|
||||
password = os.Getenv("DB_PASSWORD")
|
||||
musicPath = os.Getenv("MUSIC_PATH")
|
||||
)
|
||||
|
||||
func NewServer() *http.Server {
|
||||
|
||||
port, _ := strconv.Atoi(os.Getenv("PORT"))
|
||||
@@ -20,7 +30,19 @@ func NewServer() *http.Server {
|
||||
port: port,
|
||||
}
|
||||
|
||||
conf.SetupDb()
|
||||
//conf.SetupDb()
|
||||
if host == "" || dbPort == "" || username == "" || password == "" || database == "" || musicPath == "" {
|
||||
log.Fatal("Invalid settings")
|
||||
}
|
||||
|
||||
fmt.Printf("host: %s, dbPort: %v, username: %s, password: %s, dbName: %s\n",
|
||||
host, dbPort, username, password, database)
|
||||
|
||||
log.Printf("Path: %s\n", musicPath)
|
||||
|
||||
db.Migrate_db(host, dbPort, username, password, database)
|
||||
|
||||
db.InitDB(host, dbPort, username, password, database)
|
||||
|
||||
// Declare Server config
|
||||
server := &http.Server{
|
||||
|
||||
47
internal/server/syncHandler.go
Normal file
47
internal/server/syncHandler.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"log"
|
||||
"music-server/internal/backend"
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo"
|
||||
)
|
||||
|
||||
type SyncHandler struct {
|
||||
}
|
||||
|
||||
func NewSyncHandler() *SyncHandler {
|
||||
return &SyncHandler{}
|
||||
}
|
||||
|
||||
func (s *SyncHandler) SyncGames(ctx echo.Context) error {
|
||||
backend.SyncGames()
|
||||
backend.Reset()
|
||||
return ctx.JSON(http.StatusOK, "Games are synced")
|
||||
}
|
||||
|
||||
func (s *SyncHandler) SyncGamesQuick(ctx echo.Context) error {
|
||||
backend.SyncGamesQuick()
|
||||
backend.Reset()
|
||||
return ctx.JSON(http.StatusOK, "Games are synced")
|
||||
}
|
||||
|
||||
func (s *SyncHandler) SyncGamesNewOnlyChanges(ctx echo.Context) error {
|
||||
log.Println("Syncing games new")
|
||||
response := backend.SyncGamesNewOnlyChanges()
|
||||
backend.Reset()
|
||||
return ctx.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
func (s *SyncHandler) SyncGamesNewFull(ctx echo.Context) error {
|
||||
log.Println("Syncing games new full")
|
||||
response := backend.SyncGamesNewFull()
|
||||
backend.Reset()
|
||||
return ctx.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
func (s *SyncHandler) ResetGames(ctx echo.Context) error {
|
||||
backend.ResetDB()
|
||||
return ctx.JSON(http.StatusOK, "Games and songs are deleted from the database")
|
||||
}
|
||||
Reference in New Issue
Block a user