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 3e37303979
commit 98c1948eff
11 changed files with 320 additions and 100 deletions
+19 -1
View File
@@ -8,6 +8,9 @@ import (
"testing"
"time"
"music-server/internal/backend"
"music-server/internal/db"
"github.com/labstack/echo/v5"
)
@@ -45,8 +48,23 @@ func StartTestServer(t *testing.T) *echo.Echo {
os.Setenv("LOG_JSON", "false")
}
// Initialize database for tests
db.TestSetupDB(t)
// Initialize backend with the global Dbpool
// This ensures BackendRepo() and BackendCtx() are available
if db.Dbpool != nil {
backend.InitBackend(db.Dbpool)
}
// Create a Server instance and get its routes
s := &Server{}
s := &Server{
db: &db.Database{
Pool: db.Dbpool,
Ctx: db.Ctx,
},
tokenHandler: NewTokenHandler(db.Dbpool),
}
handler := s.RegisterRoutes()
// Wrap the http.Handler in an echo.Echo