Documentation
¶
Overview ¶
Package audit records an activity log of changes to your models/resources — who did what to which subject, with a field-level diff — plus optional model versioning with restore. It is the togo answer to Spatie Activitylog / PaperTrail / django-reversion.
a, _ := audit.FromKernel(k) a.LogChange(ctx, "updated", "post", post.ID, userID, oldPost, newPost) // diffs old→new a.Snapshot(ctx, "post", post.ID, oldPost, userID) // version for restore
Index ¶
- func Diff(old, new map[string]any) map[string]any
- func Middleware(next http.Handler) http.Handler
- func WithMeta(ctx context.Context, m Meta) context.Context
- type Change
- type Entry
- type Filter
- type JSONText
- type Meta
- type Service
- func (s *Service) Activity(ctx context.Context, f Filter) ([]Entry, error)
- func (s *Service) Log(ctx context.Context, action, subjectType, subjectID, causerID string, ...) (*Entry, error)
- func (s *Service) LogChange(ctx context.Context, action, subjectType, subjectID, causerID string, ...) (*Entry, error)
- func (s *Service) Restore(ctx context.Context, versionID string) (map[string]any, *Version, error)
- func (s *Service) Snapshot(ctx context.Context, subjectType, subjectID string, data map[string]any, ...) (*Version, error)
- func (s *Service) Versions(ctx context.Context, subjectType, subjectID string) ([]Version, error)
- type Version
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Diff ¶
Diff returns a changeset of fields that differ between old and new (added/removed/changed), each as {old,new}.
func Middleware ¶
Middleware captures the causer (X-User-Id header), client IP and user-agent into the request context so subsequent audit.Log calls auto-fill them.
Types ¶
type Entry ¶
type Entry struct {
ID string `db:"id" json:"id"`
Action string `db:"action" json:"action"`
SubjectType string `db:"subject_type" json:"subject_type"`
SubjectID string `db:"subject_id" json:"subject_id"`
CauserID string `db:"causer_id" json:"causer_id"`
Changes JSONText `db:"changes" json:"changes"`
IP string `db:"ip" json:"ip,omitempty"`
UserAgent string `db:"user_agent" json:"user_agent,omitempty"`
CreatedAt string `db:"created_at" json:"created_at"`
}
Entry is one activity-log record.
type Filter ¶
type Filter struct {
SubjectType string
SubjectID string
CauserID string
Action string
Limit int
Offset int
}
Filter narrows an activity query.
type JSONText ¶
type JSONText []byte
JSONText is a JSON value stored in a text column. It scans from string/[]byte and marshals back to raw JSON (so API responses nest objects, not strings).
func (JSONText) MarshalJSON ¶
MarshalJSON emits the raw JSON.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the audit runtime stored on the kernel (k.Get("audit")).
func FromKernel ¶
FromKernel returns the audit Service registered on the kernel.
func (*Service) Log ¶
func (s *Service) Log(ctx context.Context, action, subjectType, subjectID, causerID string, changes map[string]any) (*Entry, error)
Log writes an activity-log entry. changes is an arbitrary changeset (often the output of Diff). IP / user-agent / causer are filled from the request context (see Middleware) when not supplied.
func (*Service) LogChange ¶
func (s *Service) LogChange(ctx context.Context, action, subjectType, subjectID, causerID string, old, new map[string]any) (*Entry, error)
LogChange diffs old→new and records the changeset as an "updated"-style entry.
func (*Service) Restore ¶
Restore returns the snapshot data for a version id. The caller applies it to the live record (audit owns history, not your tables).
type Version ¶
type Version struct {
ID string `db:"id" json:"id"`
SubjectType string `db:"subject_type" json:"subject_type"`
SubjectID string `db:"subject_id" json:"subject_id"`
Data JSONText `db:"data" json:"data"`
CauserID string `db:"causer_id" json:"causer_id,omitempty"`
CreatedAt string `db:"created_at" json:"created_at"`
}
Version is a stored snapshot of a subject, used to restore prior state.