From 4907cfb72ef69fb94c49cfb484a5a2907db0dee8 Mon Sep 17 00:00:00 2001 From: wikiapiserver Date: Thu, 25 Jun 2026 22:00:25 +0200 Subject: fix: pass through upstream HTTP status codes from /article - Non-2xx responses (404, 429, 5xx) are now forwarded to the client with their original status code and response body - Increased WriteTimeout to 45s to accommodate slow upstream API - Added explicit resp.Body.Close() and response flush before return --- api/handlers.go | 10 +++++++++- main.go | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/api/handlers.go b/api/handlers.go index 04fcc23..93f605e 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -258,6 +258,8 @@ func (h *Handler) GetArticle(w http.ResponseWriter, r *http.Request) { if resp.StatusCode < 200 || resp.StatusCode >= 300 { b, _ := io.ReadAll(resp.Body) + resp.Body.Close() + log.Printf("article request failed: user=%s article=%s status=%d body=%s (%dms)", username, article, resp.StatusCode, string(b), elapsed) h.db.LogApiCall(ctx, &db.ApiLogEntry{ @@ -268,7 +270,13 @@ func (h *Handler) GetArticle(w http.ResponseWriter, r *http.Request) { Error: string(b), RequestURL: queryURL, }) - http.Error(w, string(b), resp.StatusCode) + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(resp.StatusCode) + w.Write(b) //nolint:errcheck + if f, ok := w.(http.Flusher); ok { + f.Flush() + } return } diff --git a/main.go b/main.go index 341a175..1633e96 100644 --- a/main.go +++ b/main.go @@ -94,7 +94,7 @@ func main() { Addr: addr, Handler: mux, ReadTimeout: 5 * time.Second, - WriteTimeout: 10 * time.Second, + WriteTimeout: 45 * time.Second, IdleTimeout: 120 * time.Second, } -- cgit v1.2.3