mirror of
https://github.com/marcusDenslow/terminalShop.git
synced 2026-10-25 10:02:21 +01:00
* feat(sca): expire inactive saved cards * fix(sca): address card ttl review feedback - Correct sql typos breaking GET /cards and /view/init (used_id, id_default) - Stop swallowing backfillCardStorageExpiration error in Migrate - Wrap expireStoredCard local writes in a transaction and run stripe detach + audit only after commit, preventing double-fire on retry - Drop redundant ttl refresh in ConvertCart; webhook is authoritative - Rename validation_expired_at metric label to validation_card_expired - Fix CardStorageExpiresAt doc comment and test failure message typos - Add handler-level tests for expireStoredCards (user scoping + cart deref) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * refactor(sca): move card expiration to periodic reconciler Read paths (GetCards, GetViewInit) now filter expired cards from query results instead of detaching them inline, so request handlers no longer block on Stripe API calls. Physical deletion runs in a new ReconcileExpiredCards goroutine on an hourly ticker, mirroring the existing ReconcileOrders / ReconcileUnshipped jobs. Per-card action sites (SetCard, SetDefaultCard, ConvertCart) still expire synchronously since they operate on one row the user is acting on. ViewHandler's stripeKey dependency is dropped now that GetViewInit no longer touches Stripe. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(sca): address card ttl review feedback - guard zero order.CardID before refreshing card storage ttl in payment_intent.succeeded webhook to avoid noisy warn logs on payment paths that did not use a saved card - delete unused Card.RefreshStorageTTL model method; the SQL-only refreshCardStorageTTL helper is the single canonical path - clarify duplicate-card webhook path: storage_expires_at reset is a card-add refresh, not a payment-success refresh, so last_used_at intentionally stays untouched Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * test(sca): cover card ttl read paths and expired-card sync sites --------- Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
164 lines
4.1 KiB
Go
164 lines
4.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"terminalShop/api/handlers"
|
|
"terminalShop/api/middleware"
|
|
"terminalShop/api/routes"
|
|
"terminalShop/pkg/audit"
|
|
"terminalShop/pkg/auth"
|
|
"terminalShop/pkg/config"
|
|
"terminalShop/pkg/database"
|
|
"terminalShop/pkg/observability"
|
|
"terminalShop/pkg/stripeclient"
|
|
)
|
|
|
|
const version = "v0.1.0"
|
|
|
|
func main() {
|
|
observability.InitLogger()
|
|
middleware.SetBuildInfo(version)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
// Load configuration
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
log.Fatalf("Failed to load config: %v", err)
|
|
}
|
|
|
|
// Initialize database connection
|
|
db, err := database.Connect(cfg.DatabaseURL)
|
|
if err != nil {
|
|
log.Fatalf("Failed to connect to database: %v", err)
|
|
}
|
|
|
|
defer func() { _ = database.Close() }()
|
|
|
|
// Run migrations
|
|
if err := database.Migrate(db); err != nil {
|
|
log.Fatalf("Failed to run migrations: %v", err)
|
|
}
|
|
audit.SetDB(db)
|
|
|
|
// Seed database with initial data
|
|
if err := database.Seed(db); err != nil {
|
|
log.Fatalf("Failed to seed database: %v", err)
|
|
}
|
|
|
|
otlpEndpoint := os.Getenv("OTLP_ENDPOINT")
|
|
if otlpEndpoint == "" {
|
|
otlpEndpoint = "tempo:4318"
|
|
}
|
|
shutdown, err := observability.InitTracing(context.Background(), "terminalshop-api", otlpEndpoint)
|
|
if err != nil {
|
|
log.Printf("tracing init failed (non-fatal): %v", err)
|
|
} else {
|
|
defer func() { _ = shutdown(context.Background()) }()
|
|
}
|
|
|
|
stripeclient.InitOTel()
|
|
|
|
// Init JWT manager (same secret + duration as SSH server)
|
|
jwtManager := auth.NewJWTManager(cfg.JWTSecret, 30*time.Minute)
|
|
|
|
// Setup routes
|
|
router := routes.SetupRoutes(version, cfg.StripeSecretKey, cfg.StripeWebhookSecret, jwtManager, cfg.AuthFingerprintKey, cfg.ShippoAPIKey, cfg.BringAPIUID, cfg.BringAPIKey, cfg.BringCustomerNumber, cfg.ShippoWebhookSecret, cfg.AppURL, cfg.SlackSigningSecret, cfg.AdminAPIKey, cfg.APIPort, cfg.MaxOrderCents)
|
|
|
|
// Create HTTP server
|
|
server := &http.Server{
|
|
Addr: fmt.Sprintf(":%s", cfg.APIPort),
|
|
Handler: router,
|
|
ReadTimeout: 15 * time.Second,
|
|
WriteTimeout: 15 * time.Second,
|
|
IdleTimeout: 60 * time.Second,
|
|
}
|
|
|
|
// Channel to listen for interrupt signals
|
|
done := make(chan os.Signal, 1)
|
|
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
// Start server in a goroutine
|
|
go func() {
|
|
log.Printf("Starting API server on port %s", cfg.APIPort)
|
|
log.Printf("Environment: %s", cfg.Environment)
|
|
log.Printf("Database: %s", cfg.SafeDatabaseURL())
|
|
log.Printf("Health check: http://localhost:%s/api/v1/health", cfg.APIPort)
|
|
|
|
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
log.Fatalf("Server failed to start: %v", err)
|
|
}
|
|
}()
|
|
|
|
// Start reconciliation job. runs every 5 minutes to catch any orders
|
|
// that were charged but not recorded due to a crash.
|
|
go func() {
|
|
ticker := time.NewTicker(5 * time.Minute)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-ticker.C:
|
|
handlers.ReconcileOrders(cfg.StripeSecretKey)
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
go func() {
|
|
time.Sleep(30 * time.Second)
|
|
handlers.ReconcileUnshipped()
|
|
ticker := time.NewTicker(24 * time.Hour)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-ticker.C:
|
|
handlers.ReconcileUnshipped()
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
// Sweep saved cards whose retention deadline elapsed. Read paths filter
|
|
// expired rows from query results, so this sweeper only drives the Stripe
|
|
// detach + audit + physical row delete asynchronously.
|
|
go func() {
|
|
ticker := time.NewTicker(1 * time.Hour)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-ticker.C:
|
|
handlers.ReconcileExpiredCards(cfg.StripeSecretKey)
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
log.Println("API server started. Press Ctrl+C to shutdown.")
|
|
|
|
// Block until signal received
|
|
<-done
|
|
cancel()
|
|
log.Println("Server is shutting down...")
|
|
|
|
// Graceful shutdown with 30 second timeout
|
|
ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
|
|
if err := server.Shutdown(ctx); err != nil {
|
|
log.Fatalf("Server forced to shutdown: %v", err)
|
|
}
|
|
|
|
log.Println("Server exited gracefully")
|
|
}
|