Files
book-comic-library/backend/internal/seed/seed.go
T
XingfenD 85c61d9f24 refactor: consolidate isUnique and path validation helpers (Task 28)
- seed.Admin: hand-rolled pgconn.PgError 23505 check → store.IsUniqueViolation
  (single predicate for unique violations across the codebase)
- books.absBookPath: local hasPrefixDir (filepath.Rel-based) removed in favor
  of bookfile.Contains, which also resolves symlinks — stricter escape check
- handlers isUnique alias + libraries.go local prefix check were already
  folded into ports.IsUniqueViolation / bookfile.Contains in Task 25;
  scanner.inside was folded in Task 24 — this commit closes the last two

Full gate green: gofmt, vet, go test -p 1 (real PG+Redis, 0 skip)
2026-09-14 23:26:16 +08:00

37 lines
761 B
Go

package seed
import (
"context"
"log"
"booklib/internal/auth"
"booklib/internal/store"
)
func Admin(ctx context.Context, s *store.Store, user, pass string) error {
if user == "" || pass == "" {
return nil
}
if len(pass) < 8 { // 与 API 建户口令下限一致
log.Printf("seed admin skipped: ADMIN_PASSWORD must be >= 8 chars")
return nil
}
n, err := s.CountUsers(ctx)
if err != nil || n > 0 {
return err
}
h, err := auth.HashPassword(pass)
if err != nil {
return err
}
if _, err := s.CreateUser(ctx, user, h, "admin"); err != nil {
if store.IsUniqueViolation(err) {
log.Printf("seed admin %q may already exist: %v", user, err)
return nil
}
return err
}
log.Printf("seeded initial admin user %q", user)
return nil
}