#1 - Created request to check newest version of the app
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
This commit is contained in:
2025-08-23 11:36:03 +02:00
parent 0d1c69d95e
commit 806e88adeb
26 changed files with 673 additions and 675 deletions

View File

@@ -0,0 +1,41 @@
package server
import (
"github.com/labstack/echo/v4"
"log"
"music-server/internal/backend"
"net/http"
)
type DownloadHandler struct {
}
func NewDownloadHandler() *DownloadHandler {
return &DownloadHandler{}
}
func (d *DownloadHandler) checkLatest(ctx echo.Context) error {
log.Println("Checking latest version")
latest := backend.CheckLatest()
return ctx.JSON(http.StatusOK, latest)
}
func (d *DownloadHandler) listAssetsOfLatest(ctx echo.Context) error {
log.Println("Listing assets")
assets := backend.ListAssetsOfLatest()
return ctx.JSON(http.StatusOK, assets)
}
func (d *DownloadHandler) downloadLatestWindows(ctx echo.Context) error {
log.Println("Downloading latest windows")
asset := backend.DownloadLatestWindows()
ctx.Response().Header().Set("Content-Type", "application/octet-stream")
return ctx.Redirect(http.StatusFound, asset)
}
func (d *DownloadHandler) downloadLatestLinux(ctx echo.Context) error {
log.Println("Downloading latest linux")
asset := backend.DownloadLatestLinux()
ctx.Response().Header().Set("Content-Type", "application/octet-stream")
return ctx.Redirect(http.StatusFound, asset)
}

View File

