feat: Remove global db.Dbpool with dependency injection (Phase 0)

- Add Database struct in internal/db/database.go with Pool, Ctx, and RunMigrations()
- Update server.go to use Database struct with NewServerInstance()
- Add backend.go with InitBackend(), BackendRepo(), BackendCtx(), BackendPool()
- Update music.go and sync.go to use BackendRepo() and BackendCtx() instead of db.Dbpool/db.Ctx
- Update token_handler.go to accept pool parameter
- Update routes.go to use s.db.Pool for middleware
- Update cmd/main.go to use NewServerInstance() and HTTPServer()
- Update test_helpers.go to initialize backend with test database
- Update test files to use backend.BackendPool() and backend.BackendCtx()

Benefits:
- Easier to mock database for unit tests
- Follows Go best practices (dependency injection)
- Better architecture with explicit dependencies
- RunMigrations() replaces old Migrate_db() function

Note: Global db.Dbpool and db.Ctx still exist in dbHelper.go for backward compatibility
with test_helpers.go, but production code no longer uses them.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
2026-06-01 18:50:05 +02:00
parent 89e884fae9
commit 06cbad708d
11 changed files with 320 additions and 100 deletions
+17 -16
View File
@@ -2,7 +2,6 @@ package backend
import (
"math/rand"
"music-server/internal/db"
"music-server/internal/db/repository"
"music-server/internal/logging"
"os"
@@ -28,18 +27,20 @@ var gamesNew []repository.Game
var songQueNew []repository.Song
var lastFetchedNew repository.Song
var repo *repository.Queries
func initRepo() {
if repo == nil {
repo = repository.New(db.Dbpool)
// This function is kept for backward compatibility
// but now uses the backend package's initialized repo
// If not initialized, this will panic intentionally
if BackendRepo() == nil {
panic("backend not initialized - call backend.InitBackend() first")
}
}
func getAllGames() []repository.Game {
if len(gamesNew) == 0 {
initRepo()
gamesNew, _ = repo.FindAllGames(db.Ctx)
gamesNew, _ = BackendRepo().FindAllGames(BackendCtx())
}
return gamesNew
@@ -58,7 +59,7 @@ func Reset() {
songQueNew = nil
currentSong = -1
initRepo()
gamesNew, _ = repo.FindAllGames(db.Ctx)
gamesNew, _ = BackendRepo().FindAllGames(BackendCtx())
}
func AddLatestToQue() {
@@ -76,8 +77,8 @@ func AddLatestPlayed() {
currentSongData := songQueNew[currentSong]
initRepo()
repo.AddGamePlayed(db.Ctx, currentSongData.GameID)
repo.AddSongPlayed(db.Ctx, repository.AddSongPlayedParams{GameID: currentSongData.GameID, SongName: currentSongData.SongName})
BackendRepo().AddGamePlayed(BackendCtx(), currentSongData.GameID)
BackendRepo().AddSongPlayed(BackendCtx(), repository.AddSongPlayedParams{GameID: currentSongData.GameID, SongName: currentSongData.SongName})
}
func SetPlayed(songNumber int) {
@@ -86,8 +87,8 @@ func SetPlayed(songNumber int) {
}
songData := songQueNew[songNumber]
initRepo()
repo.AddGamePlayed(db.Ctx, songData.GameID)
repo.AddSongPlayed(db.Ctx, repository.AddSongPlayedParams{GameID: songData.GameID, SongName: songData.SongName})
BackendRepo().AddGamePlayed(BackendCtx(), songData.GameID)
BackendRepo().AddSongPlayed(BackendCtx(), repository.AddSongPlayedParams{GameID: songData.GameID, SongName: songData.SongName})
}
func GetRandomSong() string {
@@ -130,7 +131,7 @@ func GetRandomSongClassic() string {
var listOfAllSongs []repository.Song
for _, game := range gamesNew {
songList, _ := repo.FindSongsFromGame(db.Ctx, game.ID)
songList, _ := BackendRepo().FindSongsFromGame(BackendCtx(), game.ID)
listOfAllSongs = append(listOfAllSongs, songList...)
}
@@ -138,10 +139,10 @@ func GetRandomSongClassic() string {
var song repository.Song
for !songFound {
song = listOfAllSongs[rand.Intn(len(listOfAllSongs))]
gameData, err := repo.GetGameById(db.Ctx, song.GameID)
gameData, err := BackendRepo().GetGameById(BackendCtx(), song.GameID)
if err != nil {
repo.RemoveBrokenSong(db.Ctx, song.Path)
BackendRepo().RemoveBrokenSong(BackendCtx(), song.Path)
logging.GetLogger().Warn("Song not found, removed from database",
zap.String("song", song.SongName),
zap.String("game", gameData.GameName),
@@ -153,7 +154,7 @@ func GetRandomSongClassic() string {
openFile, err := os.Open(song.Path)
if err != nil || (song.FileName != nil && gameData.Path+*song.FileName != song.Path) {
//File not found
repo.RemoveBrokenSong(db.Ctx, song.Path)
BackendRepo().RemoveBrokenSong(BackendCtx(), song.Path)
logging.GetLogger().Warn("Song not found, removed from database",
zap.String("song", song.SongName),
zap.String("game", gameData.GameName),
@@ -270,7 +271,7 @@ func getSongFromList(games []repository.Game) repository.Song {
var song repository.Song
for !songFound {
game := getRandomGame(games)
songs, _ := repo.FindSongsFromGame(db.Ctx, game.ID)
songs, _ := BackendRepo().FindSongsFromGame(BackendCtx(), game.ID)
if len(songs) == 0 {
continue
}
@@ -281,7 +282,7 @@ func getSongFromList(games []repository.Game) repository.Song {
openFile, err := os.Open(song.Path)
if err != nil || (song.FileName != nil && game.Path+*song.FileName != song.Path) || (song.FileName != nil && strings.HasSuffix(*song.FileName, ".wav")) {
//File not found
repo.RemoveBrokenSong(db.Ctx, song.Path)
BackendRepo().RemoveBrokenSong(BackendCtx(), song.Path)
logging.GetLogger().Warn("Song not found, removed from database",
zap.String("song", song.SongName),
zap.String("game", game.GameName),