Files
Crussell/backend/handlers/user/gdpr_export.go
T
popertots 990c86495c
CI / Docker compose check (push) Successful in 1m6s
CI / Env docs check (push) Successful in 1m6s
CI / Frontend deps check (push) Successful in 1m9s
CI / Frontend major deps (push) Failing after 1m9s
CI / Secrets scan (push) Successful in 1m9s
CI / Go build (push) Successful in 1m10s
CI / Frontend build (push) Successful in 1m10s
CI / Nginx config check (push) Successful in 1m12s
CI / Knip (push) Successful in 25s
CI / Go vet (prod) (push) Successful in 2m1s
CI / Frontend a11y check (push) Successful in 2m13s
CI / Go vet (dev) (push) Successful in 2m20s
CI / go mod tidy (push) Successful in 1m18s
CI / Staticcheck (prod) (push) Successful in 3m23s
CI / Staticcheck (dev) (push) Successful in 3m25s
CI / golangci-lint (push) Successful in 3m51s
CI / Frontend QC (audit) (push) Successful in 2m9s
CI / Security scan (prod) (push) Successful in 4m5s
CI / Security scan (dev) (push) Successful in 4m31s
CI / Go vulnerabilities (push) Successful in 2m22s
CI / Frontend QC (lint) (push) Failing after 1m11s
CI / Frontend QC (typecheck) (push) Successful in 1m21s
CI / Svelte strict check (push) Has been skipped
CI / Tests (prod) (push) Successful in 1m52s
CI / Tests (dev) (push) Failing after 2m12s
CI / Race (prod) (push) Successful in 3m31s
CI / Race (dev) (push) Successful in 5m0s
fix: suppress unused nonDepositPaymentType in prod-staticcheck
2026-07-10 13:00:33 +01:00

96 lines
2.2 KiB
Go

package user
import (
"context"
"encoding/json"
"log"
"net/http"
"sync"
"time"
"crussell/db"
"crussell/clock"
"crussell/mw"
)
var (
gdprExportCache = make(map[string]*gdprCacheEntry)
gdprExportCacheMu sync.RWMutex
)
type gdprCacheEntry struct {
data json.RawMessage
expiresAt time.Time
generating bool
}
// CleanupGDPRExportCache removes expired entries from the GDPR export cache.
// Called by the centralised jobs scheduler.
func CleanupGDPRExportCache(ctx context.Context) (int, error) {
gdprExportCacheMu.Lock()
defer gdprExportCacheMu.Unlock()
now := clock.Now()
var n int
for k, v := range gdprExportCache {
if now.After(v.expiresAt) {
delete(gdprExportCache, k)
n++
}
}
return n, nil
}
// GET /api/user/gdpr-export
func GetGDPRExportHandler(w http.ResponseWriter, r *http.Request) {
userID, ok := mw.GetUserID(r.Context())
if !ok {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
gdprExportCacheMu.Lock()
if entry, found := gdprExportCache[userID]; found && clock.Now().Before(entry.expiresAt) {
if entry.generating {
gdprExportCacheMu.Unlock()
w.Header().Set("X-Cache", "GENERATING")
_, _ = w.Write([]byte(`{"status":"generating"}`))
return
}
gdprExportCacheMu.Unlock()
w.Header().Set("X-Cache", "HIT")
_, _ = w.Write(entry.data)
return
}
gdprExportCache[userID] = &gdprCacheEntry{generating: true}
gdprExportCacheMu.Unlock()
// #nosec G118 — intentional background goroutine for async GDPR export
go func() {
defer func() {
if r := recover(); r != nil {
log.Printf("Panic recovered in GDPR export query: %v", r)
}
}()
var result json.RawMessage
err := db.Conn.QueryRow(context.Background(), `SELECT export_all_user_data($1)`, userID).Scan(&result)
if err != nil {
log.Printf("GDPR export failed for user %s: %v", userID, err)
gdprExportCacheMu.Lock()
delete(gdprExportCache, userID)
gdprExportCacheMu.Unlock()
return
}
gdprExportCacheMu.Lock()
gdprExportCache[userID] = &gdprCacheEntry{
data: result,
expiresAt: clock.Now().Add(12 * time.Hour),
}
gdprExportCacheMu.Unlock()
}()
w.Header().Set("X-Cache", "MISS")
_, _ = w.Write([]byte(`{"status":"generating"}`))
}