06cbad708d
- 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>
41 lines
1.0 KiB
Go
41 lines
1.0 KiB
Go
package backend
|
|
|
|
import (
|
|
"context"
|
|
"music-server/internal/db/repository"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
// Global variables - these are initialized by InitBackend
|
|
var (
|
|
backendPool *pgxpool.Pool
|
|
repo *repository.Queries
|
|
backendCtx context.Context = context.Background()
|
|
)
|
|
|
|
// InitBackend initializes the backend package with the database pool.
|
|
// This should be called once at application startup.
|
|
func InitBackend(pool *pgxpool.Pool) {
|
|
backendPool = pool
|
|
repo = repository.New(pool)
|
|
backendCtx = context.Background()
|
|
}
|
|
|
|
// BackendCtx returns the context used by backend operations.
|
|
// This is exposed for use by the backend functions.
|
|
func BackendCtx() context.Context {
|
|
return backendCtx
|
|
}
|
|
|
|
// BackendRepo returns the repository queries instance.
|
|
// This is exposed for use by the backend functions.
|
|
func BackendRepo() *repository.Queries {
|
|
return repo
|
|
}
|
|
|
|
// BackendPool returns the underlying database pool.
|
|
// This is exposed for test utilities that need direct pool access.
|
|
func BackendPool() *pgxpool.Pool {
|
|
return backendPool
|
|
}
|