package storage import ( "context" "fmt" "os" "path/filepath" "strings" internallog "envoy-control-plane/internal/log" _ "github.com/lib/pq" // Postgres driver _ "github.com/mattn/go-sqlite3" // Import SQLite driver" // Import drivers in cmd/main.go or just keep them in the original main.go ) // determineDriver returns driver name from connection string func determineDriver(dsn string) string { if strings.HasPrefix(dsn, "postgres://") || strings.HasPrefix(dsn, "postgresql://") { return "postgres" } return "sqlite3" } // SetupDBConnection determines the DB connection string and driver. func SetupDBConnection(ctx context.Context, dbConnStrIn string) (connStr, driver string, err error) { log := internallog.LogFromContext(ctx) connStr = dbConnStrIn // Default DB to SQLite file if none provided if connStr == "" { defaultDBPath := "data/config.db" if err := os.MkdirAll(filepath.Dir(defaultDBPath), 0755); err != nil { return "", "", fmt.Errorf("failed to create data directory: %w", err) } connStr = fmt.Sprintf("file:%s?_foreign_keys=on", defaultDBPath) driver = "sqlite3" } else { driver = determineDriver(connStr) } log.Debugf("Using database driver: %s", driver) return connStr, driver, nil }