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 FROM golang:1.16-alpine as build_go
COPY go.* /music-server/ COPY go.* /music-server/
@@ -25,27 +10,21 @@ RUN go mod download
WORKDIR /music-server/cmd WORKDIR /music-server/cmd
#COPY --from=build_vue /app/dist ./frontend/dist
RUN go build -o /music-server/MusicServer RUN go build -o /music-server/MusicServer
# Stage 2, distribution container # Stage 2, distribution container
FROM golang:1.16-alpine FROM golang:1.16-alpine
RUN apk add --no-cache bash npm
EXPOSE 8080 EXPOSE 8080
VOLUME /sorted VOLUME /sorted
VOLUME /frontend
ENV DB_HOST "" ENV DB_HOST ""
ENV DB_PORT "" ENV DB_PORT ""
ENV DB_USERNAME "" ENV DB_USERNAME ""
ENV DB_PASSWORD "" ENV DB_PASSWORD ""
ENV DB_NAME "" ENV DB_NAME ""
ENV HOSTNAME ""
COPY --from=build_go /music-server/MusicServer . COPY --from=build_go /music-server/MusicServer .
COPY cmd/frontend ./frontend/
COPY ./songs/ ./songs/ 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 openapi: 3.0.3
info: info:
version: "2.3.0" version: "3.1"
title: "Music Server" 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: contact:
email: "zarnor91@gmail.com" email: "zarnor91@gmail.com"
servers: servers:
@@ -34,21 +34,21 @@ paths:
required: true required: true
responses: responses:
"200": "200":
description: "The speciefied file" description: "The specified file"
content: content:
audio/mpeg: audio/mpeg:
schema: schema:
type: object type: object
format: binary format: binary
"500": "500":
description: "Something wnet wrong on the server" description: "Something went wrong on the server"
/music/first: /music/soundTest:
get: get:
tags: tags:
- "Music" - "Music"
summary: "Start a match" summary: "Start a sound test"
description: "Get a sound check song and starts a new song que" description: "Get a sound check song"
operationId: "getFisrt" operationId: "getSoundCheckSong"
responses: responses:
"200": "200":
description: "A file" description: "A file"
@@ -58,7 +58,7 @@ paths:
type: object type: object
format: binary format: binary
"500": "500":
description: "Something wnet wrong on the server" description: "Something went wrong on the server"
/music/rand: /music/rand:
get: get:
tags: tags:
@@ -75,13 +75,13 @@ paths:
type: object type: object
format: binary format: binary
"500": "500":
description: "Something wnet wrong on the server" description: "Something went wrong on the server"
/music/rand/low: /music/rand/low:
get: get:
tags: tags:
- "Music" - "Music"
summary: "Get random song" 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" operationId: "getRandomLow"
responses: responses:
"200": "200":
@@ -92,7 +92,7 @@ paths:
type: object type: object
format: binary format: binary
"500": "500":
description: "Something wnet wrong on the server" description: "Something went wrong on the server"
/music/info: /music/info:
get: get:
tags: tags:
@@ -108,7 +108,7 @@ paths:
schema: schema:
$ref: '#/components/schemas/info' $ref: '#/components/schemas/info'
"500": "500":
description: "Something wnet wrong on the server" description: "Something went wrong on the server"
/music/list: /music/list:
get: get:
tags: tags:
@@ -143,7 +143,7 @@ paths:
type: object type: object
format: binary format: binary
"500": "500":
description: "Something wnet wrong on the server" description: "Something went wrong on the server"
/music/previous: /music/previous:
get: get:
tags: tags:
@@ -160,7 +160,7 @@ paths:
type: object type: object
format: binary format: binary
"500": "500":
description: "Something wnet wrong on the server" description: "Something went wrong on the server"
/music/all: /music/all:
get: get:
tags: tags:

View File

@@ -24,7 +24,7 @@ func (m *Music) GetSong(ctx *gin.Context) {
helpers.SendSong(ctx, s) helpers.SendSong(ctx, s)
} }
func (m *Music) GetMusicFirst(ctx *gin.Context) { func (m *Music) GetSoundCheckSong(ctx *gin.Context) {
song := server.GetSoundCheckSong() song := server.GetSoundCheckSong()
helpers.SendSong(ctx, song) helpers.SendSong(ctx, song)
} }