@@ -1,6 +1,7 @@
package server
import (
"log"
"music-server/internal/backend"
"net/http"
"os"
@@ -16,6 +17,10 @@ func NewMusicHandler() *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")
@@ -30,6 +35,10 @@ func (m *MusicHandler) GetSong(ctx echo.Context) error {
}
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 {
@@ -40,11 +49,19 @@ func (m *MusicHandler) GetSoundCheckSong(ctx echo.Context) error {
}
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 {
@@ -55,6 +72,10 @@ func (m *MusicHandler) GetRandomSong(ctx echo.Context) error {
}
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 {
@@ -65,6 +86,10 @@ func (m *MusicHandler) GetRandomSongLowChance(ctx echo.Context) error {
}
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 {
@@ -85,6 +110,10 @@ func (m *MusicHandler) GetPlayedSongs(ctx echo.Context) error {
}
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 {
@@ -95,6 +124,10 @@ func (m *MusicHandler) GetNextSong(ctx echo.Context) error {
}
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 {
@@ -105,11 +138,19 @@ func (m *MusicHandler) GetPreviousSong(ctx echo.Context) error {
}
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)
}
@@ -119,6 +160,10 @@ type played struct {
}
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 {
@@ -129,11 +174,19 @@ func (m *MusicHandler) PutPlayed(ctx echo.Context) error {
}
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)
}

View File

@@ -12,10 +12,22 @@ import (
"github.com/a-h/templ"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
echoSwagger "github.com/swaggo/echo-swagger" // echo-swagger middleware
"github.com/swaggo/echo-swagger" // echo-swagger middleware
//_ "github.com/swaggo/echo-swagger/example/docs" // docs is generated by Swag CLI, you have to import it.
)
// @Title Swagger Example API
// @version 0.5
// @description This is a sample server Petstore server.
// @termsOfService http://swagger.io/terms/
// @contact.name Sebastian Olsson
// @contact.email zarnor91@gmail.com
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8080
func (s *Server) RegisterRoutes() http.Handler {
e := echo.New()
e.Use(middleware.Logger())
@@ -37,15 +49,15 @@ func (s *Server) RegisterRoutes() http.Handler {
e.Static("/", "/frontend")
swagger := http.FileServer(http.FS(web.Swagger))
e.GET("/swagger/*", echo.WrapHandler(swagger))
/*swagger := http.FileServer(http.FS(web.Swagger))
e.GET("/swagger/*", echo.WrapHandler(swagger))*/
swaggerRedirect := func(c echo.Context) error {
return c.Redirect(http.StatusMovedPermanently, "/doc/index.html")
return c.Redirect(http.StatusMovedPermanently, "/swagger/index.html")
}
e.GET("/doc", swaggerRedirect)
e.GET("/doc/", swaggerRedirect)
e.GET("/doc/*", echoSwagger.WrapHandler)
e.GET("/swagger", swaggerRedirect)
e.GET("/swagger/", swaggerRedirect)
e.GET("/swagger/*", echoSwagger.WrapHandler)
index := NewIndexHandler()
e.GET("/version", index.GetVersion)
@@ -54,12 +66,19 @@ func (s *Server) RegisterRoutes() http.Handler {
e.GET("/character", index.GetCharacter)
e.GET("/characters", index.GetCharacters)
download := NewDownloadHandler()
e.GET("/download", download.checkLatest)
e.GET("/download/list", download.listAssetsOfLatest)
e.GET("/download/windows", download.downloadLatestWindows)
e.GET("/download/linux", download.downloadLatestLinux)
sync := NewSyncHandler()
syncGroup := e.Group("/sync")
syncGroup.GET("", sync.SyncGames)
syncGroup.GET("", sync.SyncGamesNewOnlyChanges)
syncGroup.GET("/progress", sync.SyncProgress)
syncGroup.GET("/new", sync.SyncGamesNewOnlyChanges)
syncGroup.GET("/new/full", sync.SyncGamesNewFull)
syncGroup.GET("/quick", sync.SyncGamesQuick)
syncGroup.GET("/quick", sync.SyncGamesNewOnlyChanges)
syncGroup.GET("/reset", sync.ResetGames)
music := NewMusicHandler()

View File

@@ -15,12 +15,13 @@ type Server struct {
}
var (
host = os.Getenv("DB_HOST")
dbPort = os.Getenv("DB_PORT")
dbName = os.Getenv("DB_NAME")
username = os.Getenv("DB_USERNAME")
password = os.Getenv("DB_PASSWORD")
musicPath = os.Getenv("MUSIC_PATH")
host = os.Getenv("DB_HOST")
dbPort = os.Getenv("DB_PORT")
dbName = os.Getenv("DB_NAME")
username = os.Getenv("DB_USERNAME")
password = os.Getenv("DB_PASSWORD")
musicPath = os.Getenv("MUSIC_PATH")
charactersPath = os.Getenv("CHARACTERS_PATH")
)
func NewServer() *http.Server {
@@ -30,15 +31,16 @@ func NewServer() *http.Server {
port: port,
}
//conf.SetupDb()
if host == "" || dbPort == "" || username == "" || password == "" || dbName == "" || musicPath == "" {
log.Fatal("Invalid settings")
}
fmt.Printf("host: %s, dbPort: %v, username: %s, password: %s, dbName: %s\n",
host, dbPort, username, password, dbName)
log.Printf("Path: %s\n", musicPath)
log.Printf("musicPath: %s\n", musicPath)
log.Printf("charactersPath: %s\n", charactersPath)
//conf.SetupDb()
if host == "" || dbPort == "" || username == "" || password == "" || dbName == "" || musicPath == "" || charactersPath == "" {
log.Fatal("Invalid settings")
}
db.Migrate_db(host, dbPort, username, password, dbName)

View File

@@ -3,7 +3,6 @@ package server
import (
"log"
"music-server/internal/backend"
"music-server/internal/database"
"net/http"
"github.com/labstack/echo/v4"
@@ -16,33 +15,42 @@ func NewSyncHandler() *SyncHandler {
return &SyncHandler{}
}
func (s *SyncHandler) SyncGames(ctx echo.Context) error {
database.SyncGames()
backend.Reset()
return ctx.JSON(http.StatusOK, "Games are synced")
}
func (s *SyncHandler) SyncGamesQuick(ctx echo.Context) error {
database.SyncGamesQuick()
backend.Reset()
return ctx.JSON(http.StatusOK, "Games are synced")
func (s *SyncHandler) SyncProgress(ctx echo.Context) error {
if backend.Syncing {
log.Println("Getting progress")
response := backend.SyncProgress()
return ctx.JSON(http.StatusOK, response)
}
log.Println("Getting result")
response := backend.SyncResult()
return ctx.JSON(http.StatusOK, response)
}
func (s *SyncHandler) SyncGamesNewOnlyChanges(ctx echo.Context) error {
log.Println("Syncing games new")
response := backend.SyncGamesNewOnlyChanges()
backend.Reset()
return ctx.JSON(http.StatusOK, response)
if backend.Syncing {
log.Println("Syncing is in progress")
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
}
log.Println("Start syncing games")
go backend.SyncGamesNewOnlyChanges()
return ctx.JSON(http.StatusOK, "Start syncing games")
}
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)
if backend.Syncing {
log.Println("Syncing is in progress")
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
}
log.Println("Start syncing games full")
go backend.SyncGamesNewFull()
return ctx.JSON(http.StatusOK, "Start syncing games full")
}
func (s *SyncHandler) ResetGames(ctx echo.Context) error {
if backend.Syncing {
log.Println("Syncing is in progress")
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
}
backend.ResetDB()
return ctx.JSON(http.StatusOK, "Games and songs are deleted from the database")
}