diff options
| author | wikiapiserver | 2026-06-25 21:35:50 +0200 |
|---|---|---|
| committer | wikiapiserver | 2026-06-25 21:35:50 +0200 |
| commit | 795fb7facf403f4e3d452d2e08ba11f98e8ee997 (patch) | |
| tree | cab7c8dc6b16ef0d5cbd9871edb1bbab9fd4161c /db | |
| parent | 550d382014b5f3476caed167025d6c4be16d844d (diff) | |
| download | wikiapiserver-795fb7facf403f4e3d452d2e08ba11f98e8ee997.tar.gz | |
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
Diffstat (limited to 'db')
| -rw-r--r-- | db/db.go | 24 |
1 files changed, 24 insertions, 0 deletions
@@ -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 +} |