View File

@@ -66,7 +66,7 @@ func SetupRestServer(swagger embed.FS) {
musicGroup := router.Group("/music") musicGroup := router.Group("/music")
{ {
musicGroup.GET("", music.GetSong) musicGroup.GET("", music.GetSong)
musicGroup.GET("first", music.GetMusicFirst) musicGroup.GET("soundTest", music.GetSoundCheckSong)
musicGroup.GET("reset", music.ResetMusic) musicGroup.GET("reset", music.ResetMusic)
musicGroup.GET("rand", music.GetRandomSong) musicGroup.GET("rand", music.GetRandomSong)
musicGroup.GET("rand/low", music.GetRandomSongLowChance) musicGroup.GET("rand/low", music.GetRandomSongLowChance)
@@ -86,7 +86,7 @@ func SetupRestServer(swagger embed.FS) {
router.GET("/version", index.GetVersion) router.GET("/version", index.GetVersion)
router.GET("/test", index.GetDBTest) router.GET("/test", index.GetDBTest)
router.StaticFS("/swagger", helpers.EmbedFolder(swagger, "swagger", false)) 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") port := os.Getenv("PORT")
if port == "" { if port == "" {

View File

@@ -7,6 +7,8 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"io" "io"
"io/fs" "io/fs"
"music-server/pkg/db"
"music-server/pkg/models"
"net/http" "net/http"
"os" "os"
"strconv" "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) { func SendSong(ctx *gin.Context, Filename string) {
fmt.Println("Client requests: " + Filename) fmt.Println("Client requests: " + Filename)

View File

@@ -10,9 +10,13 @@ func TestDB() {
} }
func GetVersionHistory() models.VersionData { func GetVersionHistory() models.VersionData {
data := models.VersionData{Version: "3.0", data := models.VersionData{Version: "3.1",
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.", 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{ 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", Version: "2.3.0",
Changelog: "Images should not be included in the database, removes songs where the path doesn't work.", 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" "log"
"math/rand" "math/rand"
"music-server/pkg/db" "music-server/pkg/db"
"music-server/pkg/helpers"
"music-server/pkg/models" "music-server/pkg/models"
"os" "os"
"strconv" "strconv"
@@ -17,8 +18,6 @@ var songQue []models.SongData
var lastFetched models.SongData var lastFetched models.SongData
func GetSoundCheckSong() string { func GetSoundCheckSong() string {
Reset()
files, err := ioutil.ReadDir("songs") files, err := ioutil.ReadDir("songs")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@@ -51,9 +50,13 @@ func GetRandomSong() string {
if games == nil || len(games) == 0 { if games == nil || len(games) == 0 {
games = db.FindAllGames() games = db.FindAllGames()
} }
songExists := false
var song models.SongData
song := getSongFromList(games) for !songExists {
song = getSongFromList(games)
songExists = helpers.CheckIfSongExists(song)
}
lastFetched = song lastFetched = song
return song.Path return song.Path
} }
@@ -75,8 +78,12 @@ func GetRandomSongLowChance() string {
} }
} }
} }
songExists := false
song := getSongFromList(listOfGames) var song models.SongData
for !songExists {
song = getSongFromList(listOfGames)
songExists = helpers.CheckIfSongExists(song)
}
lastFetched = song lastFetched = song
return song.Path return song.Path

View File

@@ -109,28 +109,20 @@ func addNewGame(name string, path string) {
} }
func checkSongs(gameDir string, gameId int) { func checkSongs(gameDir string, gameId int) {
songs := make([]models.SongData, 0)
findSongsFromGame := db.FindSongsFromGame(gameId)
files, err := ioutil.ReadDir(gameDir) files, err := ioutil.ReadDir(gameDir)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
db.ClearSongs(gameId)
for _, entry := range files { for _, entry := range files {
path := gameDir + entry.Name() path := gameDir + entry.Name()
fileName := entry.Name() fileName := entry.Name()
if isSong(entry) { 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) { } else if isCoverImage(entry) {
//TODO: Later add cover art image here in db //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 //TODO: Add number of songs here
} }