@@ -0,0 +1,51 @@
|
||||
package audit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.gangary.cn/gary/Hugs-Proxy/internal/db"
|
||||
)
|
||||
|
||||
type Entry struct {
|
||||
RequestIP string
|
||||
UserID int64
|
||||
HasUser bool
|
||||
TokenID int64
|
||||
HasToken bool
|
||||
OriginalURL string
|
||||
HTTPStatus int
|
||||
Success bool
|
||||
ErrorReason string
|
||||
CountAsSuccess bool
|
||||
OccurredAt time.Time
|
||||
}
|
||||
|
||||
type Logger struct {
|
||||
store *db.Store
|
||||
}
|
||||
|
||||
func NewLogger(store *db.Store) *Logger {
|
||||
return &Logger{store: store}
|
||||
}
|
||||
|
||||
func (l *Logger) Log(ctx context.Context, entry Entry) error {
|
||||
usage := db.UsageEntry{
|
||||
RequestIP: strings.TrimSpace(entry.RequestIP),
|
||||
OriginalURL: strings.TrimSpace(entry.OriginalURL),
|
||||
HTTPStatus: entry.HTTPStatus,
|
||||
Success: entry.Success,
|
||||
ErrorReason: strings.TrimSpace(entry.ErrorReason),
|
||||
OccurredAt: entry.OccurredAt,
|
||||
}
|
||||
if entry.HasUser {
|
||||
usage.UserID = sql.NullInt64{Int64: entry.UserID, Valid: true}
|
||||
}
|
||||
if entry.HasToken {
|
||||
usage.TokenID = sql.NullInt64{Int64: entry.TokenID, Valid: true}
|
||||
}
|
||||
|
||||
return l.store.RecordUsageAndMaybeIncrement(ctx, usage, entry.CountAsSuccess)
|
||||
}
|
||||
Reference in New Issue
Block a user