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.

This commit is contained in:
2024-05-18 11:03:03 +02:00
parent 16f2e07bd1
commit a863702b22
8 changed files with 57 additions and 59 deletions

View File

@@ -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
CMD ./MusicServer

View File

@@ -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:

View File

@@ -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)
}

View File

@@ -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 == "" {

View File

@@ -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)

View File

@@ -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.",

View File

@@ -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

View File

@@ -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
}