Upgraded Go version and the version of all dependencies. Fixed som more bugs.

This commit is contained in:
2024-05-18 13:11:15 +02:00
parent a863702b22
commit 59f1e2c75c
11 changed files with 227 additions and 1111 deletions

View File

@@ -94,5 +94,8 @@ func SetupRestServer(swagger embed.FS) {
log.Printf("Defaulting to port %s", port)
}
log.Printf("Open http://localhost:%s in the browser", port)
router.Run(fmt.Sprintf(":%s", port))
err := router.Run(fmt.Sprintf(":%s", port))
if err != nil {
panic(err)
}
}

View File

@@ -3,11 +3,11 @@ package db
import (
"context"
"fmt"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/jackc/pgx/v5"
"os"
)
var pool *pgxpool.Pool
var pool *pgx.Conn
func InitDB(host string, port int, user string, password string, dbname string) {
@@ -18,7 +18,7 @@ func InitDB(host string, port int, user string, password string, dbname string)
fmt.Println(psqlInfo)
var err error
pool, err = pgxpool.Connect(context.Background(), psqlInfo)
pool, err = pgx.Connect(context.Background(), psqlInfo)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
@@ -30,11 +30,15 @@ func InitDB(host string, port int, user string, password string, dbname string)
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
os.Exit(1)
}
Testf()
fmt.Println(success)
}
func CloseDb() {
pool.Close()
err := pool.Close(context.Background())
if err != nil {
return
}
}
func Testf() {

View File

@@ -7,8 +7,6 @@ import (
"github.com/gin-gonic/gin"
"io"
"io/fs"
"music-server/pkg/db"
"music-server/pkg/models"
"net/http"
"os"
"strconv"
@@ -36,7 +34,7 @@ type embedFileSystem struct {
indexes bool
}
func (e embedFileSystem) Exists(prefix string, path string) bool {
func (e embedFileSystem) Exists(_ string, path string) bool {
f, err := e.Open(path)
if err != nil {
return false
@@ -62,20 +60,6 @@ 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.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.",
data := models.VersionData{Version: "3.2",
Changelog: "Upgraded Go version and the version of all dependencies. Fixed som more bugs.",
History: []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.",
},
{
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.",

View File

@@ -1,15 +1,12 @@
package server
import (
"io/ioutil"
"log"
"math/rand"
"music-server/pkg/db"
"music-server/pkg/helpers"
"music-server/pkg/models"
"os"
"strconv"
"time"
)
var currentSong = -1
@@ -18,7 +15,7 @@ var songQue []models.SongData
var lastFetched models.SongData
func GetSoundCheckSong() string {
files, err := ioutil.ReadDir("songs")
files, err := os.ReadDir("songs")
if err != nil {
log.Fatal(err)
}
@@ -50,13 +47,8 @@ func GetRandomSong() string {
if games == nil || len(games) == 0 {
games = db.FindAllGames()
}
songExists := false
var song models.SongData
for !songExists {
song = getSongFromList(games)
songExists = helpers.CheckIfSongExists(song)
}
song := getSongFromList(games)
lastFetched = song
return song.Path
}
@@ -78,12 +70,7 @@ func GetRandomSongLowChance() string {
}
}
}
songExists := false
var song models.SongData
for !songExists {
song = getSongFromList(listOfGames)
songExists = helpers.CheckIfSongExists(song)
}
song := getSongFromList(games)
lastFetched = song
return song.Path
@@ -157,7 +144,6 @@ func GetAllGamesRandom() []string {
for _, game := range games {
jsonArray = append(jsonArray, game.GameName)
}
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(jsonArray), func(i, j int) { jsonArray[i], jsonArray[j] = jsonArray[j], jsonArray[i] })
return jsonArray
}
@@ -212,7 +198,7 @@ func getSongFromList(games []models.GameData) models.SongData {
if err != nil {
//File not found
db.RemoveBrokenSong(song)
log.Fatal("Song not found, song '" + song.SongName + "' deleted from game '" + game.GameName + "' songPath: " + song.Path)
log.Println("Song not found, song '" + song.SongName + "' deleted from game '" + game.GameName + "' songPath: " + song.Path)
} else {
songFound = true
}

View File

@@ -3,7 +3,6 @@ package server
import (
"fmt"
"io/fs"
"io/ioutil"
"log"
"music-server/pkg/db"
"music-server/pkg/models"
@@ -27,7 +26,7 @@ func SyncGames() {
db.SetGameDeletionDate()
checkBrokenSongs()
files, err := ioutil.ReadDir(dir)
files, err := os.ReadDir(dir)
if err != nil {
log.Fatal(err)
}
@@ -38,13 +37,17 @@ func SyncGames() {
path := dir + file.Name() + "/"
fmt.Println(path)
innerFiles, err := ioutil.ReadDir(path)
entries, err := os.ReadDir(path)
if err != nil {
log.Println(err)
}
id := -1
for _, song := range innerFiles {
id = getIdFromFile(song)
for _, entry := range entries {
fileInfo, err := entry.Info()
if err != nil {
log.Println(err)
}
id = getIdFromFile(fileInfo)
if id != -1 {
break
}
@@ -87,6 +90,7 @@ func checkIfChanged(id int, name string, path string) {
} else if name != nameFromDb {
fmt.Println("Diff name")
db.UpdateGameName(id, name, path)
checkBrokenSongs()
}
db.RemoveDeletionDate(id)
}
@@ -101,7 +105,7 @@ func addNewGame(name string, path string) {
fileName := path + "/." + strconv.Itoa(newId) + ".id"
fmt.Printf("fileName = %v", fileName)
err := ioutil.WriteFile(fileName, nil, 0644)
err := os.WriteFile(fileName, nil, 0644)
if err != nil {
panic(err)
}
@@ -109,12 +113,15 @@ func addNewGame(name string, path string) {
}
func checkSongs(gameDir string, gameId int) {
files, err := ioutil.ReadDir(gameDir)
files, err := os.ReadDir(gameDir)
if err != nil {
log.Println(err)
}
db.ClearSongs(gameId)
for _, entry := range files {
for _, file := range files {
entry, err := file.Info()
if err != nil {
log.Println(err)
}
path := gameDir + entry.Name()
fileName := entry.Name()
if isSong(entry) {