first commit

This commit is contained in:
2026-04-29 02:35:00 +00:00
commit 2cb17df4c5
90 changed files with 7321 additions and 0 deletions

44
internal/audit/audit.go Normal file
View File

@@ -0,0 +1,44 @@
package audit
import (
"context"
"encoding/json"
"log/slog"
"github.com/noderunners/nip05api/internal/db"
)
const (
ActorAdmin = "admin"
ActorSystem = "system"
ActionUserAdded = "user.added"
ActionUserUsernameChanged = "user.username_changed"
ActionUserExtended = "user.extended"
ActionUserDeleted = "user.deleted"
ActionPaymentConfirmed = "payment.confirmed"
ActionUserExpired = "user.expired"
ActionUserGracePurged = "user.grace_purged"
)
type Logger struct{ db *db.DB }
func New(d *db.DB) *Logger { return &Logger{db: d} }
func (l *Logger) Log(ctx context.Context, action, actor, pubkey string, details map[string]any) {
if l == nil || l.db == nil {
return
}
var detailsJSON string
if details != nil {
b, err := json.Marshal(details)
if err == nil {
detailsJSON = string(b)
}
}
if _, err := l.db.ExecContext(ctx,
`INSERT INTO audit_log (action, actor, pubkey, details) VALUES (?, ?, ?, ?)`,
action, actor, pubkey, detailsJSON); err != nil {
slog.Warn("audit log insert failed", "action", action, "err", err)
}
}