From a863702b22239bb5462182fe6c73bc519aa789f2 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sat, 18 May 2024 11:03:03 +0200 Subject: [PATCH] Fixed some bugs with songs not found made the application crash. Now checking if song exists and if not, remove song from DB and find another one. Frontend is now decoupled from the backend. --- Dockerfile | 25 ++----------------------- docs/swagger.yaml | 30 +++++++++++++++--------------- pkg/api/music.go | 2 +- pkg/conf/conf.go | 4 ++-- pkg/helpers/helpers.go | 16 ++++++++++++++++ pkg/server/index.go | 8 ++++++-- pkg/server/music.go | 19 +++++++++++++------ pkg/server/sync.go | 12 ++---------- 8 files changed, 57 insertions(+), 59 deletions(-) diff --git a/Dockerfile b/Dockerfile index deb27c5..d817bf3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,18 +1,3 @@ -#FROM node:lts-alpine as build_vue - -# make the 'app' folder the current working directory -#WORKDIR /app - -# copy both 'package.json' and 'package-lock.json' (if available) -#COPY cmd/frontend/package*.json ./ - -# install project dependencies -#RUN npm install - -#COPY cmd/frontend . - -#RUN npm run build - FROM golang:1.16-alpine as build_go COPY go.* /music-server/ @@ -25,27 +10,21 @@ RUN go mod download WORKDIR /music-server/cmd -#COPY --from=build_vue /app/dist ./frontend/dist - RUN go build -o /music-server/MusicServer # Stage 2, distribution container FROM golang:1.16-alpine -RUN apk add --no-cache bash npm EXPOSE 8080 VOLUME /sorted +VOLUME /frontend ENV DB_HOST "" ENV DB_PORT "" ENV DB_USERNAME "" ENV DB_PASSWORD "" ENV DB_NAME "" -ENV HOSTNAME "" COPY --from=build_go /music-server/MusicServer . -COPY cmd/frontend ./frontend/ COPY ./songs/ ./songs/ -COPY ./init.sh . -RUN chmod 777 ./init.sh -CMD ./init.sh \ No newline at end of file +CMD ./MusicServer \ No newline at end of file diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 8bdd07c..ffe5abf 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1,8 +1,8 @@ openapi: 3.0.3 info: - version: "2.3.0" + version: "3.1" title: "Music Server" - description: "Images should not be included in the database, removes songs where the path doesn't work." + description: "Fixed some bugs with songs not found made the application crash. Now checking if song exists and if not, remove song from DB and find another one. Frontend is now decoupled from the backend." contact: email: "zarnor91@gmail.com" servers: @@ -34,21 +34,21 @@ paths: required: true responses: "200": - description: "The speciefied file" + description: "The specified file" content: audio/mpeg: schema: type: object format: binary "500": - description: "Something wnet wrong on the server" - /music/first: + description: "Something went wrong on the server" + /music/soundTest: get: tags: - "Music" - summary: "Start a match" - description: "Get a sound check song and starts a new song que" - operationId: "getFisrt" + summary: "Start a sound test" + description: "Get a sound check song" + operationId: "getSoundCheckSong" responses: "200": description: "A file" @@ -58,7 +58,7 @@ paths: type: object format: binary "500": - description: "Something wnet wrong on the server" + description: "Something went wrong on the server" /music/rand: get: tags: @@ -75,13 +75,13 @@ paths: type: object format: binary "500": - description: "Something wnet wrong on the server" + description: "Something went wrong on the server" /music/rand/low: get: tags: - "Music" summary: "Get random song" - description: "Takes a random song from a random game but increases the chans for games that haven't been played" + description: "Takes a random song from a random game but increases the chance for games that haven't been played" operationId: "getRandomLow" responses: "200": @@ -92,7 +92,7 @@ paths: type: object format: binary "500": - description: "Something wnet wrong on the server" + description: "Something went wrong on the server" /music/info: get: tags: @@ -108,7 +108,7 @@ paths: schema: $ref: '#/components/schemas/info' "500": - description: "Something wnet wrong on the server" + description: "Something went wrong on the server" /music/list: get: tags: @@ -143,7 +143,7 @@ paths: type: object format: binary "500": - description: "Something wnet wrong on the server" + description: "Something went wrong on the server" /music/previous: get: tags: @@ -160,7 +160,7 @@ paths: type: object format: binary "500": - description: "Something wnet wrong on the server" + description: "Something went wrong on the server" /music/all: get: tags: diff --git a/pkg/api/music.go b/pkg/api/music.go index b0861b3..74653be 100644 --- a/pkg/api/music.go +++ b/pkg/api/music.go @@ -24,7 +24,7 @@ func (m *Music) GetSong(ctx *gin.Context) { helpers.SendSong(ctx, s) } -func (m *Music) GetMusicFirst(ctx *gin.Context) { +func (m *Music) GetSoundCheckSong(ctx *gin.Context) { song := server.GetSoundCheckSong() helpers.SendSong(ctx, song) } diff --git a/pkg/conf/conf.go b/pkg/conf/conf.go index 86676f3..a72bedb 100644 --- a/pkg/conf/conf.go +++ b/pkg/conf/conf.go @@ -66,7 +66,7 @@ func SetupRestServer(swagger embed.FS) { musicGroup := router.Group("/music") { musicGroup.GET("", music.GetSong) - musicGroup.GET("first", music.GetMusicFirst) + musicGroup.GET("soundTest", music.GetSoundCheckSong) musicGroup.GET("reset", music.ResetMusic) musicGroup.GET("rand", music.GetRandomSong) musicGroup.GET("rand/low", music.GetRandomSongLowChance) @@ -86,7 +86,7 @@ func SetupRestServer(swagger embed.FS) { router.GET("/version", index.GetVersion) router.GET("/test", index.GetDBTest) router.StaticFS("/swagger", helpers.EmbedFolder(swagger, "swagger", false)) - router.Use(static.Serve("/", static.LocalFile("frontend/dist", true))) + router.Use(static.Serve("/", static.LocalFile("/frontend", true))) port := os.Getenv("PORT") if port == "" { diff --git a/pkg/helpers/helpers.go b/pkg/helpers/helpers.go index 1b3ba60..5c13449 100644 --- a/pkg/helpers/helpers.go +++ b/pkg/helpers/helpers.go @@ -7,6 +7,8 @@ import ( "github.com/gin-gonic/gin" "io" "io/fs" + "music-server/pkg/db" + "music-server/pkg/models" "net/http" "os" "strconv" @@ -60,6 +62,20 @@ func EmbedFolder(fsEmbed embed.FS, targetPath string, index bool) static.ServeFi } } +func CheckIfSongExists(song models.SongData) bool { + //Check if file exists and open + openFile, err := os.Open(song.Path) + if err != nil { + //File not found + db.RemoveBrokenSong(song) + return false + } + defer func(openFile *os.File) { + _ = openFile.Close() + }(openFile) //Close after function return + return true +} + func SendSong(ctx *gin.Context, Filename string) { fmt.Println("Client requests: " + Filename) diff --git a/pkg/server/index.go b/pkg/server/index.go index 311d97a..ca03b6c 100644 --- a/pkg/server/index.go +++ b/pkg/server/index.go @@ -10,9 +10,13 @@ func TestDB() { } func GetVersionHistory() models.VersionData { - data := models.VersionData{Version: "3.0", - Changelog: "Changed routing framework from mux to Gin. Swagger doc is now included in the application. A fronted can now be hosted from the application.", + data := models.VersionData{Version: "3.1", + Changelog: "Fixed some bugs with songs not found made the application crash. Now checking if song exists and if not, remove song from DB and find another one. Frontend is now decoupled from the backend.", History: []models.VersionData{ + { + Version: "3.0", + Changelog: "Changed routing framework from mux to Gin. Swagger doc is now included in the application. A fronted can now be hosted from the application.", + }, { Version: "2.3.0", Changelog: "Images should not be included in the database, removes songs where the path doesn't work.", diff --git a/pkg/server/music.go b/pkg/server/music.go index dca83a6..12243ab 100644 --- a/pkg/server/music.go +++ b/pkg/server/music.go @@ -5,6 +5,7 @@ import ( "log" "math/rand" "music-server/pkg/db" + "music-server/pkg/helpers" "music-server/pkg/models" "os" "strconv" @@ -17,8 +18,6 @@ var songQue []models.SongData var lastFetched models.SongData func GetSoundCheckSong() string { - Reset() - files, err := ioutil.ReadDir("songs") if err != nil { log.Fatal(err) @@ -51,9 +50,13 @@ func GetRandomSong() string { if games == nil || len(games) == 0 { games = db.FindAllGames() } + songExists := false + var song models.SongData - song := getSongFromList(games) - + for !songExists { + song = getSongFromList(games) + songExists = helpers.CheckIfSongExists(song) + } lastFetched = song return song.Path } @@ -75,8 +78,12 @@ func GetRandomSongLowChance() string { } } } - - song := getSongFromList(listOfGames) + songExists := false + var song models.SongData + for !songExists { + song = getSongFromList(listOfGames) + songExists = helpers.CheckIfSongExists(song) + } lastFetched = song return song.Path diff --git a/pkg/server/sync.go b/pkg/server/sync.go index 337e32d..4bda449 100644 --- a/pkg/server/sync.go +++ b/pkg/server/sync.go @@ -109,28 +109,20 @@ func addNewGame(name string, path string) { } func checkSongs(gameDir string, gameId int) { - songs := make([]models.SongData, 0) - findSongsFromGame := db.FindSongsFromGame(gameId) - files, err := ioutil.ReadDir(gameDir) if err != nil { log.Println(err) } + db.ClearSongs(gameId) for _, entry := range files { path := gameDir + entry.Name() fileName := entry.Name() if isSong(entry) { - songs = append(songs, models.SongData{GameId: gameId, SongName: fileName, Path: path}) + db.AddSong(models.SongData{GameId: gameId, SongName: fileName, Path: path}) } else if isCoverImage(entry) { //TODO: Later add cover art image here in db } } - if len(songs) != len(findSongsFromGame) { - db.ClearSongs(gameId) - for _, song := range songs { - db.AddSong(song) - } - } //TODO: Add number of songs here }