feat(audit): recorder with pg implementation and ip parsing

This commit is contained in:
2026-04-28 22:53:22 +08:00
parent 0b323b91ee
commit f42b88e57d
3 changed files with 129 additions and 9 deletions
+25 -7
View File
@@ -7,15 +7,33 @@ package auditsqlc
import (
"context"
"net/netip"
"github.com/jackc/pgx/v5/pgtype"
)
const ping = `-- name: Ping :one
SELECT 1::int AS ok
const insertAuditLog = `-- name: InsertAuditLog :exec
INSERT INTO audit_logs (user_id, action, target_type, target_id, ip, metadata)
VALUES ($1, $2, $3, $4, $5, $6)
`
func (q *Queries) Ping(ctx context.Context) (int32, error) {
row := q.db.QueryRow(ctx, ping)
var ok int32
err := row.Scan(&ok)
return ok, err
type InsertAuditLogParams struct {
UserID pgtype.UUID `json:"user_id"`
Action string `json:"action"`
TargetType *string `json:"target_type"`
TargetID *string `json:"target_id"`
Ip *netip.Addr `json:"ip"`
Metadata []byte `json:"metadata"`
}
func (q *Queries) InsertAuditLog(ctx context.Context, arg InsertAuditLogParams) error {
_, err := q.db.Exec(ctx, insertAuditLog,
arg.UserID,
arg.Action,
arg.TargetType,
arg.TargetID,
arg.Ip,
arg.Metadata,
)
return err
}