From 795fb7facf403f4e3d452d2e08ba11f98e8ee997 Mon Sep 17 00:00:00 2001 From: wikiapiserver Date: Thu, 25 Jun 2026 21:35:50 +0200 Subject: feat: log article API failures to database - Created api_logs table (username, article_name, status_code, response_time_ms, error, request_url) - GetArticle logs failures (network errors and non-2xx responses) with timing, status code, and response body - Successful requests are not logged --- db/db.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'db') diff --git a/db/db.go b/db/db.go index 228a655..7409047 100644 --- a/db/db.go +++ b/db/db.go @@ -325,3 +325,27 @@ func (d *DB) HealthCheck(ctx context.Context) error { _, err := d.conn.ExecContext(ctx, "SELECT 1") return err } + +// ApiLogEntry represents an API call log. +type ApiLogEntry struct { + Username string + ArticleName string + StatusCode int + ResponseMs int + Error string + RequestURL string +} + +// LogApiCall stores an API call log in the database. +func (d *DB) LogApiCall(ctx context.Context, entry *ApiLogEntry) error { + _, err := d.conn.ExecContext(ctx, + `INSERT INTO api_logs (username, article_name, status_code, response_time_ms, error, request_url) + VALUES (?, ?, ?, ?, ?, ?)`, + entry.Username, entry.ArticleName, entry.StatusCode, + entry.ResponseMs, entry.Error, entry.RequestURL, + ) + if err != nil { + return fmt.Errorf("log api call: %w", err) + } + return nil +} -- cgit v1.2.3