Files
terminalShop-old/pkg/database/db.go
marcusDenslow 062242e346 feat: add browser-based card collection via Stripe Checkout Session
adds a /pay/{token} redirect endpoint that generates a short URL
pointing to a stripe checkout sessions in setup mode. The TUI shows a QR
code and URL on the payment page so users can add a card via the browser
instead of entering raw card data over SSH.

the checkout.session.completed webhook saves the card to the database.
the TUI polls GET /api/v1/cards every second and auto-proceeds to
confirmation once the new card is detected.

also adds a dev.sh startup script that runs all four required processes.
(SSH server, API server, Stripe CLI listener, ngrok) in a single
terminal
2026-04-08 01:03:43 +02:00

99 lines
1.9 KiB
Go

package database
import (
"fmt"
"log"
"strings"
"sync"
"time"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
var (
db *gorm.DB
once sync.Once
)
// Connect initializes the database connection (singleton pattern).
// If dsn starts with postgres:// or postgresql://, connects to PostgreSQL.
// Otherwise falls back to SQLite (development only).
func Connect(dsn string) (*gorm.DB, error) {
var err error
once.Do(func() {
config := &gorm.Config{
Logger: logger.Default.LogMode(logger.Warn),
}
isPostgres := strings.HasPrefix(dsn, "postgres://") ||
strings.HasPrefix(dsn, "postgresql://")
if isPostgres {
db, err = gorm.Open(postgres.Open(dsn), config)
} else {
db, err = gorm.Open(sqlite.Open(dsn), config)
}
if err != nil {
err = fmt.Errorf("failed to connect to database: %w", err)
return
}
// Configure connection pool for PostgreSQL.
if isPostgres {
sqlDB, poolErr := db.DB()
if poolErr != nil {
err = fmt.Errorf("failed to get sql.DB: %w", poolErr)
return
}
sqlDB.SetMaxOpenConns(25)
sqlDB.SetMaxIdleConns(5)
sqlDB.SetConnMaxLifetime(5 * time.Minute)
}
log.Println("Database connection established")
})
return db, err
}
// GetDB returns the database instance (must call Connect first)
func GetDB() *gorm.DB {
if db == nil {
log.Fatal("Database not initialized. Call Connect() first.")
}
return db
}
// Close closes the database connection
func Close() error {
if db == nil {
return nil
}
sqlDB, err := db.DB()
if err != nil {
return fmt.Errorf("failed to get database instance: %w", err)
}
if err := sqlDB.Close(); err != nil {
return fmt.Errorf("failed to close database: %w", err)
}
log.Println("Database connection closed")
return nil
}
// ResetForTesting resets the singleton for testing purposes
// WARNING: Only use this in tests!
func ResetForTesting() {
if db != nil {
Close()
db = nil
once = sync.Once{}
}
}